您的位置:首页 > 运维架构 > Linux

linux 路由表设置 之 route 指令详解

2017-07-26 00:42 676 查看
使用下面的 route 命令可以查看 Linux 内核路由表。

默认情况下,route会去找出该IP的主机名称,如果找不到,就会显得有点迟钝,所以通常使用route -n。

[root@racnode1 ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.236.46.0     10.236.23.251   255.255.255.0   UG    0      0        0 eth1
10.236.202.0    10.236.23.251   255.255.255.0   UG    0      0        0 eth1
10.236.61.0     10.236.23.251   255.255.255.0   UG    0      0        0 eth1
10.236.95.0     10.236.23.251   255.255.255.0   UG    0      0        0 eth1
10.236.6.0      10.236.23.251   255.255.255.0   UG    0      0        0 eth1
10.236.101.0    *               255.255.255.0   U     0      0        0 eth0
10.236.69.0     10.236.23.251   255.255.255.0   UG    0      0        0 eth1
10.236.23.0     *               255.255.255.0   U     0      0        0 eth1
10.236.53.0     10.236.23.251   255.255.255.0   UG    0      0        0 eth1
10.232.21.0     10.236.23.251   255.255.255.0   UG    0      0        0 eth1
10.236.51.0     10.236.23.251   255.255.255.0   UG    0      0        0 eth1
169.254.0.0     *               255.255.0.0     U     0      0        0 eth1
default         10.236.101.254  0.0.0.0         UG    0      0        0 eth0
[root@racnode1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.236.46.0     10.236.23.251   255.255.255.0   UG    0      0        0 eth1
10.236.202.0    10.236.23.251   255.255.255.0   UG    0      0        0 eth1
10.236.61.0     10.236.23.251   255.255.255.0   UG    0      0        0 eth1
10.236.95.0     10.236.23.251   255.255.255.0   UG    0      0        0 eth1
10.236.6.0      10.236.23.251   255.255.255.0   UG    0      0        0 eth1
10.236.101.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
10.236.69.0     10.236.23.251   255.255.255.0   UG    0      0        0 eth1
10.236.23.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
10.236.53.0     10.236.23.251   255.255.255.0   UG    0      0        0 eth1
10.232.21.0     10.236.23.251   255.255.255.0   UG    0      0        0 eth1
10.236.51.0     10.236.23.251   255.255.255.0   UG    0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth1
0.0.0.0         10.236.101.254  0.0.0.0         UG    0      0        0 eth0


route 命令的输出项说明

输出项 说明

Destination 目标网段或者主机

Gateway 网关地址,”*” 表示目标是本主机所属的网络,不需要路由

Genmask 网络掩码

Flags 标记。一些可能的标记如下:

U — 路由是活动的

H — 目标是一个主机

G — 路由指向网关

R — 恢复动态路由产生的表项

D — 由路由的后台程序动态地安装

M — 由路由的后台程序修改

! — 拒绝路由

Metric 路由距离,到达指定网络所需的中转数(linux 内核中没有使用)

Ref 路由项引用次数(linux 内核中没有使用)

Use 此路由项被路由软件查找的次数

Iface 该路由表项对应的输出接口

3 种路由类型

主机路由

主机路由是路由选择表中指向单个IP地址或主机名的路由记录。主机路由的Flags字段为H。例如,在下面的示例中,本地主机通过IP地址10.236.101.254的路由器到达IP地址为10.236.202.116的主机。
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.236.202.116  10.236.101.254  255.255.255.255 UGH   0      0        0 eth0


网络路由

网络路由是代表主机可以到达的网络。网络路由的Flags字段为N。例如,在下面的示例中,本地主机将发送到网络10.236.46的数据包转发到IP地址为10.236.23.251的路由器。
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.236.46.0     10.236.23.251   255.255.255.0   UG    0      0        0 eth1


默认路由

当主机不能在路由表中查找到目标主机的IP地址或网络路由时,数据包就被发送到默认路由(默认网关)上。默认路由的Flags字段为G。例如,在下面的示例中,默认路由是IP地址为10.236.101.254的路由器。
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         10.236.101.254  0.0.0.0         UG    0      0        0 eth0


配置静态路由

route 命令

设置和查看路由表都可以用 route 命令,设置内核路由表的命令格式是:

# route  [add|del] [-net|-host] target [netmask Nm] [gw Gw] [[dev] If]

其中:

add : 添加一条路由规则

del : 删除一条路由规则

-net : 目的地址是一个网络

-host : 目的地址是一个主机

target : 目的网络或主机

netmask : 目的地址的网络掩码

gw : 路由数据包通过的网关

dev : 为路由指定的网络接口

route 命令使用举例

添加到主机的路由

# route add -host 10.236.202.116 dev eth0 

# route add -host 10.236.202.116 gw 10.236.101.254     #添加到10.236.101.254的网关

添加到网络的路由

# route add -net 20.236.50.0/24 gw 10.236.23.251

# route add -net 20.236.50.0 netmask 255.255.255.0 eth0   #添加20.236.50.0的网络

# route add -net 10.236.202.0 netmask 255.255.255.0 gw 10.236.101.254 #添加10.236.202.0的网络

# route add -net 192.168.1.0/24 eth1

添加默认路由

# route add default gw 10.236.101.254

删除路由

# route del -host 192.168.1.2 dev eth0:0

# route del -host 10.236.202.116

# route del -host 10.20.30.148 gw 10.20.30.40

# route del -net 20.236.50.0/24 eth0

# route del -net 20.236.50.0/24

# route del -net 10.20.30.40 netmask 255.255.255.248 eth0

# route del -net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41

# route del -net 192.168.1.0/24 eth1

# route del default gw 192.168.1.1

在linux下设置永久路由的方法:    

    

  1.在/etc/rc.local里添加    

    

  方法:        

route add -net 10.236.51.0/24 gw 10.236.23.251

route add -net 10.236.202.0/24 gw 10.236.23.251

route add -net 10.236.95.0/24 gw 10.236.23.251

route add -net 10.236.6.0/24 gw 10.236.23.251

route add -net 10.236.53.0/24 gw 10.236.23.251

route add -net 10.236.46.0/24 gw 10.236.23.251

    

  2.在/etc/sysconfig/network里添加到末尾    

    

  方法:GATEWAY=gw-ip 或者 GATEWAY=gw-dev    

    

  3./etc/sysconfig/static-router :    

    

  any net x.x.x.x/24 gw y.y.y.y    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: