2.route路由配置

摘要:
需要注意的是,route命令直接在命令行执行,以添加一条路由,而不会永久保存。当网卡重新启动或机器重新启动时,路由将变为无效;您可以在/etc/rc.local中添加路由命令,以确保路由设置永久有效。要获取接口列表及其对应的接口索引,请使用routeprint命令的显示功能。

转自 http://www.cnblogs.com/peida/archive/2013/03/05/2943698.html

Linux系统的route命令用于显示和操作IP路由表(show / manipulate the IP routing table)。要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现。在Linux系统中,设置路由通常是为了解决以下问题:该Linux系统在一个局域网中,局域网中有一个网关,能够让机器访问Internet,那么就需要将这台机器的IP地址设置为Linux机器的默认路由。要注意的是,直接在命令行下执行route命令来添加路由,不会永久保存,当网卡重启或者机器重启之后,该路由就失效了;可以在/etc/rc.local中添加route命令来保证该路由设置永久有效。

1.命令格式:

route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]] 

2.命令功能:

Route命令是用于操作基于内核ip路由表,它的主要作用是创建一个静态路由让指定一个主机或者一个网络通过一个网络接口,如eth0。当使用"add"或者"del"参数时,路由表被修改,如果没有参数,则显示路由表当前的内容。

3.命令参数:

-c 显示更多信息

-n 不解析名字

-v 显示详细的处理信息

-F 显示发送信息

-C 显示路由缓存

-f 清除所有网关入口的路由表。 

-p 与 add 命令一起使用时使路由具有永久性。

add:添加一条新路由。

del:删除一条路由。

-net:目标地址是一个网络。

-host:目标地址是一个主机。

netmask:当添加一个网络路由时,需要使用网络掩码。

gw:路由数据包通过网关。注意,你指定的网关必须能够达到。

metric:设置路由跳数。

Command 指定您想运行的命令 (Add/Change/Delete/Print)。 

Destination 指定该路由的网络目标。 

mask Netmask 指定与网络目标相关的网络掩码(也被称作子网掩码)。 

Gateway 指定网络目标定义的地址集和子网掩码可以到达的前进或下一跃点 IP 地址。 

metric Metric 为路由指定一个整数成本值标(从 1 至 9999),当在路由表(与转发的数据包目标地址最匹配)的多个路由中进行选择时可以使用。 

if Interface 为可以访问目标的接口指定接口索引。若要获得一个接口列表和它们相应的接口索引,使用 route print 命令的显示功能。可以使用十进制或十六进制值进行接口索引。

4.使用实例:

实例1:显示当前路由

命令:

route

route -n

输出:

复制代码
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
e192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
0.0.0.0         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
复制代码

说明:

第一行表示主机所在网络的地址为192.168.120.0,若数据传送目标是在本局域网内通信,则可直接通过eth0转发数据包;

第四行表示数据传送目的是访问Internet,则由接口eth0,将数据包发送到网关192.168.120.240

其中Flags为路由标志,标记当前网络节点的状态。

Flags标志说明:

U Up表示此路由当前为启动状态

H Host,表示此网关为一主机

G Gateway,表示此网关为一路由器

R Reinstate Route,使用动态路由重新初始化的路由

D Dynamically,此路由是动态性地写入

M Modified,此路由是由路由守护程序或导向器动态修改

! 表示此路由当前为关闭状态

备注:

route -n (-n 表示不解析名字,列出速度会比route 快)

实例2:添加网关/设置网关

命令:

route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0

输出:

复制代码
[root@localhost ~]# route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       *               240.0.0.0       U     0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
复制代码

[root@localhost ~]#  

说明:

增加一条 到达244.0.0.0的路由

实例3:屏蔽一条路由

命令:

route add -net 224.0.0.0 netmask 240.0.0.0 reject

输出:

复制代码
[root@localhost ~]# route add -net 224.0.0.0 netmask 240.0.0.0 reject
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       -               240.0.0.0       !     0      -        0 -
224.0.0.0       *               240.0.0.0       U     0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
复制代码

说明:

增加一条屏蔽的路由,目的地址为 224.x.x.x 将被拒绝

实例4:删除路由记录

命令:

route del -net 224.0.0.0 netmask 240.0.0.0

route del -net 224.0.0.0 netmask 240.0.0.0 reject

输出:

复制代码
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       -               240.0.0.0       !     0      -        0 -
224.0.0.0       *               240.0.0.0       U     0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# route del -net 224.0.0.0 netmask 240.0.0.0
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       -               240.0.0.0       !     0      -        0 -
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# route del -net 224.0.0.0 netmask 240.0.0.0 reject
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# 
复制代码

