Linux系统之静态路由配置

简介: 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
相关文章
|
1月前
|
Ubuntu Linux Anolis
Linux系统禁用swap
本文介绍了在新版本Linux系统(如Ubuntu 20.04+、CentOS Stream、openEuler等)中禁用swap的两种方法。传统通过注释/etc/fstab中swap行的方式已失效,现需使用systemd管理swap.target服务或在/etc/fstab中添加noauto参数实现禁用。方法1通过屏蔽swap.target适用于新版系统,方法2通过修改fstab挂载选项更通用,兼容所有系统。
136 3
Linux系统禁用swap
|
1月前
|
Linux
Linux系统修改网卡名为eth0、eth1
在Linux系统中,可通过修改GRUB配置和创建Udev规则或使用systemd链接文件,将网卡名改为`eth0`、`eth1`等传统命名方式,适用于多种发行版并支持多网卡配置。
184 3
|
2月前
|
Ubuntu Linux
计算机基础知识:linux系统怎么安装?
在虚拟机软件中创建一个新的虚拟机,并选择相应操作系统类型和硬盘空间大小等参数。将下载的 ISO 镜像文件加载到虚拟机中。启动虚拟机,进入安装界面,并按照步骤进行安装。安装完成后,可以在虚拟机中使用 Linux 系统。
|
2月前
|
存储 Ubuntu Linux
「正点原子Linux连载」第二章Ubuntu系统入门
在图2.8.2.4中,我们使用命令umount卸载了U盘,卸载以后当我们再去访问文件夹/mnt/tmp的时候发现里面没有任何文件了,说明我们卸载成功了。
|
Ubuntu Linux 网络安全
Linux系统初始化脚本
一款支持Rocky、CentOS、Ubuntu、Debian、openEuler等主流Linux发行版的系统初始化Shell脚本,涵盖网络配置、主机名设置、镜像源更换、安全加固等多项功能,适配单/双网卡环境,支持UEFI引导,提供多版本下载与持续更新。
155 0
Linux系统初始化脚本
|
2月前
|
运维 Linux 开发者
Linux系统中使用Python的ping3库进行网络连通性测试
以上步骤展示了如何利用 Python 的 `ping3` 库来检测网络连通性,并且提供了基本错误处理方法以确保程序能够优雅地处理各种意外情形。通过简洁明快、易读易懂、实操性强等特点使得该方法非常适合开发者或系统管理员快速集成至自动化工具链之内进行日常运维任务之需求满足。
153 18
|
1月前
|
安全 Linux Shell
Linux系统提权方式全面总结:从基础到高级攻防技术
本文全面总结Linux系统提权技术,涵盖权限体系、配置错误、漏洞利用、密码攻击等方法,帮助安全研究人员掌握攻防技术,提升系统防护能力。
150 1
|
1月前
|
监控 安全 Linux
Linux系统提权之计划任务(Cron Jobs)提权
在Linux系统中,计划任务(Cron Jobs)常用于定时执行脚本或命令。若配置不当,攻击者可利用其提权至root权限。常见漏洞包括可写的Cron脚本、目录、通配符注入及PATH变量劫持。攻击者通过修改脚本、创建恶意任务或注入命令实现提权。系统管理员应遵循最小权限原则、使用绝对路径、避免通配符、设置安全PATH并定期审计,以防范此类攻击。
616 1
|
2月前
|
缓存 监控 Linux
Linux系统清理缓存(buff/cache)的有效方法。
总结而言,在大多数情形下你不必担心Linux中buffer与cache占用过多内存在影响到其他程序运行;因为当程序请求更多内存在没有足够可用资源时,Linux会自行调整其占有量。只有当你明确知道当前环境与需求并希望立即回收这部分资源给即将运行重负载任务之前才考虑上述方法去主动干预。
970 10
|
2月前
|
安全 Linux 数据安全/隐私保护
为Linux系统的普通账户授予sudo访问权限的过程
完成上述步骤后,你提升的用户就能够使用 `sudo`命令来执行管理员级别的操作,而无需切换到root用户。这是一种更加安全和便捷的权限管理方式,因为它能够留下完整的权限使用记录,并以最小权限的方式工作。需要注意的是,随意授予sudo权限可能会使系统暴露在风险之中,尤其是在用户不了解其所执行命令可能带来的后果的情况下。所以在配置sudo权限时,必须谨慎行事。
395 0