Rate This Document
Findability
Accuracy
Completeness
Readability

(Optional) Configuring a VM Network

To access the services on a KVM VM from another PC on the same LAN, you can connect the KVM VM (guest) to the host machine in bridge mode so that the VM is allocated an independent IP address on the LAN.

If you do not need to access the KVM VM from other PCs on the LAN, you do not need to configure the network.

Configuring a Network Bridge

  1. View the network information.
    ip a

    This example illustrates how to bridge the KVM VM to the network segment (192.168.224.XXX, subnet mask: 255.255.255.0) where enp3s0 is located.

  2. Check for network bridges and ensure that the br0 network bridge does not exist.
    1
    brctl show
    
  3. Stop the NetworkManager service to prevent configuration modifications from taking effect unexpectedly.
    1
    systemctl stop NetworkManager
    
  4. Create a br0 network bridge.
    1. Open the ifcfg-br0 file.
      1
      vi /etc/sysconfig/network-scripts/ifcfg-br0
      
    2. Press i to enter the insert mode and add the following content:
      DEVICE=br0
      ONBOOT=yes
      TYPE=Bridge
      NM_CONTROLLED=yes
      BOOTPROTO=static
      IPADDR=192.168.224.2
      NETMASK=255.255.255.0
      GATEWAY=192.168.224.1
      STP=on
      DELAY=0.0
    3. Press Esc, type :wq!, and press Enter to save the file and exit.
    • In this example, replace the values of IPADDR, NETMASK, and GATEWAY with actual ones.
    • If you perform the following operations to create a network bridge, the network bridge becomes invalid after the host machine is restarted. Replace the IP address in the example with the actual one.
      brctl addbr br0
      brctl addif br0 enp3s0
      ifconfig br0 192.168.224.2/24
      ifconfig br0 up
  5. Modify the ifcfg-enp3s0 file to connect the enp3s0 network port to the br0 bridge.
    1. Open the ifcfg-enp3s0 file.
      1
      vi /etc/sysconfig/network-scripts/ifcfg-enp3s0
      
    2. Press i to enter the insert mode, set BOOTPROTO to none, set ONBOOT to yes, comment out the original IP address, network mask, and gateway, and add the following information:
      BRIDGE=br0
    3. Press Esc, type :wq!, and press Enter to save the file and exit.
  6. Make the ifcfg-br0 and ifcfg-enp3s0 files take effect.
    • CentOS
      1
      systemctl restart network
      
    • openEuler
      1
      systemctl restart NetworkManager
      

      If the configuration does not take effect after the network service is restarted, check whether the previous configurations are correct. Alternatively, restart the host machine and then restart the network service.

  7. View the network information.
    1
    ip a
    

  8. View the network bridge.
    1
    brctl show
    

    If enp3s0 is displayed under interfaces of br0, the network bridge is configured successfully.

