(Linux系统之静态路由配置)
一、环境介绍
server1:192.168.32.77/24 192.168.32.254 ——使用node1节点 server2: 192.168.50.16/24 192.168.50.254 ——使用node3节点 route:192.168.32.254 192.168.50.254 ——使用node2节点
二、检查各节点IP情况
1.node1节点
[root@node1 ~]# ifconfig ens256
ens256: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.32.77 netmask 255.255.255.0 broadcast 192.168.32.255
inet6 fe80::98f4:d12d:141:ed83 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:4e:fd:c6 txqueuelen 1000 (Ethernet)
RX packets 424 bytes 32744 (31.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 152 bytes 10588 (10.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@node1 ~]#
2.node2节点
[root@node2 ~]# ifconfig ens224 && ifconfig ens256
ens224: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.32.254 netmask 255.255.255.0 broadcast 192.168.32.255
inet6 fe80::c22:ee4e:848d:a53e prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:9c:b9:83 txqueuelen 1000 (Ethernet)
RX packets 581 bytes 43388 (42.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 188 bytes 12603 (12.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens256: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.50.254 netmask 255.255.255.0 broadcast 192.168.50.255
inet6 fe80::a25f:22cb:322a:b6f prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:9c:b9:8d txqueuelen 1000 (Ethernet)
RX packets 525 bytes 39245 (38.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 168 bytes 11212 (10.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
3.node3节点
[root@node3 ~]# ifconfig ens256
ens256: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.50.16 netmask 255.255.255.0 broadcast 192.168.50.255
inet6 fe80::c675:411b:2e90:34d6 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:fd:e3:aa txqueuelen 1000 (Ethernet)
RX packets 27 bytes 2838 (2.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 46 bytes 6658 (6.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
二、在route角色的节点上开启包转发
[root@node2 ~]# cd /proc/sys/net/ipv4/
[root@node2 ipv4]# echo 1 > /proc/sys/net/ipv4/ip_forward
三、测试当前server1和server2互通情况
[root@node1 ~]# ping 192.168.50.16 -c2
PING 192.168.50.16 (192.168.50.16) 56(84) bytes of data.
--- 192.168.50.16 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 22ms
四、在node1节点添加静态路由
[root@node1 ~]# route add -net 192.168.50.0/24 gw 192.168.32.254
[root@node1 ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default _gateway 0.0.0.0 UG 100 0 0 ens160
default _gateway 0.0.0.0 UG 101 0 0 ens161
default _gateway 0.0.0.0 UG 102 0 0 ens224
default _gateway 0.0.0.0 UG 103 0 0 ens256
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
192.168.3.0 0.0.0.0 255.255.255.0 U 101 0 0 ens161
192.168.8.0 0.0.0.0 255.255.255.0 U 102 0 0 ens224
192.168.8.0 0.0.0.0 255.255.255.0 U 102 0 0 ens224
192.168.32.0 0.0.0.0 255.255.255.0 U 103 0 0 ens256
192.168.50.0 _gateway 255.255.255.0 UG 0 0 0 ens256
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
192.168.200.0 0.0.0.0 255.255.255.0 U 100 0 0 ens160
五、在node3节点配置静态路由
[root@node3 ~]# route add -net 192.168.32.0/24 gw 192.168.50.254
[root@node3 ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.200.2 0.0.0.0 UG 100 0 0 ens160
default 192.168.50.254 0.0.0.0 UG 102 0 0 ens256
192.168.32.0 192.168.50.254 255.255.255.0 UG 0 0 0 ens256
192.168.50.0 0.0.0.0 255.255.255.0 U 102 0 0 ens256
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
192.168.200.0 0.0.0.0 255.255.255.0 U 100 0 0 ens160
192.168.243.0 0.0.0.0 255.255.255.0 U 101 0 0 ens224
[root@node3 ~]#
六、在node1节点和node3节点互ping
[root@node1 ~]# ping 192.168.50.16 -c2
PING 192.168.50.16 (192.168.50.16) 56(84) bytes of data.
64 bytes from 192.168.50.16: icmp_seq=1 ttl=63 time=0.567 ms
64 bytes from 192.168.50.16: icmp_seq=2 ttl=63 time=0.305 ms
--- 192.168.50.16 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 56ms
rtt min/avg/max/mdev = 0.305/0.436/0.567/0.131 ms
[root@node3 ~]# ping 192.168.32.77 -c4
PING 192.168.32.77 (192.168.32.77) 56(84) bytes of data.
64 bytes from 192.168.32.77: icmp_seq=1 ttl=63 time=0.466 ms
64 bytes from 192.168.32.77: icmp_seq=2 ttl=63 time=0.277 ms
64 bytes from 192.168.32.77: icmp_seq=3 ttl=63 time=0.513 ms
64 bytes from 192.168.32.77: icmp_seq=4 ttl=63 time=0.299 ms
--- 192.168.32.77 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 103ms
rtt min/avg/max/mdev = 0.277/0.388/0.513/0.105 ms
七、使静态路由永久生效——centos7
1.删除临时添加的静态路由
[root@node1 ~]# route del -net 192.168.50.0/24
[root@node1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.200.2 0.0.0.0 UG 100 0 0 ens160
0.0.0.0 192.168.3.1 0.0.0.0 UG 101 0 0 ens161
0.0.0.0 192.168.8.1 0.0.0.0 UG 102 0 0 ens224
0.0.0.0 192.168.32.254 0.0.0.0 UG 103 0 0 ens256
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
192.168.3.0 0.0.0.0 255.255.255.0 U 101 0 0 ens161
192.168.8.0 0.0.0.0 255.255.255.0 U 102 0 0 ens224
192.168.8.0 0.0.0.0 255.255.255.0 U 102 0 0 ens224
192.168.32.0 0.0.0.0 255.255.255.0 U 103 0 0 ens256
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
192.168.200.0 0.0.0.0 255.255.255.0 U 100 0 0 ens160
2.在网络接口目录配置静态路由文件
[root@node1 ~]# cd /etc/
[root@node1 ~]# cd /etc/sysconfig/network-scripts/
[root@node1 network-scripts]# ls
ifcfg-ens160 ifcfg-ens161-sp ifcfg-ens224-con224 ifcfg-ens224-con256
[root@node1 network-scripts]# vim route-ens256
Failed to search for file: /mnt/cdrom/BaseOS was not found
[root@node1 network-scripts]# cat route-ens256
192.168.50.0/24 via 192.168.32.254
3.重启网络服务
systemctl restart network
4.查看路由
route -n
八、使静态路由永久生效——centos8
1.写入网络配置文件
[root@node1 ~]# cd /etc/
[root@node1 ~]# cd /etc/sysconfig/network-scripts/
[root@node1 network-scripts]# ls
ifcfg-ens160 ifcfg-ens161-sp ifcfg-ens224-con224 ifcfg-ens224-con256
[root@node1 network-scripts]# vim route-ens256
Failed to search for file: /mnt/cdrom/BaseOS was not found
[root@node1 network-scripts]# cat route-ens256
192.168.50.0/24 via 192.168.32.254
2.重启网卡
nmcli con reload
nmcli con up ens224-con256
3.查看路由
ip route show