网络基础-路由选择
路由
概述
- 路由:路由器从一个接口接收数据包并转发到另一个接口的过程
- 路由表:记录路由信息系统,可更改,定义了数据包从哪里接收并发送到何处
查看路由表
[root@server1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.226.2 0.0.0.0 UG 100 0 0 ens33
192.168.226.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
参数解析:
Destination:目标地址
Gateway:网关,显示0.0.0.0表示该路由是从本机发出的
Genmask:子网掩码
Flags:路由标记
U:up运行
G:表示网关是一个路由器
H:表示网关是一个主机
!:表示当前路由已被禁止
Iface:网卡
按上面的路由表来看,如果我ping一个公网IP,应该怎么走?
判断是否为本机ip
- 是:访问本机
- 否:查看路由,是否含有目标ip
- 是:从含有目标ip的路由指定的网卡出去
- 否:是否能匹配默认路由(网关)
- 是:从默认路由指定的网卡出去
- 否:报错,网络不可达Network is unreachable
增删网关
[root@server1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.226.2 0.0.0.0 UG 100 0 0 ens33
192.168.226.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
删除网关(临时生效)
[root@server1 ~]# route del default gw 192.168.226.2
[root@server1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.226.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
增加网关(临时生效)
[root@server1 ~]# route add default gw 192.168.100.2
SIOCADDRT: 网络不可达
报错:SIOCADDRT: 网络不可达
原因: 添加网关只能从现有的路由条目中选择一个ip作为网关
解决:添加路由192.168.100.0(命令:route add -net 192.168.100.0/24 dev ens33)
[root@server1 ~]# route add -net 192.168.100.0/24 dev ens33
[root@server1 ~]# route add default gw 192.168.100.2
[root@server1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.100.2 0.0.0.0 UG 0 0 0 ens33
192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
192.168.226.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
想要设置的网关永久生效,需要修改配置文件/etc/sysconfig/network-scripts/ifcfg-ens33
GATEWAY=x.x.x.x
增删路由
[root@server1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.226.2 0.0.0.0 UG 0 0 0 ens33
192.168.226.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
增加路由
[root@server1 ~]# route add -net 192.168.100.0/24 dev ens33
[root@server1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.226.2 0.0.0.0 UG 0 0 0 ens33
192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
192.168.226.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
删除路由
[root@server1 ~]# route del -net 192.168.100.0/24
[root@server1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.226.2 0.0.0.0 UG 0 0 0 ens33
192.168.226.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
搭建路由选择实验
三台主机的网络模式都设置为仅主机模式,这时就不能远程登录了
建议提前下载好net-tools(提供route,ifconfig等命令)软件包,仅主机模式下不能访问外网,不能yum安装
实验完成后记得将网络模式改为NAT模式
主机名 | IP地址 | 角色 |
---|---|---|
r1 | 192.168.12.1 | node1 |
r2 | 192.168.123.2 | router |
r3 | 192.168.23.3 | node2 |
修改主机名
[root@server1 ~]# hostnamectl set-hostname r1
[root@server1 ~]# su
[root@r1 ~]#
[root@server2 ~]# hostnamectl set-hostname r2
[root@server2 ~]# su
[root@r2 ~]#
[root@server3 ~]# hostnamectl set-hostname r3
[root@server3 ~]# su
[root@r3 ~]#
修改ip地址,删除网关
[root@r1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
[root@r1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.12.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
[root@r2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
[root@r2 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.123.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
[root@r3 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
[root@r3 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.23.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
r2开启路由转发功能
[root@r2 ~]# cat /proc/sys/net/ipv4/ip_forward
0
[root@r2 ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
[root@r2 ~]# cat /proc/sys/net/ipv4/ip_forward
1
#永久开启需要修改配置文件/etc/sysctl.conf
[root@r2 ~]# sed -i 's/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/' /proc/sys/net/ipv4/ip_forward
分别添加到r1和r3两台主机的路由
[root@r2 ~]# route add -net 192.168.12.0/24 dev ens33
[root@r2 ~]# route add -net 192.168.23.0/24 dev ens33
[root@server1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.12.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
192.168.23.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
192.168.123.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
分别配置r1和r3的网关
[root@r1 ~]# route add -net 192.168.123.0/24 dev ens33
[root@r1 ~]# route add default gw 192.168.123.0
[root@r2 ~]# route add -net 192.168.123.0/24 dev ens33
[root@r2 ~]# route add default gw 192.168.123.0
测试
[root@r1 ~]# ping 192.168.123.2
PING 192.168.123.2 (192.168.123.2) 56(84) bytes of data.
64 bytes from 192.168.123.2: icmp_seq=1 ttl=64 time=0.449 ms
64 bytes from 192.168.123.2: icmp_seq=2 ttl=64 time=1.27 ms
64 bytes from 192.168.123.2: icmp_seq=3 ttl=64 time=1.25 ms
64 bytes from 192.168.123.2: icmp_seq=4 ttl=64 time=1.24 ms
^C
--- 192.168.123.2 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3022ms
rtt min/avg/max/mdev = 0.449/1.055/1.272/0.351 ms
[root@r3 ~]# ping 192.168.12.1
PING 192.168.12.1 (192.168.12.1) 56(84) bytes of data.
64 bytes from 192.168.12.1: icmp_seq=1 ttl=64 time=0.624 ms
64 bytes from 192.168.12.1: icmp_seq=2 ttl=64 time=1.00 ms
64 bytes from 192.168.12.1: icmp_seq=3 ttl=64 time=1.05 ms
^C
--- 192.168.12.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2022ms
rtt min/avg/max/mdev = 0.624/0.896/1.059/0.195 ms