Linux系统之静态路由配置

举报
江湖有缘 发表于 2023/01/15 10:29:49 2023/01/15
【摘要】 Linux系统之静态路由配置

@TOC

一、环境介绍

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
【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。