A virtual machine is a piece of software that emulates a real operating system. Being able to run Debian in a virtual machine is a great way to play around with it without having to worry about affecting your native system. This article will discuss how to install Debian in a QEMU KVM virtual machine using either Ubuntu or Debian.
Step 0. Make sure your CPU supports virtualization
(If you already know that your CPU supports virtualization, and how to enable it, you can skip this step.)
To test if your CPU supports virtualization, you can type either of the following commands
egrep --color=auto 'vmx|svm|0xc0f' /proc/cpuinfo
You will be looking for output like vmx, svm, or virtualization. vmx is for Intel and svm is for AMD
From my own computer, here is the output I get after typing in lscpu.
lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 8 On-line CPU(s) list: 0-7 Thread(s) per core: 2 Core(s) per socket: 4 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 60 Model name: Intel(R) Core(TM) i7-4710HQ CPU @ 2.50GHz Stepping: 3 CPU MHz: 1272.125 CPU max MHz: 3500.0000 CPU min MHz: 800.0000 BogoMIPS: 4988.38 Virtualization: VT-x L1d cache: 32K L1i cache: 32K L2 cache: 256K L3 cache: 6144K NUMA node0 CPU(s): 0-7 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm epb tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts
If you get output like: VT-x and or/vmx, like on my computer, then you will know that your OS supports virtualization. It will be similar if your computer uses an AMD processor.
From the previously mentioned egrep command, if you get output in color, like for example getting vmx, then you will know that your OS supports virtualization.
To enable virtualization, you will probably need to open your BIOS, and explicitly allow virtualization.
You can read more about enabling virtualization with kvm below
Step 1: Install QEMU and KVM
Now, we simply install qemu and qemu-kvm. Open a terminal and type:
sudo apt-get install qemu qemu-kvm
Step 2: Get a Debian ISO file
Download the latest Debian ISO, or use one your already have. Currently, the latest version of Debian is here. Because I have a fairly new computer with an Intel processor, I will download the amd64 version which is debian-9.6.0-amd64-xfce-CD-1.iso
Step 3: Create Setup For QEMU and KVM
Now is where we actually setup kvm to use for emulating Debian.
Pick any directory, and make a folder. I will assume you will make a folder inside of your home directory called debian. Though, you can actually call the folder whatever you want, and place it wherever you want, just make sure that you adjust the steps.
Inside of the debian folder is where we will put our ISO file, and have our qemu/kvm files.
Find where you downloaded the Debian ISO file and put it inside of the ~/debian folder you just made. I will assume that you downloaded it inside of the ~/Downloads folder.
mv debian-9.6.0-amd64-xfce-CD-1.iso ~/debian/
Step 4: Change Into the ~/debian directory and make a QEMU image file
Now that we have the Debian ISO file and the ~/debian folder ready, we will actually start doing QEMU stuff. The next thing we will
do is to create a QEMU image file. This is what will contain your virtual machine. It will basically be like a virtual hard drive.
The command we will be using is called qemu-img
With this command we will create an image file called virtualdebian.img and will give it 30G of hard drive space. You can change the name and amount of space you will give it though.
qemu-img create -f qcow2 virtualdebian.img 30G
That step should only take a few seconds.
Step 5: Emulate Opening The Debian ISO File As a CD-ROM/DVD Drive And Install Debian In The Virtual Machine
This is the step which will take the longest and where you will actually install Debian in the virtual machine. For this step it is very important to input the command correctly, otherwise you may spend hours trying to debug. You can just copy and paste the command that I will show you below. The command we will use is the kvm command with lots of options. The first part of the command, kvm -hda virtualdebian.img, tells kvm that the virtual image is virtualdebian.img which we created earlier. The -m 2048 option tells kvm that we are giving it 2GB of RAM. -soundhw all option makes all sound available in the virtual machine. You can read the kvm manual if you want to learn about the other options. Now, simply copy and paste the following into your terminal.
kvm -hda virtualdebian.img -cdrom debian-9.6.0-amd64-xfce-CD-1.iso -m 2048 -net nic -net user -soundhw all
Note, you made need to add
sudo to the above command if it complains about permissions.
If all goes well, the Debian should load like if you were loading Debian from a real DVD or CD on your own computer. You will now install Debian as if you were installing it on your own computer as the main OS, although you will actually be installing it in the virtual machine.
After you have finished installing Debian, to turn off the virtual machine, simply shut down the Debian virtual machine as if it were your main OS (though don’t turn off your own main computer). You will only be turning off the virtual machine.
Step 6: Run The Debian Virtual Machine As Often As You Want
All of the hard work has now been done. You have installed Debian in the image file. Now, from now on, all you need to do to run your Debian Virtual machine is to run the following command.
kvm -soundhw all -m 2048 -hda ~/debian/virtualdebian.img
Note, if you get output like the following after running the above command:
Could not access KVM kernel module: Permission denied qemu-system-x86_64: failed to initialize KVM: Permission denied
then you will need to add
sudo to the above kvm command.
When you are done with using the Debian virtual machine, make sure to always turn it off properly.
If you tire of using the virtual machine, or think you made a mistake, you can always redo the above steps. You can delete the virtualdebian.img file, and then create a new one following all the other steps.
You can actually replace the
kvm command with
qemu-system-x86_64, but you shouldn’t do it if kvm is available. QEMU is MUCH slower than using KVM, and your CPU will work much harder using QEMU. So, make sure you use KVM if possible.
Did you like this article? Do you have anything to add? Let’s discuss it in the comments below.