Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Create Debian 10 (Buster) KVM Guest From Cloud Image

This tutorial will show you how to install Debian 10 on your KVM server through the use of one of the official Debian cloud images.

For some reason, I was having strange issues with Debian 10 guests I had installed through traditional ISO means on my Hetzner KVM, so I wanted to try the "cloud image" way, and this seems to have resolved the strange issues I was having.

Similar Posts

Steps

Download the Debian 10 cloud Image:

sudo mkdir /var/lib/libvirt/images/templates
wget https://cloud.debian.org/images/cloud/buster/daily/latest/debian-10-nocloud-amd64-daily.qcow2

sudo mv -i debian-10-nocloud-amd64-daily.qcow2 \
  /var/lib/libvirt/images/templates/debian-10.qcow2

I really wanted to use the genericcloud image, but had to use an image I could log into with. Unfortunately, it looks like genericcloud is expecting one to just set an SSH key and use that to log in, but I need to be able to log in before I can fix up the networking within my Hetzner KVM.

We need to ensure we have cloud-utils and whois packages for later.

sudo apt update && sudo apt install cloud-utils whois -y

The cloud-utils package is required for Debian 10 to be able to run the cloud-localds command, and the whois package is required for us to be able to run mkpasswd later. Your distro may have a different name for the required packages.

Steps

First, pick a name for your new VPS, and what you would want the username/password combo to be for the default user that will be set up:

VM_NAME="debian-10-cloud-image"
USERNAME="programster"
PASSWORD="thisIsMyPassword"

Create an area for our new VM and copy the template cloud image into it, which will be used by the new VM.

sudo mkdir /var/lib/libvirt/images/$VM_NAME \
  && sudo qemu-img convert \
  -f qcow2 \
  -O qcow2 \
  /var/lib/libvirt/images/templates/debian-10.qcow2 \
  /var/lib/libvirt/images/$VM_NAME/root-disk.qcow2

Increase the disk size to whatever you want for the VM, in this case I'm setting 20 GB

sudo qemu-img resize \
  /var/lib/libvirt/images/$VM_NAME/root-disk.qcow2 \
  20G

Create a cloud-init configuration so we can set the password and the hostname etc.

sudo echo "#cloud-config
hostname: $VM_NAME
" | sudo tee /var/lib/libvirt/images/$VM_NAME/cloud-init.cfg

Create the ISO file from the cloud config file we just created:

sudo cloud-localds \
  /var/lib/libvirt/images/$VM_NAME/cloud-init.iso \
  /var/lib/libvirt/images/$VM_NAME/cloud-init.cfg
sudo virt-install \
  --name $VM_NAME \
  --memory 1024 \
  --disk /var/lib/libvirt/images/$VM_NAME/root-disk.qcow2,device=disk,bus=virtio \
  --disk /var/lib/libvirt/images/$VM_NAME/cloud-init.iso,device=cdrom \
  --os-type linux \
  --os-variant debian10 \
  --virt-type kvm \
  --graphics none \
  --network network=default,model=virtio \
  --import

Login

After running the previous command, you will be taken to the login screen in the console, which you can log in with the username root. You will not be prompted for a password.

Now you really need to manually create your account username, and set a strong random password for the root user.

Changing Network Settings

One of the first things I wanted to do is change the network settings. This is best done by editing the single file within the following folder:

/run/network/interfaces.d/

Exit Console

If you need to get out of the console just press ctrl + ].

Cleanup

After that, it's probably a good idea to cleanup so raw passwords aren't lying around.

sudo rm /var/lib/libvirt/images/$VM_NAME/cloud-init.iso \
  && sudo rm /var/lib/libvirt/images/$VM_NAME/cloud-init.cfg

Hostname And Hosts File

It appears that setting the hostname in the cloud-init config will set the hostname that the machine will boot as. However, it will not add the entry to the /etc/hosts file, so you may wish to do this quickly to prevent your machine taking unnecessarily long when performing some operations. Alternatively, you may figure out how to update the cloud-init.cfg file to update the hosts file correctly using the Etc hosts module.

SSH Not Working

You will need to run the following command to get your server to generate its host key so that you can SSH into the server. More info here.

sudo dpkg-reconfigure openssh-server

I would also suggest checking that you are happy with your /etc/ssh/sshd_config file's settings.

References

Last updated: 20th August 2021
First published: 19th July 2021