Linux系统之Network静态路由配置

简介: Linux系统之Network静态路由配置

[Linux系统之Network静态路由配置]()

一、环境介绍

1.环境规划

hostname IP地址 网关 系统版本
server01 192.168.20.66 192.168.20.1 centos7.6
route01 192.168.20.254/24-172.16.10.254/24 centos7.6
server02 172.16.10.66 172.16.10.1 centos7.6

2.实验目的

server02与server01可以互通

二、检查各节点IP地址

1.检查server01服务器上

[root@server01 ~]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.20.66  netmask 255.255.255.0  broadcast 192.168.20.255
        inet6 fe80::2a6e:d4ff:fe89:5da4  prefixlen 64  scopeid 0x20<link>
        ether 28:6e:d4:89:5d:a4  txqueuelen 1000  (Ethernet)
        RX packets 6020  bytes 682748 (666.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 112  bytes 17061 (16.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 4  bytes 344 (344.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4  bytes 344 (344.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        
AI 代码解读

2.检查server02服务器网卡

[root@server02 ~]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.10.66  netmask 255.255.255.0  broadcast 172.16.10.255
        inet6 fe80::2a6e:d4ff:fe89:cc37  prefixlen 64  scopeid 0x20<link>
        ether 28:6e:d4:89:cc:37  txqueuelen 1000  (Ethernet)
        RX packets 6030  bytes 686378 (670.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 107  bytes 16548 (16.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 4  bytes 344 (344.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4  bytes 344 (344.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


AI 代码解读

3.检查route01上的网卡


[root@route01 ~]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.20.254  netmask 255.255.255.0  broadcast 192.168.20.255
        inet6 fe80::2a6e:d4ff:fe89:7954  prefixlen 64  scopeid 0x20<link>
        ether 28:6e:d4:89:79:54  txqueuelen 1000  (Ethernet)
        RX packets 5762  bytes 655197 (639.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 117  bytes 17132 (16.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.10.254  netmask 255.255.255.0  broadcast 172.16.10.255
        inet6 fe80::2a6e:d4ff:fe89:d1a0  prefixlen 64  scopeid 0x20<link>
        ether 28:6e:d4:89:d1:a0  txqueuelen 1000  (Ethernet)
        RX packets 3012  bytes 349027 (340.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 23  bytes 1499 (1.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 12  bytes 1040 (1.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12  bytes 1040 (1.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0



AI 代码解读

三、在route01上启动IP包转发

[root@route01 ~]# cd /proc/sys/net/ipv4/
[root@route01 ipv4]# echo 1 > /proc/sys/net/ipv4/ip_forward
[root@route01 ipv4]# 
AI 代码解读

四、查看当前两节点互通情况

1.查看server01和server02连通状态

image.png

image.png

2.查看当前server01路由表

[root@server01 ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    0      0        0 eth0
link-local      0.0.0.0         255.255.0.0     U     1002   0        0 eth0
192.168.20.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
AI 代码解读

3.查看当前server01路由表

[root@server02 ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    0      0        0 eth0
link-local      0.0.0.0         255.255.0.0     U     1002   0        0 eth0
172.16.10.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
AI 代码解读

五、在server01配置静态路由

1.添加静态路由

[root@server01 ~]# route add -net 172.16.10.0/24 gw 192.168.20.254
AI 代码解读

2.查看当前路由表

[root@server01 ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    0      0        0 eth0
link-local      0.0.0.0         255.255.0.0     U     1002   0        0 eth0
172.16.10.0     192.168.20.254  255.255.255.0   UG    0      0        0 eth0
192.168.20.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0

AI 代码解读

六、在server02配置静态路由

1.添加静态路由

[root@server02 ~]# route add -net 192.168.20.0/24 gw 172.16.10.254
AI 代码解读

2.查看当前路由表

[root@server02 ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    0      0        0 eth0
link-local      0.0.0.0         255.255.0.0     U     1002   0        0 eth0
172.16.10.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.20.0    172.16.10.254   255.255.255.0   UG    0      0        0 eth0
AI 代码解读

七、检查server01和server02连通情况

image.png

image.png

八、server01配置永久路由

1.在server01上删除临时静态路由

[root@server01 ~]# route delete -net 172.16.10.0/24
[root@server01 ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    0      0        0 eth0
link-local      0.0.0.0         255.255.0.0     U     1002   0        0 eth0
192.168.20.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
AI 代码解读

2.新增网卡配置文件写入路由

[root@server01 ~]# !73
cd /etc/sysconfig/network-scripts/
[root@server01 network-scripts]# vim route-eth0
[root@server01 network-scripts]# cat route-eth0 
172.16.10.0/24 via 192.168.20.254
AI 代码解读

3.重启网路服务

[root@server01 network-scripts]# service network restart 
Restarting network (via systemctl):                        [  OK  ]
AI 代码解读

4.查看路由状态

[root@server01 network-scripts]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    0      0        0 eth0
link-local      0.0.0.0         255.255.0.0     U     1002   0        0 eth0
172.16.10.0     192.168.20.254  255.255.255.0   UG    0      0        0 eth0
192.168.20.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
AI 代码解读

九、server02配置永久静态路由

1.删除临时静态路由

route delete -net 192.168.20.0/24    # 删除临时路由
cd /etc/sysconfig/network-scripts/    # 进入网卡配置文件目录
AI 代码解读

2.新增网卡配置文件写入路由

[root@server02 network-scripts]# vim route-eth1
[root@server02 network-scripts]# cat route-eth1
192.168.20.0/24 via 172.16.10.254
AI 代码解读

3.重启网络

[root@server02 network-scripts]# service network restart 
Restarting network (via systemctl):                        [  OK  ]
AI 代码解读

4.查看静态路由表

[root@server02 network-scripts]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 eth0
172.16.10.0     0.0.0.0         255.255.255.0   U     100    0        0 eth0
192.168.20.0    172.16.10.254   255.255.255.0   UG    100    0        0 eth0
AI 代码解读

十、测试server01与server02互通情况

image.png

image.png

目录
打赏
0
0
0
0
1263
分享
相关文章
Linux系统中的cd命令:目录切换技巧
踏过千山,越过万水,人生就是一场不断前行的旅程,总充满了未知与挑战。然而,“cd”命令如同你的旅伴,会带你穿梭在如棋盘一般的文件系统中,探索每一处未知。希望你能从“cd”命令中找到乐趣,像是掌控了一种络新妙的魔法,去向未知进发,开始你的探索之旅。
88 24
|
7天前
|
Linux系统下快速批量创建和删除文件的方法
总的来说,使用shell脚本来批量处理文件是一种非常强大的工具,只要你愿意花时间学习和实践,你会发现它能大大提高你的工作效率。
54 19
Linux系统之su命令的基本使用
Linux系统之su命令的基本使用
67 2
Linux系统之su命令的基本使用
在Ubuntu Linux系统下如何搭建并安装EDK2
以上就是在Ubuntu Linux系统下搭建并安装EDK2的过程。这个过程可能会有些复杂,但只要按照步骤一步步来,应该不会有太大问题。如果在过程中遇到任何问题,都可以在网上找到相应的解决方案。希望这个指南能对你有所帮助!
63 17
Linux系统资源管理:多角度查看内存使用情况。
要知道,透过内存管理的窗口,我们可以洞察到Linux系统运行的真实身姿,如同解剖学家透过微观镜,洞察生命的奥秘。记住,不要惧怕那些高深的命令和参数,他们只是你掌握系统"魔法棒"的钥匙,熟练掌握后,你就可以骄傲地说:Linux,我来了!
119 27
在Linux(CentOS和AWS)上安装更新的git2的方法并配置github-ssh
经过以上这些步骤,你现在就能在GitHub上顺利往返,如同海洋中的航海者自由驰骋。欢迎你加入码农的世界,享受这编程的乐趣吧!
50 10
|
28天前
|
linux命令详细说明以及案例
本文介绍了常用的 Linux 命令及其详细说明和示例,包括:`ls`(列出目录内容)、`cd`(更改目录)、`rm` 和 `mv`(删除与移动文件)、`grep`(搜索文本)、`cat`(显示文件内容)以及 `chmod`(更改文件权限)。每个命令均配有功能描述、选项说明及实际案例,帮助用户更好地掌握 Linux 命令的使用方法。
154 56
|
6天前
|
Linux命令的基本格式解析
总的来说,Linux命令的基本格式就像一个食谱,它可以指导你如何使用你的计算机。通过学习和实践,你可以成为一个真正的“计算机厨师”,创造出各种“美味”的命令。
44 15
调试技巧 - 用Linux命令排查Java问题
总的来说,使用Linux命令来排查Java问题,需要一定的实践经验和理论知识。然而,只要我们愿意花时间深入了解这些工具,我们就能够熟练地使用它们来分析和解决问题。此外,这些工具只是帮助我们定位问题,真正解决问题需要我们对Java和JVM有深入的理解,并能够读懂和分析代码。
59 13