说明:

实例5:删除和添加设置默认网关

命令:

route del default gw 192.168.120.240

route add default gw 192.168.120.240

输出:

复制代码
[root@localhost ~]# route del default gw 192.168.120.240
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
[root@localhost ~]# route add default gw 192.168.120.240
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# 
复制代码

说明:

http://www.tldp.org/HOWTO/Multicast-HOWTO-3.html

组播地址及路由           

⑴ D类地址不分网络地址和主机地址,它的第1个字节的前四位固定为1110,即224
⑵ D类地址范围:224.0.0.1—239.255.255.254, 即1110000.00000000.00000000.00000001 -  11101111.11111111.11111111.1111110

Once you have compiled and installed your new kernel, you should provide a default route for multicast traffic. The goal is to add a route to the network 224.0.0.0.

The problem most people seem to face in this stage of the configuration is with the value of the mask to supply. If you have read Terry Dawson's excellent NET-3-HOWTO, it should not be difficult to guess the correct value, though. As explained there, the netmask is a 32 bit number filled with all-1s in the network part of your IP address, and with all-0s in the host part. Recall from section 2.1 that a class D multicast address has no netwok/host sections. Instead it has a 28-bit group identifier and a 4-bit class D identifier. Well, this 4 bits are the network part and the remaining 28 the host part. So the netmask needed is 11110000000000000000000000000000 or, easier to read: 240.0.0.0. Then, the full command should be:

route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
or
route add -net 224.0.0.0/4 dev eth0

Depending on how old your route program is, you might need to add the -net flag after the add.

Here we supposed that eth0 was multicast-capable and that, when not otherwise specified, we wanted multicast traffic to be output there. If this is not your case, change the devparameter as appropriate.

The /proc filesystem proves here to be useful once again: you can check /proc/net/igmp to see the groups your host is currently subscribed to.

免责声明:文章转载自《2.route路由配置》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Android模拟器使用SD卡OPENQUERY (Transact-SQL),跨数据库操作。下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

路由协议之OSPF

目录 OSPF协议 OSPF的七种状态 OSPF的11种LSA Stub和Nssa OSPF中的防环机制 OSPF中的路由汇总和路由过滤 OSPF中的虚拟链路 虚拟链路有两种存在的意义 OSPF中的认证 华为模拟器中的配置 OSPF协议OSPF(Open Shortest Path First开放式最短路径优先)是一个内部网关协议IGP,用于在单一自治系...

kickstart详解

ks详解 一、文件组成 文件由三部分组成 1、选项指令段 用于自动应答图形/文本界面安装时除了包以外的所有操作 2、package选择段 使用%packages引导该功能 3、脚本段 脚本段分为两部分 1)%pre 预安装脚本段,在安装系统之前就执行的脚本,该段很少使用,命令太少了 2)%post 后安装脚本段,在系统安装完成后执行的较薄 二、ks必须选项...

vue+elementui搭建后台管理界面(5递归生成侧栏路由)

有一个菜单树,顶层菜单下面有多个子菜单,子菜单下还有子菜单。。。 这时候就要用递归处理 1 定义多级菜单 修改 src/router/index.js 的 / 路由 { path: '/', redirect: '/dashboard', name: 'Container', component: Container,...

完美的外出上网解决方案随身随地享用你的专有WIFI网络(3G无线路由器+sim卡卡托+3G资费卡)

距离元旦和春节长假越来越近了,相信周围很多朋友肯定是打算趁着长假出去旅旅游。正好前几天跟一个朋友聊到在外面上网不方便的问题。因此我今天专门写一篇Blog来介绍一下我当时去西藏时的上网解决方案,供周围有类似外出需要上网的朋友参考。 正式介绍之前为了便于“小白”朋友的理解,我先介绍几个概念: 3G网络:具体的概念在这里我就不废话了,你只要STFG(Search...

thinkphp3.2.3中设置路由,优化url

需求:访问这个目录的时候,http://xx.com/p-412313要重定向到(暂且这么叫)http://xx.com/Home/Blog/index/id/412313 就是看着好看 我的应用目录是Application。模块是Home 第一步:知道哪个文件怎么处理的路由路由处理在think/Route.class.php [php]view p...

vue-router路由跳转,实现target: _blank,单独打开一个标签页

1,编程式路由跳转 let { href } = this.$router.resolve({ path: '/help-center' }) window.open(href, '_blank') 2,页面标签跳转 <div class="item-fore7 cur">注册即代表同意 <router-link ta...