Follow installation steps of KVM on Debian Linux 9.x headless sever

Step 1: Install KVM

Type the following apt-get command/apt command:

$ sudo apt install qemu-kvm libvirt-clients libvirt-daemon-system bridge-utils libguestfs-tools genisoimage virtinst libosinfo-bin


Allow normal user to manage virtual machine

If you want normal/regular user can manage virtual machines. Add user admin01 to libvirt and libvirt-qemu using the usermod command:

$ sudo adduser admin01 libvirt
$ sudo adduser admin01 libvirt-qemu



Reload group membership with the help of newgrp command:


$ newgrp libvirt
$ newgrp libvirt-qemu



Verify your group membership with id command:

$ id


Please note that you need to use the following command to connect to KVM server:

$ virsh --connect qemu:///system
$ virsh --connect qemu:///system command
$ virsh --connect qemu:///system list --all


Step 2: Verify KVM installation on Debian

Run the following egrep command to verify that Intel VMX or AMD SVM supported on your CPU:

$ egrep --color 'vmx|svm' /proc/cpuinfo


Step 3: Configure bridged networking on Debian

I am going to create bridge Interface br0 as the network connection in VM guests configuration for eth0 interface:

$ sudo vi /etc/network/interfaces.d/br0

Append the following:

## make sure all config related to eth0 deleted ##
auto br0
iface br0 inet static
	address 192.168.2.23        ## set up/netmask/broadcast/gateway as per your setup
	broadcast 192.168.2.255
	netmask 255.255.255.0
	gateway 192.168.2.254
	bridge_ports eth0    # replace eth0 with your actual interface name
	bridge_stp off       # disable Spanning Tree Protocol
        bridge_waitport 0    # no delay before a port becomes available
        bridge_fd 0          # no forwarding delay

Restart the networking service on Linux:


$ sudo systemctl restart network-manager

To see current networking setting for KVM, run:

$ sudo virsh net-list --all


You need to configure a KVM guest domain on a bridged network. To create a file named bridge.xml as follows a text editor such as NA command:


$ sudo vi /root/bridged.xml


Append the following config:

<network>
<name>br0</name>
<forward mode="bridge"/>
<bridge name="br0"/>
</network>

Save and close the file in vi/vim

Step 4: Create your first virtual machine using an ISO image installer

I am going to create a CentOS 7.x VM. First, grab CentOS 7.x latest ISO image:

$ cd /var/lib/libvirt/boot/
$ sudo wget https://mirrors.kernel.org/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1708.iso


CREATE CENTOS 7 VM

In this example, I’m creating CentOS 7.x VM with 2GB RAM, 2 CPU core, 1 nic and 40GB disk space, enter:

$ sudo virt-install \
--virt-type=kvm \
--name centos7 \
--ram 2048 \
--vcpus=2 \
--os-variant=rhel7 \
--virt-type=kvm \
--hvm \
--cdrom=/var/lib/libvirt/boot/CentOS-7-x86_64-DVD-1708.iso \
--network=bridge=br0,model=virtio \
--graphics vnc \
--disk path=/var/lib/libvirt/images/centos7.qcow2,size=40,bus=virtio,format=qcow2


To configure vnc login from another terminal over ssh and type:

$ sudo virsh dumpxml centos7 | grep vnc
<graphics type='vnc' port='5901' autoport='yes' listen='127.0.0.1'>

You can also use the following command:
$ sudo virsh vncdisplay centos7

Please note down the port value (i.e. 5901). You need to use an SSH client to setup tunnel and a VNC client to access the remote vnc server.

 

Once you have ssh tunnel established, you can point your VNC client at your own 127.0.0.1 (localhost) address and port 5901 as follows:

$ sudo virsh net-define --file /root/bridged.xml
$ sudo virsh net-autostart br0
$ sudo virsh net-start br0


You should see CentOS Linux 7 guest installation screen as follows:

Now just follow on-screen instructions and install CentOS 7. Once installed, go ahead and click reboot button. The remote server closed the connection to our VNC client. You can reconnect via KVM client to configure the rest of the server including SSH based session or firewall.


Step 5 – Use virt-builder to create VM

Above method (virt-install) works nicely but if you need quickly building new virtual machines, try virt-builder.


How to list the virtual machines available

$ virt-builder --list | more

You can use the grep command to filter out only x86_64 arch based VMs:


$ virt-builder --list | grep x86_64


To see additional notes for any os run:


$ virt-builder --notes ubuntu-16.04
$ virt-builder --notes debian-9

Create Debian 9.x VM

Create Debian 9 VM with 10GB disk space, 2GB ram, 2 vCPU and random password for root account, run:

$ sudo virt-builder debian-9 \
--size=10G \
--format qcow2 -o /var/lib/libvirt/images/debian9-vm1.qcow2 \
--hostname debain9-vm1 \
--network \
--timezone Asia/Kolkata

Finally import image with virt-install command:


$ sudo virt-install --import --name debian9-vm1 \
--ram 2048 \
--vcpu 2 \
--disk path=/var/lib/libvirt/images/debian9-vm1.qcow2,format=qcow2 \
--os-variant debian9 \
--network=bridge=br0,model=virtio \
--noautoconsole


You can login to your VM using x0E4iZ8sHjA6ekb6 password for root account:


$ sudo virsh list --all
$ virsh console debian9-vm1

You must disable root account for ssh session and create ssh keys for your VM. Login as above:


# dpkg-reconfigure openssh-server
# useradd -r -m -d /home/vivek -s /bin/bash vivek
# passwd vivek
# systemctl enable ssh
### [ Disable root user login when using ssh] ###
# echo 'PermitRootLogin no' >> /etc/ssh/sshd_config
# systemctl restart ssh
# ip a s
Verify that you can login using an IP address for admin01user and use ‘su -‘ to become a root user.


Was this answer helpful? 0 Users Found This Useful (1 Votes)