(host)$ ip link show ... 46: tap0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether 42:47:1b:de:8e:07 brd ff:ff:ff:ff:ff:ff 47: tap1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/ether f2:23:b1:c5:55:d3 brd ff:ff:ff:ff:ff:ff
可以看到随虚拟机启动,创建了4个tap接口
2. 配置IP
VM1
1
(vm1)$ sudo ifconfig eth0 192.168.1.10
VM2
1
(vm2)$ sudo ifconfig eth0 192.168.1.11
3. 创建网桥
Host
1 2 3 4 5 6
(host)$ brctl addbr br0 (host)$ brctl addif br0 tap0 (host)$ brctl addif br0 tap1 (host)$ bridge name bridge id STP enabled interfaces br0 8000.42471bde8e07 no tap0 tap1
尝试ping一下,发现还是ping不通。因为对应的tap接口及br0没有up
1 2 3
(host)$ ifconfig br0 up (host)$ ifconfig tap0 up (host)$ ifconfig tap1 up
现在,可以两个VM相互ping一下了
4. 访问外部PC
虚拟机需要访问外部网络,则需要添加物理接口,如下图
Host
1 2
(host)$ ifconfig ens192 up (host)$ brctl addif br0 ens192
VM1
1 2 3 4
(vm1)$ ping 192.168.1.3 PING 192.168.1.3 (192.168.1.3): 56 data bytes 64 bytes from 192.168.1.3: seq=0 ttl=64 time=29.700 ms ...
抓个包看看 - ens192
1 2 3 4 5
(host)$ tcpdump -i ens192 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on ens192, link-type EN10MB (Ethernet), capture size 262144 bytes 11:15:02.317164 IP 192.168.1.11 > 192.168.1.3: ICMP echo request, id 47873, seq 0, length 64 11:15:02.317527 IP 192.168.1.3 > 192.168.1.11: ICMP echo reply, id 47873, seq 0, length 64