记录一些自己的心得以及方法,本着共同学习,互相进步为目的。本次总结了Linux主机路由的配置,欢迎大家评论留言,主机网络对接调测时,有时候需要进行主机路由配置,实现与某台主机或者某段网段互通(以下是主机路由配置,均在试验机上操作,并且在实际工程环境中有验证过,目前配置过的OS有Redhat6.8、Redhat6.9、centos7.7)。
一、主机环境:
1、系统版本:
root@mysql1:/root>cat /etc/redhat-releaseRed Hat Enterprise Linux Server release 6.9 (Santiago)
2、主机IP配置情况:
root@mysql1:/root>hostname -I192.168.100.11 192.168.100.110 10.10.10.110root@mysql1:/root>
所以,本次操作主机配置了3个IP地地址,网口对应情况如下:
(1)eth0:192.168.100.11(2)bond0:192.168.100.110(3)bond1:10.10.10.110
3、查看路由表命令:
netstat -rn 或者 route -n
4、route命令的语法:
关于route命令的语法这里就不多说了,有兴趣的朋友可以自行在命令行界面执行man route查看。
一、操作
一)默认路由:
1、添加临时默认路由:
root@mysql1:/>route add default gw 192.168.100.1 dev bond0
查看结果如下:
root@mysql1:/root>netstat -rnKernel IP routing tableDestination Gateway Genmask Flags MSS Window irtt Iface192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 bond0192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond110.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 bond10.0.0.0 192.168.100.1 0.0.0.0 UG 0 0 0 bond0root@mysql1:/root>
2、以上操作重启网络或者重启主机后就会失效,那么如何让主机重启或者网络重启后自动加载呢?这时候就需要写入配置让主机自动加载,操作如下:
root@mysql1:/root>cd /etc/sysconfig/network-scripts/root@mysql1:/etc/sysconfig/network-scripts>cp ifcfg-bond0 ifcfg-bond0.bakroot@mysql1:/etc/sysconfig/network-scripts>echo -e \'GATEWAY=192.168.100.1\' >>ifcfg-bond0root@mysql1:/etc/sysconfig/network-scripts>
3、删除默认路由,操作命令如下:
root@mysql1:/>route del default gw 192.168.100.1 dev bond0
查看结果如下:
root@mysql1:/root>netstat -rnKernel IP routing tableDestination Gateway Genmask Flags MSS Window irtt Iface192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 bond0192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond110.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 bond1root@mysql1:/root>
4、重启网络验证如下(重启主机也正常):
root@mysql1:/root>service network restartShutting down interface bond0: [ OK ]Shutting down interface bond1: [ OK ]Shutting down loopback interface: [ OK ]Bringing up loopback interface: [ OK ]Bringing up interface bond0: Determining if ip address 192.168.100.110 is already in use for device bond0...[ OK ]Bringing up interface bond1: Determining if ip address 10.10.10.110 is already in use for device bond1...[ OK ]Bringing up interface eth0: Determining if ip address 192.168.100.11 is already in use for device eth0...[ OK ]root@mysql1:/root>netstat -rnKernel IP routing tableDestination Gateway Genmask Flags MSS Window irtt Iface192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 bond0192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond110.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 bond10.0.0.0 192.168.100.1 0.0.0.0 UG 0 0 0 bond0root@mysql1:/root>
主机默认路由一般只有一个,建议默认路由走内网,当然也可以不存在,其他路由我们可以通过配置详细路由,具体请往后看。
二)指定主机路由
1、临时添加一台主机的路由,比如:10.10.110.11,操作如下:
root@mysql1:/root>route add -host 10.10.110.11 gw 10.10.10.1 dev bond1
查看结果如下:
root@mysql1:/root>netstat -rnKernel IP routing tableDestination Gateway Genmask Flags MSS Window irtt Iface10.10.110.11 10.10.10.1 255.255.255.255 UGH 0 0 0 bond1192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 bond0192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond110.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 bond10.0.0.0 192.168.100.1 0.0.0.0 UG 0 0 0 bond0root@mysql1:/root>
2、以上操作重启网络或者重启主机后就会失效,那么如何让主机重启或者网络重启后自动加载呢?这时候就需要写入配置让主机自动加载,操作如下:
root@mysql1:/etc/sysconfig/network-scripts>cat >> route-bond110.10.110.11/32 via 10.10.10.1^Croot@mysql1:/etc/sysconfig/network-scripts>ls -ltr route-bond1-rw-r--r-- 1 root root 31 May 12 22:22 route-bond1root@mysql1:/etc/sysconfig/network-scripts>
或者:
root@mysql1:/etc/sysconfig/network-scripts>cat >>route-bond1ADDRESS0=10.10.110.11NETMASK0=255.255.255.255GATEWAY0=10.10.10.1^Croot@mysql1:/etc/sysconfig/network-scripts>
如果有多条路由走的是bond1,则继续添加即可,如:
ADDRESS1=xx.xx.xx.xxNETMASK1=xx.xx.xx.xxGATEWAY1=xx.xx.xx.xx
3、删除主机路由,操作命令如下:
root@mysql1:/etc/sysconfig/network-scripts>route del -host 10.10.110.11 gw 10.10.10.1 dev bond1root@mysql1:/etc/sysconfig/network-scripts>
以上两种方法都可以进行操作,看个人喜欢。
查看结果如下:
root@mysql1:/etc/sysconfig/network-scripts>netstat -rnKernel IP routing tableDestination Gateway Genmask Flags MSS Window irtt Iface192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 bond0192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond110.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 bond10.0.0.0 192.168.100.1 0.0.0.0 UG 0 0 0 bond0root@mysql1:/etc/sysconfig/network-scripts>
4、重启网络验证如下:
root@mysql1:/etc/sysconfig/network-scripts>netstat -rnKernel IP routing tableDestination Gateway Genmask Flags MSS Window irtt Iface10.10.110.11 10.10.10.1 255.255.255.255 UGH 0 0 0 bond1192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 bond0192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond110.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 bond10.0.0.0 192.168.100.1 0.0.0.0 UG 0 0 0 bond0root@mysql1:/etc/sysconfig/network-scripts>service network restartShutting down interface bond0: [ OK ]Shutting down interface bond1: [ OK ]Shutting down loopback interface: [ OK ]Bringing up loopback interface: [ OK ]Bringing up interface bond0: Determining if ip address 192.168.100.110 is already in use for device bond0...[ OK ]Bringing up interface bond1: Determining if ip address 10.10.10.110 is already in use for device bond1...[ OK ]Bringing up interface eth0: Determining if ip address 192.168.100.11 is already in use for device eth0...[ OK ]root@mysql1:/etc/sysconfig/network-scripts>netstat -rnKernel IP routing tableDestination Gateway Genmask Flags MSS Window irtt Iface10.10.110.11 10.10.10.1 255.255.255.255 UGH 0 0 0 bond1192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 bond0192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond110.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 bond10.0.0.0 192.168.100.1 0.0.0.0 UG 0 0 0 bond0root@mysql1:/etc/sysconfig/network-scripts>
添加一个网段的路由与添加主机路由操作是相同的,网段路由的添加请继续往下看。
三)指定网段路由
1、临时添加一个网段的路由,比如:172.16.0.0/16,操作如下:
root@mysql1:/root>route add -net 172.16.0.0 netmask 255.255.0.0 gw 192.168.100.1 dev bond0root@mysql1:/root>
查看结果如下:
root@mysql1:/root>netstat -rnKernel IP routing tableDestination Gateway Genmask Flags MSS Window irtt Iface10.10.110.11 10.10.10.1 255.255.255.255 UGH 0 0 0 bond1192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 bond0192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0172.16.0.0 192.168.100.1 255.255.0.0 UG 0 0 0 bond0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond110.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 bond10.0.0.0 192.168.100.1 0.0.0.0 UG 0 0 0 bond0root@mysql1:/root>
2、以上操作重启网络或者重启主机后就会失效,那么如何让主机重启或者网络重启后自动加载呢?这时候就需要写入配置让主机自动加载,操作如下:
root@mysql1:/etc/sysconfig/network-scripts>cat >> route-bond0172.16.0.0/16 via 192.168.100.1^Croot@mysql1:/etc/sysconfig/network-scripts>ls -ltr route-bond0-rw-r--r-- 1 root root 32 May 12 22:54 route-bond0root@mysql1:/etc/sysconfig/network-scripts>
或者:
root@mysql1:/etc/sysconfig/network-scripts>>route-bond0root@mysql1:/etc/sysconfig/network-scripts>cat >>route-bond0ADDRESS0=172.16.0.0NETMASK0=255.255.255.255GATEWAY0=192.168.100.1^Croot@mysql1:/etc/sysconfig/network-scripts>
如果有多条路由走的是bond1,则继续添加即可,如:
ADDRESS1=xx.xx.xx.xxNETMASK1=xx.xx.xx.xxGATEWAY1=xx.xx.xx.xx
3、删除网段路由,操作命令如下:
root@mysql1:/etc/sysconfig/network-scripts>route del -net 172.16.0.0 netmask 255.255.0.0 gw 192.168.100.1 dev bond0root@mysql1:/etc/sysconfig/network-scripts>netstat -rnKernel IP routing tableDestination Gateway Genmask Flags MSS Window irtt Iface10.10.110.11 10.10.10.1 255.255.255.255 UGH 0 0 0 bond1192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 bond0192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond110.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 bond10.0.0.0 192.168.100.1 0.0.0.0 UG 0 0 0 bond0root@mysql1:/etc/sysconfig/network-scripts>
4、重启网卡验证如下:
root@mysql1:/etc/sysconfig/network-scripts>service network restartShutting down interface bond0: [ OK ]Shutting down interface bond1: [ OK ]Shutting down loopback interface: [ OK ]Bringing up loopback interface: [ OK ]Bringing up interface bond0: Determining if ip address 192.168.100.110 is already in use for device bond0...[ OK ]Bringing up interface bond1: Determining if ip address 10.10.10.110 is8000already in use for device bond1...[ OK ]Bringing up interface eth0: Determining if ip address 192.168.100.11 is already in use for device eth0...[ OK ]root@mysql1:/etc/sysconfig/network-scripts>netstat -rnKernel IP routing tableDestination Gateway Genmask Flags MSS Window irtt Iface10.10.110.11 10.10.10.1 255.255.255.255 UGH 0 0 0 bond1172.16.0.0 192.168.100.1 255.255.255.255 UGH 0 0 0 bond0192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 bond0192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond0169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 bond110.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 bond10.0.0.0 192.168.100.1 0.0.0.0 UG 0 0 0 bond0root@mysql1:/etc/sysconfig/network-scripts>
二、建议:
在网络的配置目录下,如果没有添加route-bondX路由,重启网络路由会缺失,在现网环境中,这会造成业务中断,影响正常业务;通常有人习惯把路由写进入rc.local,其实也可以,但是不建议这么操作,虽然主机重启的情况下,可以通过rc.local加载路由;如果添加route-bondX路由,无论主机或者网络重启都要去加载相关文件,可以减少路由缺失造成的麻烦。因此,建议将路由写入route-bondX文件。