创建完kvm虚拟机后,宿主机上会自动创建一个默认网桥(Bridge)virbr0
其作用是给其上的虚机网卡提供 NAT 访问外网的功能
virbr0 默认IP一般为 192.168.122.1/24,并为连接其上的其他虚拟网卡提供 DHCP 服务
[root@controller1 ~]# yum -y install bridge-utils Last metadata expiration check: 2:25:54 ago on Thu 08 Dec 2022 07:14:53 AM CST. Package bridge-utils-1.7.1-2.el8.x86_64 is already installed. Dependencies resolved. Nothing to do. Complete! [root@controller1 ~]# brctl show bridge name bridge id STP enabled interfaces virbr0 8000.52540084740b yes vnet0 [root@controller1 ~]# brctl show virbr0 #可以看到虚拟网卡vnet0已经挂在网桥virbr0上 bridge name bridge id STP enabled interfaces virbr0 8000.52540084740b yes vnet0 [root@controller1 ~]# [root@controller1 ~]# virsh list Id Name State -------------------------------- 1 centos-stream8 running [root@controller1 ~]# virsh domiflist centos-stream8 #可看到虚拟机centos-stream8使用的虚拟网卡正是vnet0 Interface Type Source Model MAC ------------------------------------------------------------- vnet0 network default virtio 52:54:00:cb:8f:37 [root@controller1 ~]#
virbr0 使用 dnsmasq 提供 DHCP 服务,在宿主机中可以查看到该进程
[root@controller1 ~]# ps -elf|grep dnsmasq 5 S dnsmasq 1822 1 0 80 0 - 18351 x64_sy Dec07 ? 00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper 1 S root 1823 1822 0 80 0 - 18325 - Dec07 ? 00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper 0 S root 14846 2268 0 80 0 - 3034 - 09:52 pts/0 00:00:00 grep --color=auto dnsmasq [root@controller1 ~]#
在 /var/lib/libvirt/dnsmasq/virbr0.macs中,可看到虚拟机对应的网卡mac地址信息
[root@controller1 ~]# cat /var/lib/libvirt/dnsmasq/virbr0.macs #可看到virbr0对应虚拟机的mac地址 [ { "domain": "centos-stream8", "macs": [ "52:54:00:cb:8f:37" ] } ] [root@controller1 ~]# virsh domiflist centos-stream8 #可看到虚拟网卡vnet0的mac地址 Interface Type Source Model MAC ------------------------------------------------------------- vnet0 network default virtio 52:54:00:cb:8f:37 [root@controller1 ~]# virsh console centos-stream8 Connected to domain 'centos-stream8' Escape character is ^] (Ctrl + ]) #退出连接按【ctrl + ]】,连接到虚拟机按【Enter】键 CentOS Linux 8 Kernel 4.18.0-348.el8.x86_64 on an x86_64 localhost login: root Password: Last login: Wed Dec 7 20:46:23 on ttyS0 [root@localhost ~]# ip a #可看到虚拟机mac地址 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 52:54:00:cb:8f:37 brd ff:ff:ff:ff:ff:ff [root@localhost ~]# [root@localhost ~]# ping 8.8.8.8 connect: Network is unreachable [root@localhost ~]# nmcli c s NAME UUID TYPE DEVICE enp1s0 258c6e26-0d82-4633-b13b-c2f92cc476ad ethernet -- [root@localhost ~]# nmcli c u enp1s0 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/1) [root@localhost ~]# ping 8.8.8.8 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 64 bytes from 8.8.8.8: icmp_seq=1 ttl=112 time=69.1 ms --- 8.8.8.8 ping statistics --- 2 packets transmitted, 1 received, 50% packet loss, time 1003ms rtt min/avg/max/mdev = 69.123/69.123/69.123/0.000 ms [root@localhost ~]#
网桥IP是可修改的
宿主机virbr0的ip配置
[root@controller1 ~]# cat /etc/libvirt/qemu/networks/default.xml #默认网络配置文件,virbr0的ip在此配置文件中,可看见它是一个软连接 <!-- WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE OVERWRITTEN AND LOST. Changes to this xml configuration should be made using: virsh net-edit default or other application using the libvirt API. --> <network> <name>default</name> <uuid>60f31a91-e8b6-4ef8-bf97-f7c0bbf1e0bb</uuid> <forward mode='nat'/> <bridge name='virbr0' stp='on' delay='0'/> <mac address='52:54:00:84:74:0b'/> <ip address='192.168.122.1' netmask='255.255.255.0'> <dhcp> <range start='192.168.122.2' end='192.168.122.254'/> </dhcp> </ip> </network> [root@controller1 ~]# ls -ld /etc/libvirt/qemu/networks/autostart/default.xml lrwxrwxrwx 1 root root 14 Dec 5 12:55 /etc/libvirt/qemu/networks/autostart/default.xml -> ../default.xml [root@controller1 ~]# ls -ld /etc/libvirt/qemu/networks/default.xml -rw------- 1 root root 576 Dec 5 13:42 /etc/libvirt/qemu/networks/default.xml
修改宿主机virbr0的ip
[root@controller1 ~]# sed -i 's/192.168.122/192.168.115/g' /etc/libvirt/qemu/networks/default.xml [root@controller1 ~]# virsh net-define /etc/libvirt/qemu/networks/default.xml Network default defined from /etc/libvirt/qemu/networks/default.xml [root@controller1 ~]# virsh net-destroy default Network default destroyed [root@controller1 ~]# virsh net-start default Network default started [root@controller1 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:50:56:86:9e:82 brd ff:ff:ff:ff:ff:ff inet 192.168.116.81/24 brd 192.168.116.255 scope global noprefixroute ens192 valid_lft forever preferred_lft forever inet6 fe80::250:56ff:fe86:9e82/64 scope link noprefixroute valid_lft forever preferred_lft forever 4: ens224: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:50:56:86:46:30 brd ff:ff:ff:ff:ff:ff 5: vnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1000 link/ether fe:54:00:cb:8f:37 brd ff:ff:ff:ff:ff:ff inet6 fe80::fc54:ff:fecb:8f37/64 scope link valid_lft forever preferred_lft forever 7: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000 link/ether 52:54:00:84:74:0b brd ff:ff:ff:ff:ff:ff inet 192.168.115.1/24 brd 192.168.115.255 scope global virbr0 valid_lft forever preferred_lft forever [root@controller1 ~]# [root@controller1 ~]# brctl show bridge name bridge id STP enabled interfaces virbr0 8000.52540084740b yes [root@controller1 ~]# brctl addif virbr0 vnet0 [root@controller1 ~]# brctl show bridge name bridge id STP enabled interfaces virbr0 8000.52540084740b yes vnet0
virsh net-list #查看网络状态 virsh net-destroy default #停用default网络,重启libvirtd服务后会恢复 virsh net-undefine default #彻底删除,重启系统后也不会恢复 virsh net-define /etc/libvirt/qemu/networks/default.xml #从一个default.xml文件定义(但不开始)一个网络 virsh net-start default #开始一个(以前定义的default)不活跃的网络,执行后ifconfig可见virbr0 virsh net-autostart default #执行后Autostart外会变成yes virsh domiflist centos-stream8 #查看网络对应状态
brctl show #查看虚拟网卡与宿主网桥对应情况
修改虚拟机ip
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-enp1s0 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=none DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no NAME=enp1s0 UUID=258c6e26-0d82-4633-b13b-c2f92cc476ad DEVICE=enp1s0 ONBOOT=yes IPADDR=192.168.115.100 PREFIX=24 GATEWAY=192.168.115.1 DNS1=114.114.114.114 ~ ~ "/etc/sysconfig/network-scripts/ifcfg-enp1s0" 18L, 324C written [root@localhost ~]# nmcli c r enp1s0 [root@localhost ~]# nmcli c d enp1s0 Connection 'enp1s0' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2) [root@localhost ~]# nmcli c u enp1s0 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3) [root@localhost ~]# ping 8.8.8.8 -c1 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 64 bytes from 8.8.8.8: icmp_seq=1 ttl=112 time=44.9 ms --- 8.8.8.8 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 44.855/44.855/44.855/0.000 ms -enp1s0ocalhost ~]#
注:此时由于虚拟机是nat模式,正常是无法进行外部访问的,如果需要进行外部访问,需要固定虚拟机IP,并在宿主机上配置转发策略
标签:00,default,8.8,kvm,controller1,ff,root,virbr0 From: https://www.cnblogs.com/santia-god/p/16966386.html