ip a show virbr0 51: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000 link/ether 52:54:00:04:a9:99 brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 valid_lft forever preferred_lft forever
##WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE ##OVERWRITTEN AND LOST. Changes to this configuration should be made using: ## virsh net-edit default ## or other application using the libvirt API. ## ## dnsmasq conf file created by libvirt strict-order pid-file=/run/libvirt/network/default.pid except-interface=lo bind-dynamic interface=virbr0 dhcp-range=192.168.122.2,192.168.122.254,255.255.255.0 dhcp-no-override dhcp-authoritative dhcp-lease-max=253 dhcp-hostsfile=/var/lib/libvirt/dnsmasq/default.hostsfile addn-hosts=/var/lib/libvirt/dnsmasq/default.addnhosts
ip a eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:e0:70:e8:fd:b7 brd ff:ff:ff:ff:ff:ff inet 10.12.17.200/24 brd 10.12.17.255 scope global dynamic noprefixroute eno1 valid_lft 26998sec preferred_lft 26998sec inet6 fe80::1f78:1d6:7d71:8430/64 scope link noprefixroute valid_lft forever preferred_lft forever
第四行,bridge用于指定构建虚拟网络的网桥设备,虚拟机接入这个网桥设备用于互相通信,这个网桥设备也可以接入到物理LAN中。当forward的mode(模式)为:nat、route或者open时,如果bridge的name未指定,会自动随机生成一个网桥的名称。对于nat、route、open和isolated类型的网络,建议网桥设备的名称以virbr作为前缀。属性stp指定Spanning Tree Protocol(生成树协议,按照树的结构来构造网络拓扑,消除网络中的环路,避免由于环路的存在而造成广播风暴问题。)是on或off(默认on)。属性delay设置网桥桥的转发延迟值,单位是秒(默认为0)。
ip route default via 10.12.17.1 dev eno1 proto dhcp metric 100 10.12.17.0/24 dev eno1 proto kernel scope link src 10.12.17.200 metric 100 192.168.10.0/24 dev virbr-nat proto kernel scope link src 192.168.10.1 linkdown
brctl show bridge name bridge id STP enabled interfaces virbr-nat 8000.5254004dcf1b yes vnet5
查看vnet5的详细信息。
1 2 3 4 5 6
ip a show vnet5 vnet5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master virbr-nat state UNKNOWN group default qlen 1000 link/ether fe:54:00:ec:49:66 brd ff:ff:ff:ff:ff:ff inet6 fe80::fc54:ff:feec:4966/64 scope link valid_lft forever preferred_lft forever
virsh net-dhcp-leases nat-network Expiry Time MAC 地址 Protocol IP address Hostname Client ID or DUID ----------------------------------------------------------------------------------------------------------- 2023-03-07 15:37:49 52:54:00:f4:6a:43 ipv4 192.168.10.10/24 - 01:52:54:00:f4:6a:43
网络测试
利用ping命令测试网络,先从虚拟机ping宿主机,模拟虚拟机访问宿主机。
1 2 3 4 5 6 7 8 9 10 11
ping -c 5 10.12.17.200 PING 10.12.17.200 (10.12.17.200) 56(84) bytes of data. 64 bytes from 10.12.17.200: icmp_seq=1 ttl=64 time=0.170 ms 64 bytes from 10.12.17.200: icmp_seq=2 ttl=64 time=0.357 ms 64 bytes from 10.12.17.200: icmp_seq=3 ttl=64 time=0.441 ms 64 bytes from 10.12.17.200: icmp_seq=4 ttl=64 time=0.246 ms 64 bytes from 10.12.17.200: icmp_seq=5 ttl=64 time=0.237 ms
--- 10.12.17.200 ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4028ms rtt min/avg/max/mdev = 0.170/0.290/0.441/0.096 ms
然后用宿主机ping虚拟机。
1 2 3 4 5 6 7 8 9 10 11
ping -c 5 192.168.10.10 PING 192.168.10.10 (192.168.10.10) 56(84) bytes of data. 64 bytes from 192.168.10.10: icmp_seq=1 ttl=64 time=0.251 ms 64 bytes from 192.168.10.10: icmp_seq=2 ttl=64 time=0.226 ms 64 bytes from 192.168.10.10: icmp_seq=3 ttl=64 time=0.290 ms 64 bytes from 192.168.10.10: icmp_seq=4 ttl=64 time=0.198 ms 64 bytes from 192.168.10.10: icmp_seq=5 ttl=64 time=0.273 ms
--- 192.168.10.10 ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 68ms rtt min/avg/max/mdev = 0.198/0.247/0.290/0.037 ms
用虚拟机ping另一台物理机。
1 2 3 4 5 6 7 8 9 10 11
ping -c 5 10.12.17.116 PING 10.12.17.116 (10.12.17.116) 56(84) bytes of data. 64 bytes from 10.12.17.116: icmp_seq=1 ttl=64 time=0.488 ms 64 bytes from 10.12.17.116: icmp_seq=2 ttl=64 time=0.383 ms 64 bytes from 10.12.17.116: icmp_seq=3 ttl=64 time=0.435 ms 64 bytes from 10.12.17.116: icmp_seq=4 ttl=64 time=0.332 ms 64 bytes from 10.12.17.116: icmp_seq=5 ttl=64 time=0.349 ms
--- 10.12.17.116 ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 108ms rtt min/avg/max/mdev = 0.332/0.397/0.488/0.060 ms
最后在另一台上物理机ping虚拟机。
1 2 3 4 5 6 7 8 9 10 11
ping -c 5 192.168.10.10 PING 192.168.10.10 (192.168.10.10) 56(84) bytes of data. From 192.168.10.1 icmp_seq=1 Destination Host Unreachable From 192.168.10.1 icmp_seq=2 Destination Host Unreachable From 192.168.10.1 icmp_seq=3 Destination Host Unreachable From 192.168.10.1 icmp_seq=4 Destination Host Unreachable From 192.168.10.1 icmp_seq=5 Destination Host Unreachable
ip a eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:e0:70:e8:fd:b7 brd ff:ff:ff:ff:ff:ff inet 10.12.17.200/24 brd 10.12.17.255 scope global dynamic noprefixroute eno1 valid_lft 26998sec preferred_lft 26998sec inet6 fe80::1f78:1d6:7d71:8430/64 scope link noprefixroute valid_lft forever preferred_lft forever
ip a virbr-routed: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000 link/ether 52:54:00:58:71:34 brd ff:ff:ff:ff:ff:ff inet 192.168.20.1/24 brd 192.168.20.255 scope global virbr-routed valid_lft forever preferred_lft forever
利用brctl命令查看网桥设备。
1 2 3
brctl show bridge name bridge id STP enabled interfaces virbr-routed 8000.525400233d8e yes
Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 53394 3746K LIBVIRT_PRT all -- * * 0.0.0.0/0 0.0.0.0/0
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination
Chain LIBVIRT_PRT (1 references) pkts bytes target prot opt in out source destination
nat表中并未增加任何的新的规则。
路由规则
routed网络创建后,宿主机上会新增一个192.168.20.0/24网段的路由规则。
1 2 3 4
ip route default via 10.12.17.1 dev eno1 proto dhcp metric 100 10.12.17.0/24 dev eno1 proto kernel scope link src 10.12.17.200 metric 100 192.168.20.0/24 dev virbr-routed proto kernel scope link src 192.168.20.1 linkdown
brctl show bridge name bridge id STP enabled interfaces virbr-routed 8000.525400233d8e yes vnet6
查看vnet6的详细信息。
1 2 3 4 5
ip a show vnet6 vnet6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master virbr-routed state UNKNOWN group default qlen 1000 link/ether fe:54:00:f4:6a:43 brd ff:ff:ff:ff:ff:ff inet6 fe80::fc54:ff:fef4:6a43/64 scope link valid_lft forever preferred_lft forever
ping -c 5 10.12.17.200 PING 10.12.17.200 (10.12.17.200) 56(84) bytes of data. 64 bytes from 10.12.17.200: icmp_seq=1 ttl=64 time=0.232 ms 64 bytes from 10.12.17.200: icmp_seq=2 ttl=64 time=0.270 ms 64 bytes from 10.12.17.200: icmp_seq=3 ttl=64 time=0.186 ms 64 bytes from 10.12.17.200: icmp_seq=4 ttl=64 time=0.425 ms 64 bytes from 10.12.17.200: icmp_seq=5 ttl=64 time=0.299 ms
--- 10.12.17.200 ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4052ms rtt min/avg/max/mdev = 0.186/0.282/0.425/0.080 ms
然后用宿主机ping虚拟机。
1 2 3 4 5 6 7 8 9 10 11
ping -c 5 192.168.20.20 PING 192.168.20.20 (192.168.20.20) 56(84) bytes of data. 64 bytes from 192.168.20.20: icmp_seq=1 ttl=64 time=0.183 ms 64 bytes from 192.168.20.20: icmp_seq=2 ttl=64 time=0.170 ms 64 bytes from 192.168.20.20: icmp_seq=3 ttl=64 time=0.190 ms 64 bytes from 192.168.20.20: icmp_seq=4 ttl=64 time=0.288 ms 64 bytes from 192.168.20.20: icmp_seq=5 ttl=64 time=0.210 ms
--- 192.168.20.254 ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 57ms rtt min/avg/max/mdev = 0.170/0.208/0.288/0.042 ms
用虚拟机ping另一台物理机。
1 2 3 4 5 6 7 8 9 10 11
ping -c 5 10.12.17.116 PING 10.12.17.116 (10.12.17.116) 56(84) bytes of data. From 192.168.20.20 icmp_seq=1 Destination Host Unreachable From 192.168.20.10 icmp_seq=2 Destination Host Unreachable From 192.168.20.10 icmp_seq=3 Destination Host Unreachable From 192.168.20.10 icmp_seq=4 Destination Host Unreachable From 192.168.20.10 icmp_seq=5 Destination Host Unreachable