How to install a virtual machine on KVM in Linux (Debian 10)

When performing the next steps, make sure that your KVM has been installed on the virtual machine.

First, configure the network bridge so that KVM and our host can be connected through the bridge.

1. The first step is to switch to the /etc/network/interfaces.d/ directory

cd /etc/network/interfaces.d/

2. The second step is to create a br0 in the interfaces.d/ directory (this is the bridge we created)

touch br0

After creation, you can see that this file exists by running the ls command.

2. The third step is to write the following content into the br0 we just created. Write it according to your own, don’t copy it.

auto br0
iface br0 inet static
#The IP of the bridge configured here. Note that the network segment must be the same as the physical network card. The IP here should not be the same as the physical network card IP (that is, the host number is different)
address 192.168.18.11
#Broadcast address, network segment must be the same as the physical network card
broadcast 192.168.18.255
netmask 255.255.255.0
#Gateway, you can fill in the gateway of your router
gateway 192.168.18.1

#Close spanning tree
bridge_stp off #close Spanning tree

#eno1 is my physical network card. I configured the IP address. This refers to connecting the network bridge to the physical network card eno1.
bridge_ports eno1

bridge_waitport 0

4. After writing, cat the br0 file and you can see that there are already things we configured in it.

cat br0

5. Restart the network configuration to make our network bridge take effect. Execute the following command

 /etc/init.d/networking restart

6. Next, you can create a virtual machine on KVM. First, I will create a vm directory in my home directory (root) and use the vm directory as my working directory. Next, I will create the iso image file and virtual machine stuff. Put them all in this directory

Then I will create a directory iso to place my image file

Next, put all the image files of the system you need to install in the iso directory. I have put what I need as follows.

7. Next I will install Windows. First, I will create a directory named Win10 (self-defined) in the vm directory to place the configuration files and disk files of win10. After creating it, switch the working directory to Win10.

8. Next allocate a disk size to the Win10 virtual machine

qemu-img create -f qcow2 virtual machine name.qcow2 disk size

I allocate a disk size of 20G to the Win10 virtual machine. As shown in the figure below, an extra file will appear. The file has a size of 20G and is used as the disk of the Win10 virtual machine.

8. Next we need to put our configuration file into the Win10 directory and modify the configuration file. After I have modified it, it will look like the following. You can modify it first and then put the configuration file in.

<domain type='kvm' id='6'>
  <name>Win10</name> #Change it to the name of the virtual machine you installed
 #This uuid cannot be the same as other virtual machines, because kvm relies on this to locate a virtual machine.
  <uuid>6a7a8c1b-95ac-48a4-9e4e-3d5c0e103e43</uuid>
  <memory unit='KiB'>4194304</memory> #Here is the configuration memory size. I allocated 4G, so you don’t need to change it.
  <currentMemory unit='KiB'>4194304</currentMemory>
  <vcpu placement='static'>4</vcpu>
  <resource>
    <partition>/machine</partition>
  </resource>
  <os>
    <type arch='x86_64' machine='pc-i440fx-5.2'>hvm</type>
    <boot dev='hd'/>
    <boot dev='cdrom'/>
  </os>
  <features>
    <acpi/>
  </features>
  <cpu mode='custom' match='exact' check='full'>
    <model fallback='forbid'>qemu64</model>
    <feature policy='require' name='x2apic'/>
    <feature policy='require' name='hypervisor'/>
    <feature policy='require' name='lahf_lm'/>
    <feature policy='disable' name='svm'/>
  </cpu>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <devices>
    <emulator>/usr/bin/kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      #Here you need to change the path to the path of your disk file. In my case, I changed it to the path of the previous Win10.qcow2.
      <source file='/root/vm/Win10/Win10.qcow2' index='2'/>
      <backingStore/>
      <target dev='vda' bus='sata'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      #Here you need to put the path of your iso image file. Mine is the path of the Windows.iso file.
      <source file='/root/vm/iso/Windows.iso' index='1'/>
      <backingStore/>
      <target dev='hdc' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='1' target='0' unit='1'/>
    </disk>
    <controller type='usb' index='0' model='piix3-uhci'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'>
    </controller>
    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <controller type='sata' index='0'>
      <alias name='sata0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </controller>
    <interface type='bridge'>
#The mac address cannot be the same as other virtual machines.
      <mac address='00:50:79:10:3e:43'/>
      <source bridge='br0'/>
      <target dev='vnet5'/>
      <model type='e1000'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    </interface>
    <input type='tablet' bus='usb'>
      <address type='usb' bus='0' port='1'/>
    </input>
    <input type='keyboard' bus='ps2'>
    </input>
    <input type='mouse' bus='ps2'>
    </input>
    #The 5901 below is the port that needs to be entered when logging in through vncviewer. If you want to set the port to log in by yourself,
    #You need to change autoport to no. You don’t need to change it here. Then the port number of the first started virtual machine is 5900.
    #The second started port number is 5901, increase accordingly
    <graphics type='vnc' port='5901' autoport='yes' listen='0.0.0.0'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>
    <video>
      <model type='virtio' heads='1' primary='yes'>
        <acceleration accel3d='yes'/>
      </model>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </memballoon>
  </devices>
</domain>

9. Create a virtual machine name.xml file in the Win10 directory, and then put the modified configuration file content into it. I created the Win10.xml file and then used the cat command to write it. You can also use vim text. editor to write

10. Next we need to load our configuration file into the virtual machine. Here I load the Win10 configuration file into the Win10 virtual machine and use the following command

virsh define [the name of the configuration file you just created, mine is Win10.xml]

11. If you want to modify the configuration file in the virtual machine you are currently installing after loading, you can use the following command

virsh edit [virtual machine name, mine is Win10]

12. Next we can start our virtual machine, use the following command

virsh start [virtual machine name, mine is Win10]

13. You can use the following command to view the virtual machine status

virsh list –all

14. The virtual machine has been started. We can check the port number of this virtual machine through the netstat -an command. We can see that it is 5900.

15. Next, you can log in to our virtual machine through the vncviewer tool. Enter the IP and port number set in the bridge (the default port of the software is 5900. If yours is 5901, you only need to write 1, and so on. ), just click connect.

16. Successfully entered (I have already initialized)

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Cloud native entry-level skills treeHomepageOverview 16,860 people are learning the system