实验:主机间跨网络通信

实验拓扑图:


实验:主机间跨网络通信_第1张图片

第一步:创建四个虚拟机

第二步:配置vmnet6和vmnet8网段

vmnet6:10.0.0.0/8

vmnet8:192.168.38.0/24

vmnet0:172.18.0.0/24

三、如上图所示,配置好每个虚拟机的网卡类型

四、配置IP

第一个虚拟机

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.38.123
NETMASK=255.255.255.0
GATEWAY=192.168.38.2
NAME=ens33
DEVICE=ens33
ONBOOT=yes
[root@localhost ~]# systemctl restart network

第二个虚拟机

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.38.200
NETMASK=255.255.255.0
GATEWAY=192.168.38.2
NAME=ens33
DEVICE=ens33
ONBOOT=yes
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens37
TYPE=Ethernet
BOOTPROTO=static
IPADDR=10.0.0.200
NETMASK=255.0.0.0
GATEWAY=10.0.0.1
NAME=ens37
DEVICE=ens37
ONBOOT=yes
[root@localhost ~]# systemctl restart network

第三个虚拟机

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
IPADDR=10.0.0.201
NETMASK=255.0.0.0
GATEWAY=10.0.0.1
NAME=ens33
DEVICE=ens33
ONBOOT=yes
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens37
TYPE=Ethernet
BOOTPROTO=static
IPADDR=172.18.28.201
NETMASK=255.255.0.0
GATEWAY=172.18.0.1
NAME=ens37
DEVICE=ens37
ONBOOT=yes
[root@localhost ~]# systemctl restart network

第四个虚拟机

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
IPADDR=172.18.28.202
NETMASK=255.255.0.0
GATEWAY=172.18.0.1
NAME=ens33
DEVICE=ens33
ONBOOT=yes
[root@localhost ~]# systemctl restart network

五、配置路由规则

现在B还ping不通A

[root@b ~]# ping 192.168.38.123
PING 192.168.38.123 (192.168.38.123) 56(84) bytes of data.
From 172.18.28.28 icmp_seq=1 Redirect Network(New nexthop: 172.18.0.1)
From 172.18.28.28: icmp_seq=1 Redirect Network(New nexthop: 172.18.0.1)

A的默认网关应该是192.168.38.200(或者给A加一条路由,让A知道172.18网段是要通过192.168.38.200出去的,不然数据包全发到192.168.38.2去了)

[root@localhost ~]# ip route add default via 192.168.38.200

R1走172.18网段通过10.0.0.201

[root@localhost ~]# ip route add 172.18.0.0/16 via 10.0.0.201

R2走192.168.38网段通过10.0.0.200

[root@r2 ~]# ip route add 192.168.38.0/24 via 10.0.0.200

B的默认网关应该是172.18.28.201

[root@b ~]# ip route add default via 172.18.28.201

六、第二三个虚拟机开启路由转发

第二个和第三个虚拟机都要执行

[root@localhost ~]# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.d/ip_forward.conf
[root@localhost ~]# sysctl -p /etc/sysctl.d/ip_forward.conf
net.ipv4.ip_forward = 1

七、接下来就是见证奇迹的时刻

B ping A

[root@b ~]# ping 192.168.38.123 -c3
PING 192.168.38.123 (192.168.38.123) 56(84) bytes of data.
64 bytes from 192.168.38.123: icmp_seq=1 ttl=62 time=4.83 ms
64 bytes from 192.168.38.123: icmp_seq=2 ttl=62 time=1.03 ms
64 bytes from 192.168.38.123: icmp_seq=3 ttl=62 time=1.12 ms

--- 192.168.38.123 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 1.034/2.329/4.832/1.770 ms

A ping B (第一次实验做完后NAT就可以直接ping到桥接了,不知道什么原因)

[root@localhost ~]# ping 172.18.28.202 -c3
PING 172.18.28.202 (172.18.28.202) 56(84) bytes of data.
64 bytes from 172.18.28.202: icmp_seq=1 ttl=62 time=2.20 ms
64 bytes from 172.18.28.202: icmp_seq=2 ttl=62 time=1.14 ms
64 bytes from 172.18.28.202: icmp_seq=3 ttl=62 time=0.876 ms

--- 172.18.28.202 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 0.876/1.408/2.202/0.573 ms

A到B怎么走的:

[root@localhost ~]# tracepath -n 172.18.28.202
 1?: [LOCALHOST]                                         pmtu 1500
 1:  192.168.38.200                                        0.558ms 
 1:  192.168.38.200                                        1.848ms 
 2:  10.0.0.201                                            2.290ms 
 3:  172.18.28.202                                         4.786ms reached
     Resume: pmtu 1500 hops 3 back 3 

B到A怎么走的

[root@b ~]# tracepath -n 192.168.38.123
 1?: [LOCALHOST]                                         pmtu 1500
 1:  172.18.28.201                                         0.887ms 
 1:  172.18.28.201                                         0.863ms 
 2:  10.0.0.200                                            5.798ms 
 3:  192.168.38.123                                        9.648ms reached
     Resume: pmtu 1500 hops 3 back 3 

现在虚拟机不知道怎么了默认NAT就能ping到桥接的虚拟机,现在tracepath看一下(这是新的一台虚拟机)

# 能ping通
[qqq@ubuntu ~]$ ping 172.18.28.202 -c2
PING 172.18.28.202 (172.18.28.202) 56(84) bytes of data.
64 bytes from 172.18.28.202: icmp_seq=1 ttl=128 time=2.15 ms
64 bytes from 172.18.28.202: icmp_seq=2 ttl=128 time=0.738 ms

--- 172.18.28.202 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 3ms
rtt min/avg/max/mdev = 0.738/1.442/2.146/0.704 ms
#看一下怎么走的
[qqq@ubuntu ~]$ tracepath -n 172.18.28.202
 1?: [LOCALHOST]                      pmtu 1500
 1:  192.168.38.2                                          0.480ms 
 1:  192.168.38.2                                          0.120ms 
 2:  no reply
 3:  no reply
 4:  no reply
 ...   (确实是到了NAT桥那里,后面一直noreply了)

持久化路由表

思路:可以把添加路由的命令写入开机启动文件

方法一:

写入rc.local文件

方法二(推荐):

# CentOS7可行,其他未测
[root@localhost ~]# ip route > /etc/sysconfig/network-scripts/route-ens33