linux's docs

2.1.1: 网络-静态路由及网关


1. 查看网关

1)固定IP时:

# 方法1
cat /etc/sysconfig/network-scripts/ifcfg-eth0 |grep GATEWAY
GATEWAY=172.16.2.1
# 方法2
route | grep default
default         172.16.2.1      0.0.0.0         UG    0      0        0 eth0

2)自动获取IP时:

route | grep default
default         172.16.2.1      0.0.0.0         UG    0      0        0 eth0

2. 静态路由

1) route命令

使用net-tools-1.60-110.el6_2.x86_64中的route命令

# 查看路由
route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.2.0        *               255.255.255.0   U     0      0        0 eth0
192.168.33.0    *               255.255.255.0   U     0      0        0 eth1
link-local      *               255.255.0.0     U     1002   0        0 eth0
link-local      *               255.255.0.0     U     1003   0        0 eth1
default         10.0.2.2        0.0.0.0         UG    0      0        0 eth0
# 参数:
# -n,解析hostname为ip显示
route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.2.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.33.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
0.0.0.0         10.0.2.2        0.0.0.0         UG    0      0        0 eth0

# 增加路由
route add -net 172.16.0.0/16 gw 10.0.2.2 dev eth0
# dev参数可有可无

# 删除路由
route del -net 172.16.0.0/16 gw 10.0.2.2 dev eth0

2) ip命令

使用iproute-2.6.32-54.el6.x86_64中的ip命令

# 查看路由
ip route
10.0.2.0/24 dev eth0  proto kernel  scope link  src 10.0.2.15
192.168.33.0/24 dev eth1  proto kernel  scope link  src 192.168.33.10
169.254.0.0/16 dev eth0  scope link  metric 1002
169.254.0.0/16 dev eth1  scope link  metric 1003
default via 10.0.2.2 dev eth0

# 增加路由
ip route add 172.16.0.0/16 via 10.0.2.2 dev eth0

# 修改路由

# 删除路由
ip route del 172.16.0.0/16 via 10.0.2.2 dev eth0

配置文件 /etc/sysconfig/network-scripts/route-ifname,ifname是网卡名称,例如eth0

default via 10.0.0.1 [dev eth0]
172.16.0.0/16 via 10.0.2.2

redhat static route doc

3. /etc/sysconfig/static-routes/etc/sysconfig/network-scripts/route-ifname区别

1) static-routes中的内容仅在系统启动,及network服务重启时加载

# 若ifdown掉某个网卡,重新ifup它,是不会重载static-routes的

# 创建static-routes文件,此文件默认不存在
echo 'any net 172.16.0.0/16 gw 10.0.2.2 eth0' > /etc/sysconfig/static-routes

# 重载network服务
service network restart

# 查看eth0的路由
ip route show dev eth0
10.0.2.0/24  proto kernel  scope link  src 10.0.2.15
172.16.0.0/16 via 10.0.2.2
169.254.0.0/16  scope link  metric 1002
default via 10.0.2.2

# down掉eth0,然后在up它
ifdown eth0
ifup eth0
# 发现写在static-routes文件中的路由不存在了
ip route show dev eth0
10.0.2.0/24  proto kernel  scope link  src 10.0.2.15
169.254.0.0/16  scope link  metric 1002
default via 10.0.2.2

2) route-ifname中的内容除了系统启动、重启network服务之外,在执行ifconfig命令时也会随着启动

# 创建route-eth0文件
echo "172.16.0.0/16 via 10.0.2.2" > /etc/sysconfig/network-scripts/route-eth0

# 重启network服务
service network restart
ip route show dev eth0
10.0.2.0/24  proto kernel  scope link  src 10.0.2.15
172.16.0.0/16 via 10.0.2.2
169.254.0.0/16  scope link  metric 1002
default via 10.0.2.2

# down掉eth0,然后在up它
ifdown eth0
ifup eth0
# 发现写在route-eth0文件中的路由又回来了
ip route show dev eth0
10.0.2.0/24  proto kernel  scope link  src 10.0.2.15
172.16.0.0/16 via 10.0.2.2
169.254.0.0/16  scope link  metric 1002
default via 10.0.2.2

Contents