O011、理解 virbr0
virbr0 是KVM 默认创建的一个Bridge ,其作用是为该宿主机上的虚机提供NAT上网的功能。virbr0默认分配了一个IP地址 192.168.122.1 并为连接在上面的虚拟网卡提供DHCP服务。
下面我们演示如何使用 virbr0
创建虚机 VM3 ,并设置网卡连接到 NAT 网络中
root@ubuntu:~# virsh list # 查看VM3运行状态
Id Name State
----------------------------------------------------
10 VM3 running
root@ubuntu:~# virsh domiflist VM3 # 查看VM3 虚机接口
Interface Type Source Model MAC
-------------------------------------------------------
vnet0 network default rtl8139 52:54:00:e3:0f:d2
root@ubuntu:~# brctl show # 查看 VM3 虚机接口连接到了 virbr0 上
bridge name bridge id STP enabled interfaces
br0 8000.005056874c70 no ens160
virbr0 8000.fe5400e30fd2 yes vnet0
root@ubuntu:~# ps -ef | grep dnsmasq # virbr0 使用 dnsmasq提供 DHCP服务
libvirt+ 3803 1 0 21:58 ? 00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper
root 3804 3803 0 21:58 ? 00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper
root 6121 5694 0 23:03 pts/3 00:00:00 grep --color=auto dnsmasq
root@ubuntu:~# cat /var/lib/libvirt/dnsmasq/default.conf # DHCP 配置文件
##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
user=libvirt-dnsmasq
pid-file=/var/run/libvirt/network/default.pid
except-interface=lo
bind-dynamic
interface=virbr0
dhcp-range=192.168.122.2,192.168.122.254
dhcp-no-override
dhcp-lease-max=253
dhcp-hostsfile=/var/lib/libvirt/dnsmasq/default.hostsfile
addn-hosts=/var/lib/libvirt/dnsmasq/default.addnhosts
在虚机中进行网络连通性测试,DHCP或得的IP地址可以ping同宿主机和宿主机外面的网络