Virtualising Linux machines is an important skill and tool for any modern Linux user, coder or sysadmin. Virtual environments with isolated Linux distros are excellent for testing all manner of things that may be either dangerous or damaging to your distro, or just might react differently in various versions of Linux.
Creating virtual machines (VMs) can be simple if you go for some of the more basic methods, but there’s a lot more that you can do with them than just install an alternative distro. You can use these techniques to create a virtual version of an old operating system so that you can keep using some specific software, or for a more private online environment.
In this feature we’re going to explore three of the best ways to achieve this on Linux. The ever-popular VirtualBox, the very powerful QEMU and the newer KVM virtualisation software built into the Linux kernel.
Tools for virtualisation
Useful for: Emulation and virtualisation on servers for more dedicated VMs
KVM is the set of virtualisation modules that are built into the Linux kernel – not a keyboard and mouse switch. The benefit of having KVM built into Linux itself is that it makes it easier to turn it into a dedicated hypervisor, maybe the kind you’d use on a server, without requiring extra layers of packages and other software on top of it. It’s quite quick to set up, especially via a graphical method, and has some good basic functionality between host and guest. It can be used on the command line very easily if you plan to have a headless server.
Useful for: Testing software, development or any kind of activity where you’d like to use a particular environment on your desktop
VirtualBox is very popular among home users. It’s also very simple to use, which is probably why it’s so popular. We use it all the time for testing, and it’s great for creating a separate distro or versions of a distro for installing software in that’s in development to see how it works under different conditions. It doesn’t quite have the efficiency of KVM as it runs slightly different within the operating system, but it’s much easier to use from the host system as you work on multiple things inside and outside the guest. Get it now from bit.ly/1nlDA5d.
Useful for: Testing and developing operating systems that run on different architectures
QEMU isn’t so much virtualisation software as it is an emulator to run images off of. You can still use ISOs and run Linux distros or Windows like any of the other VM solutions in this feature, however unlike those systems you can also run distros that require ARM (such as Raspberry Pi distros), MIPS, or any other kind of processor you can think of. This is excellent for testing out software on different architectures if you don’t have a relevant device handy. It can be a little tricky to use though as it relies more on a command line interface.
Start with VirtualBox
Learn how to make quick and very useful virtual machines with the easy-to-use VirtualBox.
To create your first VirtualBox VM, open the program and click New. Set the Type and Version as close to your guest distro as possible – for example, use a Debian base for a Parsix installation. Next, you assign RAM to your VM; this will vary depending on how many simultaneous guests you plan to run, but as a general rule assign half your RAM to a guest if you plan to run just the one. Finally, you need to create a virtual hard drive. Unless you need to work with other software, like VMWare, stick with the default VDI format. Create your hard drive at a Fixed Size, rather than Dynamically Allocated, if you can; the former is slower to set up but can be faster in use. Set the size of the hard drive to a reasonable amount – plenty for the OS with spare space left over for the programs and files you’ll need. About 50 GB should be more than enough.
With the hard drive set up, your VM is almost ready. Click Settings and go to the System page. As well as adjust your RAM setting, you can change the default boot order on the first tab. Click the Processor tab and you can set how many of your processor’s cores to assign to the virtual machine – give half to your guest if it will be the only VM running at a time. Now head to the Display page and give yourself as much Video Memory as you are recommended (ie the green area of the slider). Once you’ve installed the Guest Additions (see below), enable the 3D Acceleration and 2D Video Acceleration options here for better performance. Next, head to Storage and make sure your optical drive, real or virtual (eg via Virtual CloneDrive), is connected to the guest by adding it as the IDE Secondary Master, then clicking the disc icon and selecting your drive. Sometimes, you may find that your Internet connection doesn’t work properly to start with. If that’s the case, head to the Network settings and change your adapter (which defaults to NAT) to Bridged instead. With everything set up, click Start to boot the machine and begin installing your distro of choice.
Once you’re done and inside your virtualised OS, you’ll probably want to install the Guest Additions. This enables you to go full-screen with your VM at a proper resolution, and it will also enable mouse pointer integration, better video support, seamless windows and shared folders between guest and host, a shared clipboard and a few more features besides. To install them, go to the main Devices menu and choose ‘Insert Guest Additions CD Image’. Mount the disc, if it isn’t mounted automatically, then cd into the folder. From there, run ./VBoxLinux Additions.run as root. If the installation doesn’t work correctly (eg you still can’t display at full-screen), ensure your distro is fully up to date and then install both the dkms and virtualbox-guest-dkms packages before retrying. Finally, if you want to use USB devices inside your guest then first ensure you have the Guest Additions installed. Next, head to your VM’s Settings, click USB and enable the controllers, then click the Add Filter button to the right and add your device to the list of filters. To finish, open your distro’s Users & Groups settings, scroll down to vboxusers and then add your user to that group.
Virtualise with QEMU
Get to grips with this more manual virtualisation software that has a lot more controllable elements and a little more power.
QEMU is not so much a virtual machine software as it is an emulator, allowing you to run operating systems and Linux distros on it by emulating a processor. It doesn’t just emulate your normal processor, though; it allows you to run on x86/x64 systems while giving you access to an emulated ARM, MIPS and other processor styles. This is excellent for development and testing, but it’s also a bit trickier than other forms of virtualisation and doesn’t allow you to quite install like you can with VirtualBox or KVM. However, you can more easily run an image of a hard drive with it.
1. Get QEMU
First we need to install QEMU on your system – it’s supported by most Linux distros in their repositories, so in Ubuntu you would install it with something like:
$ sudo apt-get install qemu
For Fedora it would be yum/dnf install, etc. If you’re a sysadmin, though, you probably want to build it from source to make sure it works best on your system – the source is available from the QEMU website (wiki.qemu.org/Download).
2. Create a virtual hard drive
You can create some virtual space to use while emulating the different operating systems and distros, which is useful for storing and saving data, and you can even install to it if you wish. In the terminal you can do this by using:
$ sudo qemu-img create vhd.img 20G
This creates a 20 GB virtual hard drive, although you can make it as large or as small as you want. You should probably also keep it in a working directory.
3. Emulate x86
We can emulate our first operating system on x86. Make sure you have an ISO downloaded, then put it inside the qemu working directory to make launching easier. Once that’s done, go back to the terminal and use:
$ sudo qemu-system-i386 -hda vhd.img -cdrom
[ISO].iso -m 2048 -boot d
… with ISO being the name of the ISO you’re using. It might be a little slower than the other virtualisation solutions because it’s actually emulating the system, but it will work.
4. Install on virtual hard drive
Go through the normal installation procedure for your distro (make sure you made a big enough virtual hard drive for it – the 20 GB we created should be plenty). Again, it will take a little longer than usual. Once it’s installed and you do a proper shutdown, you can turn it back on again using:
$ sudo qemu-system-i386 -hda vhd.img -m 2048
This will then boot straight from the hard drive, without needing the CD.
5. Emulate ARM
This can be used to emulate the Raspberry Pi for Raspbian, among other things, and is a good way to test out software that will run on ARM versions of other distros if you don’t have quick access to a device. This requires a little change of the initial run command to something like:
$ sudo qemu-system-arm
… with the relevant image and RAM details (the -m option) to suit the system.
6. Emulate MIPS
Similar to emulating ARM, this allows you to test things out on MIPS hardware. It’s supported on QEMU along with many other forms of CPU architecture, and for this you simply need to use the qemu-system-mips command to run an image or operating system. There’s also support for MIPSEL and MIPS64 if you so need; just change the command to qemu-system-mipsel and qemu-system-mips64, respectively.