CentOS 6.9永久设置静态路由表以及路由表常用设置

一、路由表常用设置:

1、使用route命令添加的路由,机器重启或者网卡重启后路由就失效了,方法:

//添加到主机的路由
# route add –host 192.168.1.11 dev eth0
# route add –host 192.168.1.12 gw 192.168.1.1

//添加到网络的路由
# route add –net 192.168.1.11 netmask 255.255.255.0 dev eth0
# route add –net 192.168.1.11 netmask 255.255.255.0 gw 192.168.1.1
# route add –net 192.168.1.0/24 dev eth1

//添加默认网关
# route add default gw 192.168.2.1

//删除路由
# route del –host 192.168.1.11 dev eth0

2、还可以使用ip命令来添加、删除路由

//添加
ip route add default via 172.16.10.2 dev eth0 ip route add 172.16.1.0/24 via 172.16.10.2 dev eth0

3、查询

# netstat -nr
# route -n
# ip route list
# ip route show

二、永久设置路由表:

方法一:在/etc/sysconfig/network配置文件中配置(我测试了好像不太可行):

default via 192.168.3.1 dev eth0 #192.168.3.1为eth0网卡的网关地址
10.211.6.0/24 via 192.168.3.1 dev eth0
10.0.0.0/8 via 10.212.52.1 dev eth1 #10.212.52.1为eth1网卡的网关地址

注:该种配置写法同样支持写到/etc/sysconfig/network-scripts/route-interferface配置文件中。

具体可以参看redhat官方文档

方法二:在/etc/sysconfig/network-scripts/route-{interferface}配置文件配置({interferface}为网卡接口,如eth0)

在这里支持两种配置格式的写法

A:方法1中提到的方法

# cat /etc/sysconfig/network-scripts/route-eth0
0.0.0.0/0 via 192.168.3.1 dev eth0
10.211.6.0/24 via 192.168.3.1 dev eth0
# cat /etc/sysconfig/network-scripts/route-eth1
10.0.0.0/8 via 10.212.52.1 dev eth1

B:网络掩码法(投机取巧,走漏洞)

# cat /etc/sysconfig/network-scripts/route-eth0
ADDRESS0=0.0.0.0
NETMASK0=0.0.0.0
GATEWAY0=192.168.3.1
ADDRESS1=10.211.6.0
NETMASK1=255.255.255.0
GATEWAY1=192.168.3.1

其中网段地址和掩码全是0代表为所有网段,即默认路由。 

# cat /etc/sysconfig/network-scripts/route-eth1
ADDRESS0=10.0.0.0
NETMASK0=255.0.0.0
GATEWAY0=10.212.52.1

网络掩码法也可以参看redhat官方文档

方法三:/etc/sysconfig/static-routes配置(推荐)

# cat /etc/sysconfig/static-route
any net any gw 192.168.3.1
any net 10.211.6.0/24 gw 192.168.3.1
any net 10.0.0.0 netmask 255.0.0.0 gw 10.212.52.1

注:默认情况下主机中并没有该文件,需要手动创建。net是范围,host可以单独指定某一台机器。

之所以该方法也可以是因为/etc/init.d/network启动脚本会调用该文件,具体调用部分代码原理如下:

# Add non interface-specific static-routes.
if [ -f /etc/sysconfig/static-routes ]; then
  grep "^any" /etc/sysconfig/static-routes | while read ignore args ; do
    /sbin/route add -$args
  done
fi

方法四:写在开机脚本/etc/rc.local

route add -net 10.8.0.0 255.255.255.0 gw 192.168.199.2

 

参考:

http://www.361way.com/linux-define-static-route/4053.html(以上内容转自此篇文章)

http://dev.dafan.info/detail/221534

http://www.jb51.net/article/101683.htm

https://segmentfault.com/a/1190000004165066

http://network.chinabyte.com/194/11200694.shtml

posted @ 2018-02-07 19:14  EasonJim  阅读(11999)  评论(0编辑  收藏  举报