KaliLinux 2018.2: Install KVM for virtualization

This article will describe installing KVM.

1 Install qemu-kvm and libvirt

Install qemu-kvm package and some packages for virtual machine operation.

$ sudo apt install -y qemu-kvm libvirt0 virt-manager libguestfs-tools

2 Add user to libvirt group

The user in libvirt group can run virt-manager and virsh without sudo.

$ sudo gpasswd libvirt -a <username>
$ cat <<EOF | tee -a ~/.profile
export VIRSH_DEFAULT_CONNECT_URI=qemu:///system
$ sudo reboot

3 Remote access via SSH

virt-manager and virsh provdes remote access via SSH. If you uses SSH public key authentication, you can omit inputing password.

Create SSH keys on client which uses remote access via SSH.

$ ssh-keygen -t rsa -f ~/.ssh/id_rsa -N ""
Generating public/private rsa key pair.
Your identification has been saved in /home/hiroom2/.ssh/id_rsa.
Your public key has been saved in /home/hiroom2/.ssh/id_rsa.pub.
$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB... username@clientname

Add SSH public key entry to ~/.ssh/authorized_keys on server which is installed KVM.

$ cat ~/.ssh/authorized_keys
ssh-rsa AAAAB... username@clientname

Specify server URI with -c option on client.

$ virsh -c qemu+ssh://username@servername/system

Setting server URI to VIRSH_DEFAULT_CONNECT_URI is equal with specifying server URI with -c option. Write VIRSH_DEFAULT_CONNECT_URI settings in your .bashrc if you need.

$ export VIRSH_DEFAULT_CONNECT_URI=qemu+ssh://username@servername/system
$ virsh

4 Add a pool for ISO file

Create iso directory and define as pool.

$ sudo mkdir /var/lib/libvirt/isos
$ virsh pool-define-as isos dir - - - - /var/lib/libvirt/isos
$ virsh pool-autostart isos
$ virsh pool-start isos