资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Linux添加永久静态路由信息

因为linux服务器有多网卡,需要有些网卡走指定的路由,就需要单独设置静态路由。
通过route add添加的静态路由,如果服务器重启或者是网卡重启,这个静态路由就会丢失。
[root@test ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.123.0   0.0.0.0         255.255.255.0   U     0      0        0 eth2
10.0.0.0        0.0.0.0         255.255.0.0     U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth2
0.0.0.0         192.168.123.254 0.0.0.0         UG    0      0        0 eth2

用route add添加静态路由。
[root@test ~]# route add -net 10.0.0.0 netmask 255.0.0.0 dev eth0
[root@test ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.123.0   0.0.0.0         255.255.255.0   U     0      0        0 eth2
10.0.0.0        0.0.0.0         255.255.0.0     U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth2
10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 eth0
0.0.0.0         192.168.123.254 0.0.0.0         UG    0      0        0 eth2

添加成功,但是重启网络服务后路由丢失,证明这个路由是动态参数。
[root@test ~]# service network restart
Shutting down interface eth0:                              [  OK  ]
Shutting down interface eth2:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:                                [  OK  ]
Bringing up interface eth2:                                [  OK  ]
[root@test ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.123.0   0.0.0.0         255.255.255.0   U     0      0        0 eth2
10.0.0.0        0.0.0.0         255.255.0.0     U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth2
0.0.0.0         192.168.123.254 0.0.0.0         UG    0      0        0 eth2

需要永久添加静态路由:
到/etc/sysconfig/network-scripts/下面
[root@test ~]# cd /etc/sysconfig/network-scripts/
加入一条静态路由到route-eth0,让网络服务每次重启都会自动加载这些信息。保证路由不丢失。
[root@shwmsdb1 network-scripts]# cat route-eth0
10.0.0.0/8  via  10.0.0.254

[root@test ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.123.0   0.0.0.0         255.255.255.0   U     0      0        0 eth2
10.0.0.0        0.0.0.0         255.255.0.0     U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth2
10.0.0.0        10.0.0.254      255.0.0.0       UG    0      0        0 eth0
0.0.0.0         192.168.123.254 0.0.0.0         UG    0      0        0 eth2

这样无论是重启主机还是重启网络服务路由信息都不会丢了。


文章名称:Linux添加永久静态路由信息
地址分享:http://cdkjz.cn/article/piehhi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220