Configuring the VM

  1. Modify the VM configuration.
    1. Open the VM configuration file.
      1
      vi /etc/libvirt/qemu/vm1.xml
      

      Or

      1
      virsh edit vm1
      
    2. Find the <interface> tag under <devices>.

      If the <interface> tag does not exist, add the following content under the <devices> tag:

      1
      2
      3
      4
      5
      6
      <interface type='bridge'>
            <mac address='52:54:00:7a:95:48'/>
            <source bridge='br0'/>
            <model type='virtio'/>
            <address type='pci' domain='0x0000' bus='0x01' slot='0x00' function='0x0'/>
      </interface>
      
      • <interface type='bridge'> indicates that the VM uses the bridge mode.
      • The MAC address in <mac address='52:54:00:7a:95:48'/> is automatically generated after the VM is installed and is used as the MAC address of the VM NIC eth0. Generally, you do not need to change the MAC address. If you need to change the MAC address, ensure that it is different from the MAC address of the br0 network bridge.
      • <source bridge='br0'/> indicates the connection to br0 network bridge on the host machine.
    3. Press Esc, type :wq!, and press Enter to save the file and exit.
  2. Make the VM configuration take effect.
    1
    systemctl restart libvirtd
    
  3. Stop the VM.
    1. View the VM status.
      1
      virsh list --all
      
    2. Stop the VM.
      1
      virsh shutdown vm1
      
    3. Check whether the VM has been stopped.
      1
      virsh list --all
      

  4. Start the VM.
    virsh start vm1
    virsh list --all

  5. View the network bridge.
    1
    brctl show
    

    If vnet0 is displayed under interfaces of br0, vm1 is successfully connected to the network bridge. (vnet0 exists only after the vm1.xml configuration takes effect and vm1 is successfully started.)

  6. Log in to the VM.
    1
    2
    virsh list --all
    virsh console vm1 --force
    
  7. Configure the NIC.
    1. View the network information.
      ip a
    2. Modify the NIC configuration.
      • CentOS
        1. Edit the ifcfg-eth0 file.
          1
          vi /etc/sysconfig/network-scripts/ifcfg-eth0
          
        2. Press i to enter the insert mode and modify the following content:

          Set IPADDR to an unused IP address that is on the same network segment as the host machine. Set this parameter as required.

          TYPE=Ethernet
          PROXY_METHOD=none
          BROWSER_ONLY=no
          BOOTPROTO=static
          DEFROUTE=yes
          IPV4_FAILURE_FATAL=no
          IPV6INIT=yes
          IPV6_AUTOCONF=yes
          IPV6_DEFROUTE=yes
          IPV6_FAILURE_FATAL=no
          IPV6_ADDR_GEN_MODE=stable-privacy
          NAME=eth0
          UUID=aca69508-f4c7-4ff2-9cc5-668ac45e429e
          DEVICE=eth0
          ONBOOT=yes
          IPADDR=192.168.224.3
          NETMASK=255.255.255.0
          GATEWAY=192.168.224.1
        3. Press Esc, type :wq!, and press Enter to save the file and exit.
      • openEuler
        1. Open the ifcfg-enp1s0 file.
          1
          vi /etc/sysconfig/network-scripts/ifcfg-enp1s0
          
        2. Press i to enter the insert mode and modify the following content:
          TYPE=Ethernet
          PROXY_METHOD=none
          BROWSER_ONLY=no
          BOOTPROTO=static
          DEFROUTE=yes
          IPV4_FAILURE_FATAL=no
          IPV6INIT=yes
          IPV6_AUTOCONF=yes
          IPV6_DEFROUTE=yes
          IPV6_FAILURE_FATAL=no
          IPV6_ADDR_GEN_MODE=stable-privacy
          NAME=enp1s0
          UUID=dedf1652-5eff-3fb4-be23-c658b834774f
          DEVICE=enp1s0
          ONBOOT=yes
          AUTOCONNECT_PRIORITY=-999
          IPADDR=192.168.224.13
          NETMASK=255.255.255.0
          GATEWAY=192.168.224.1

          Set IPADDR to an unused IP address that is on the same network segment as the host machine. Set this parameter as required.

        3. Press Esc, type :wq!, and press Enter to save the file and exit.
  8. Make the NIC take effect.
    • CentOS
      systemctl restart network
      ip a
    • openEuler
      systemctl restart NetworkManager
      ifdown enp1s0
      ifup enp1s0
      ip a

Verifying the Network

  1. Check whether the bridge is configured. Ping the VM and host machine, the VM and other hosts in the LAN, and the host machine and other hosts in the LAN.
    • If the ping fails, analyze the problem of bridge br0 and go to 2.
    • If the VM can communicate with only the host machine but not with other hosts on the LAN, you may need to disable the network filter on the host machine and go to 4.
    • If the ping succeeds, the bridge connection has been configured. No further action is required.
  2. Locate the fault.
    1. On the host machine, capture packets on bridge br0.
      yum -y install tcpdump
      tcpdump -i br0
    2. Locate and rectify the fault based on the captured packets.
  3. Perform 1 again.
  4. Disable the network filter.
    1. Edit the sysctl.conf file.
      1
      vim /etc/sysctl.conf
      
    2. Press i to enter the insert mode and add the following content to the file:
      net.bridge.bridge-nf-call-ip6tables = 0
      net.bridge.bridge-nf-call-iptables = 0
      net.bridge.bridge-nf-call-arptables = 0
    3. Press Esc, type :wq!, and press Enter to save the file and exit.
    4. Reload the kernel parameters for the configuration to take effect.
      1
      sysctl -p
      
  5. Perform 1 again.