虚拟机的网络桥接bridge模式往往需要物理宿主机有两个网口,一个网口1连接外网配置ip,另一个网口2空闲不配置ip,在virt-manager里配置虚拟机的网卡绑定网口2,从而实现虚拟机桥接网络模式。
由于办公环境只有一台单网口的主机,通过手动配置arp与路由的方式可实现下方的网络拓扑关系。如果单网口主机有相同配置虚拟机传统桥接网络需求的话,可以参考本文。
先贴下网络拓扑图:
主机A、B、C处于同一个内网172.30.120.0/24里,其中虚拟机C的宿主物理机是B。
按本文操作最终可实现A<->B、A<->C、B<->C互通。
krokodil@krokodil-SY-ZL-H110N-D3V:~$ ifconfig
... ...
enp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.30.120.125 netmask 255.255.255.0 broadcast 172.30.120.255
inet6 fe80::10f4:3d60:14c5:c457 prefixlen 64 scopeid 0x20<link>
ether 00:e0:4c:5a:03:01 txqueuelen 1000 (以太网)
RX packets 45804 bytes 15342946 (15.3 MB)
RX errors 0 dropped 5188 overruns 0 frame 0
TX packets 20896 bytes 3041221 (3.0 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
... ...
krokodil@krokodil-SY-ZL-H110N-D3V:~$ nm-connection-editor
krokodil@krokodil-SY-ZL-H110N-D3V:~$ ifconfig
bridge0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.30.120.127 netmask 255.255.255.0 broadcast 172.30.120.255
ether de:91:be:83:e7:72 txqueuelen 1000 (以太网)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
enp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.30.120.125 netmask 255.255.255.0 broadcast 172.30.120.255
inet6 fe80::10f4:3d60:14c5:c457 prefixlen 64 scopeid 0x20<link>
ether 00:e0:4c:5a:03:01 txqueuelen 1000 (以太网)
RX packets 8250 bytes 7874949 (7.8 MB)
RX errors 0 dropped 84 overruns 0 frame 0
TX packets 6888 bytes 1845403 (1.8 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
... ...
krokodil@krokodil-SY-ZL-H110N-D3V:~$
krokodil@krokodil-SY-ZL-H110N-D3V:~$ arp -n
地址 类型 硬件地址 标志 Mask 接口
172.30.120.254 (incomplete) bridge0
172.30.120.120 (incomplete) enp1s0
172.30.120.120 ether 52:54:00:32:94:c2 C bridge0
172.30.120.254 ether 04:fe:8d:8e:31:e1 C enp1s0
krokodil@krokodil-SY-ZL-H110N-D3V:~$
krokodil@krokodil-SY-ZL-H110N-D3V:~$ sudo arp -s 172.30.120.120 52:54:00:32:94:c2 -i enp1s0
krokodil@krokodil-SY-ZL-H110N-D3V:~$ arp -n
地址 类型 硬件地址 标志 Mask 接口
172.30.120.254 (incomplete) bridge0
172.30.120.120 ether 52:54:00:32:94:c2 CM enp1s0
172.30.120.120 ether 52:54:00:32:94:c2 C bridge0
172.30.120.254 ether 04:fe:8d:8e:31:e1 C enp1s0
krokodil@krokodil-SY-ZL-H110N-D3V:~$
内核 IP 路由表
目标 网关 子网掩码 标志 跃点 引用 使用 接口
0.0.0.0 172.30.120.254 0.0.0.0 UG 100 0 0 enp1s0
0.0.0.0 172.30.120.254 0.0.0.0 UG 20425 0 0 bridge0
10.10.100.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 virbr0
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
172.30.120.0 0.0.0.0 255.255.255.0 U 100 0 0 enp1s0
172.30.120.0 0.0.0.0 255.255.255.0 U 425 0 0 bridge0
krokodil@krokodil-SY-ZL-H110N-D3V:~$ sudo route add 172.30.120.120 dev bridge0
krokodil@krokodil-SY-ZL-H110N-D3V:~$ route -n
内核 IP 路由表
目标 网关 子网掩码 标志 跃点 引用 使用 接口
0.0.0.0 172.30.120.254 0.0.0.0 UG 100 0 0 enp1s0
0.0.0.0 172.30.120.254 0.0.0.0 UG 20425 0 0 bridge0
10.10.100.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 virbr0
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
172.30.120.0 0.0.0.0 255.255.255.0 U 100 0 0 enp1s0
172.30.120.0 0.0.0.0 255.255.255.0 U 425 0 0 bridge0
172.30.120.120 0.0.0.0 255.255.255.255 UH 0 0 0 bridge0
krokodil@krokodil-SY-ZL-H110N-D3V:~$
enaftgm1i0: flags=4163 mtu 1500
inet 172.30.120.121 netmask 255.255.255.0 broadcast 172.30.120.255
inet6 fe80::54fe:45f6:974e:8db3 prefixlen 64 scopeid 0x20
ether 00:07:3e:9d:55:8f txqueuelen 1000 (以太网)
RX packets 137322 bytes 43916437 (43.9 MB)
RX errors 0 dropped 4476 overruns 0 frame 0
TX packets 188258 bytes 22199429 (22.1 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 7 base 0xc000
root@sujing-GW-001N1B-FTF:~#
PING 172.30.120.120 (172.30.120.120) 56(84) bytes of data.
From 172.30.120.121 icmp_seq=1 Destination Host Unreachable
--- 172.30.120.120 ping statistics ---
1 packets transmitted, 0 received, +1 errors, 100% packet loss, time 0ms
tcpdump: listening on enaftgm1i0, link-type EN10MB (Ethernet), capture size 262144 bytes
15:09:54.554190 ARP, Ethernet (len 6), IPv4 (len 4), Request who-has 172.30.120.120 tell 172.30.120.121, length 28
15:09:55.580834 ARP, Ethernet (len 6), IPv4 (len 4), Request who-has 172.30.120.120 tell 172.30.120.121, length 28
15:09:56.600821 ARP, Ethernet (len 6), IPv4 (len 4), Request who-has 172.30.120.120 tell 172.30.120.121, length 28
^C
3 packets captured
3 packets received by filter
0 packets dropped by kernel
root@sujing-GW-001N1B-FTF:~#
地址 类型 硬件地址 标志 Mask 接口
172.30.120.254 ether 04:fe:8d:8e:31:e1 C enaftgm1i0
172.30.120.120 (incomplete) enaftgm1i0
... ...
root@sujing-GW-001N1B-FTF:~# sudo arp -s 172.30.120.120 00:e0:4c:5a:03:01 -i enaftgm1i0
root@sujing-GW-001N1B-FTF:~# arp -n
地址 类型 硬件地址 标志 Mask 接口
172.30.120.254 ether 04:fe:8d:8e:31:e1 C enaftgm1i0
172.30.120.120 ether 00:e0:4c:5a:03:01 CM enaftgm1i0
... ... ... ...
这次icmp request成功到达了物理机B的enp1s0网口,但是在bridge0网口看到虚拟机C 172.30.120.120发出的arp查询包。
在虚拟机C内部配置arp规则172.30.120.121 de:91:be:83:e7:72 (注意!!这里要配置bridge0 mac地址)
root@sujing-GW-001N1B-FTF:~# ping 172.30.120.120 -c 1
PING 172.30.120.120 (172.30.120.120) 56(84) bytes of data.
64 bytes from 172.30.120.120: icmp_seq=1 ttl=63 time=0.731 ms
--- 172.30.120.120 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.731/0.731/0.731/0.000 ms
root@sujing-GW-001N1B-FTF:~#
从物理机A 网口向 虚拟机C ping一个包,抓包看整个过程:
root@sujing-GW-001N1B-FTF:~# tcpdump -i enaftgm1i0 -venn host 172.30.120.120
tcpdump: listening on enaftgm1i0, link-type EN10MB (Ethernet), capture size 262144 bytes
16:14:10.131325 00:07:3e:9d:55:8f > 00:e0:4c:5a:03:01, ethertype IPv4 (0x0800), length 98: (tos 0x0, ttl 64, id 51167, offset 0, flags [DF], proto ICMP (1), length 84)
172.30.120.121 > 172.30.120.120: ICMP echo request, id 6552, seq 1, length 64
16:14:10.131851 00:e0:4c:5a:03:01 > 00:07:3e:9d:55:8f, ethertype IPv4 (0x0800), length 98: (tos 0x0, ttl 63, id 54622, offset 0, flags [none], proto ICMP (1), length 84)
172.30.120.120 > 172.30.120.121: ICMP echo reply, id 6552, seq 1, length 64
^C
2 packets captured
2 packets received by filter
0 packets dropped by kernel
root@sujing-GW-001N1B-FTF:~#
tcpdump: listening on enp1s0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
16:14:10.163279 00:07:3e:9d:55:8f > 00:e0:4c:5a:03:01, ethertype IPv4 (0x0800), length 98: (tos 0x0, ttl 64, id 51167, offset 0, flags [DF], proto ICMP (1), length 84)
172.30.120.121 > 172.30.120.120: ICMP echo request, id 6552, seq 1, length 64
16:14:10.163690 00:e0:4c:5a:03:01 > 00:07:3e:9d:55:8f, ethertype IPv4 (0x0800), length 98: (tos 0x0, ttl 63, id 54622, offset 0, flags [none], proto ICMP (1), length 84)
172.30.120.120 > 172.30.120.121: ICMP echo reply, id 6552, seq 1, length 64
^C
2 packets captured
2 packets received by filter
0 packets dropped by kernel
root@krokodil-SY-ZL-H110N-D3V:~#
tcpdump: listening on bridge0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
16:14:10.163315 de:91:be:83:e7:72 > 52:54:00:32:94:c2, ethertype IPv4 (0x0800), length 98: (tos 0x0, ttl 63, id 51167, offset 0, flags [DF], proto ICMP (1), length 84)
172.30.120.121 > 172.30.120.120: ICMP echo request, id 6552, seq 1, length 64
16:14:10.163677 52:54:00:32:94:c2 > de:91:be:83:e7:72, ethertype IPv4 (0x0800), length 98: (tos 0x0, ttl 64, id 54622, offset 0, flags [none], proto ICMP (1), length 84)
172.30.120.120 > 172.30.120.121: ICMP echo reply, id 6552, seq 1, length 64
^C
2 packets captured
2 packets received by filter
0 packets dropped by kernel
root@krokodil-SY-ZL-H110N-D3V:~#