How to Create and Run a Virtual Machine on Linux
Table of Contents
Virtual machines allow you to run multiple operating systems on a single physical machine. This can be very useful for testing software, running legacy applications, or creating isolated environments for development or testing. In this article, we’ll show you how to create and run virtual machines on Linux using VirtualBox.
Using VirtualBox
VirtualBox is a free, open-source virtualization software developed by Oracle Corporation. It allows users to create and run virtual machines (VMs) on a single physical computer, enabling multiple operating systems to coexist and function simultaneously. VirtualBox is available for various host operating systems, including Windows, macOS, Linux, and Solaris.
VirtualBox provides a feature-rich, user-friendly interface that simplifies the process of creating, configuring, and managing virtual machines. It supports a wide range of guest operating systems, including various versions of Windows, Linux, BSD, and others. Some notable features of VirtualBox include:
- Snapshot functionality: Users can save the current state of a VM and revert to it later, allowing for easy testing and experimentation without the risk of damaging the virtual environment.
- Seamless mode: This feature allows for the seamless integration of guest operating system windows with the host desktop, improving the overall user experience.
- Shared folders: VirtualBox enables users to share folders between the host and guest systems, simplifying file exchange and improving overall workflow.
- USB support: VirtualBox can redirect USB devices from the host machine to the guest operating system, enabling the use of peripherals such as printers, flash drives, and webcams within VMs.
- Network options: Users can configure various network settings for VMs, including NAT, bridged networking, and internal networking, facilitating a wide range of network scenarios.
Overall, VirtualBox is a powerful, versatile, and easy-to-use virtualization solution suitable for personal, educational, and professional use.
Step 1: Open Terminal
Open the terminal by pressing Ctrl + Alt + T on your keyboard or by searching for it in the applications menu.
Step 2: Install VirtualBox
You can download the latest version of VirtualBox from the official website (https://www.virtualbox.org/). Once downloaded, you can install it using your package manager.
On Debian/Ubuntu-based distros, run:
sudo apt install virtualbox
On Fedora/RPM-based distros, run:
sudo dnf install VirtualBox
On Arch-based distros, run:
sudo pacman -S virtualbox
Step 3: Create VM
Once VirtualBox is installed, you can create a new virtual machine by clicking the “New” button in the main window. This will launch the New Virtual Machine wizard, which will guide you through the process of creating a new virtual machine. Here are the steps:
- Choose a name and operating system for your virtual machine.
- Set the amount of memory and CPU cores you want to allocate to the virtual machine.
- Create a new virtual hard disk or use an existing one.
- Set the size and format of the virtual hard disk.
Once you have completed the wizard, your new virtual machine will appear in the main VirtualBox window.
Step 4: Configure VM
Before you can start the virtual machine, you may need to configure some additional settings. Here are a few things you may want to configure:
Network: By default, the virtual machine will use the NAT network mode, which allows it to access the internet but not other devices on your network. If you want to access the virtual machine from other devices on your network, you can switch to the Bridged or Host-only network modes.
Shared folders: You can share folders between your host machine and the virtual machine by creating a shared folder in VirtualBox and then mounting it in the virtual machine.
Guest additions: Installing the VirtualBox Guest Additions in the virtual machine can improve performance and add additional features, such as better graphics and file sharing.
Step 5: Run VM
To start the virtual machine, simply select it in the VirtualBox window and click the Start button. This will launch the virtual machine in a new window, which will display the boot process of the operating system you installed. Once the operating system has booted, you can use it just like you would on a physical machine.
Using KVM, QEMU, and Virt-Manager
QEMU, KVM, and Virt-Manager are tools used for virtualization on Linux systems. They work together to create and manage virtual machines, allowing you to run multiple operating systems on a single physical machine. Here’s an overview of each tool:
- QEMU (Quick Emulator): QEMU is an open-source emulator and virtualization tool that can run various guest operating systems. It can emulate a wide range of hardware, from CPUs to peripheral devices. QEMU performs hardware virtualization by translating guest system calls to host system calls, allowing the guest OS to run on the host system.
- KVM (Kernel-based Virtual Machine): KVM is a virtualization technology built into the Linux kernel, allowing it to act as a hypervisor for virtual machines. KVM leverages hardware virtualization features available in modern CPUs (Intel VT-x and AMD-V) to provide near-native performance for guest operating systems. KVM works alongside QEMU to manage and run virtual machines efficiently.
- Virt-Manager (Virtual Machine Manager): Virt-Manager is a user-friendly graphical interface for managing virtual machines. It is built on top of libvirt, a library for interacting with various virtualization technologies, including QEMU and KVM. Virt-Manager simplifies the process of creating, configuring, and managing virtual machines, making it easier for users to work with virtualization on Linux systems.
Together, these tools provide a powerful and flexible solution for virtualization on Linux. QEMU and KVM handle the low-level aspects of virtualization, such as hardware emulation and virtual machine management, while Virt-Manager provides a convenient graphical interface for creating and managing virtual machines.
Step 1: Open Terminal
Open the terminal by pressing Ctrl + Alt + T on your keyboard or by searching for it in the applications menu.
Step 2: Install QEMU, KVM, and Virt-Manager
On Debian/Ubuntu-based distros, run:
sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager
On Fedora/RPM-based distros, run:
sudo dnf install qemu-kvm libvirt virt-install virt-manager
On Arch-based distros, run:
sudo pacman -S qemu virt-manager virt-viewer dnsmasq vde2 bridge-utils openbsd-netcat
Step 3: Start and Enable libvirtd Service
On Debian/Ubuntu-based distros and Fedora/RPM-based distros, run the following commands:
sudo systemctl start libvirtd
sudo systemctl enable libvirtd
On Arch-based distros, run the following commands:
sudo systemctl enable libvirtd.service
sudo systemctl start libvirtd.service
Step 4: Add User to libvirt Group
On Debian/Ubuntu-based distros, run the following commands:
sudo adduser your_username libvirt
sudo adduser your_username libvirt-qemu
Replace your_username with your username.
On Fedora/RPM-based distros and Arch-based distros, run:
sudo gpasswd -a your_username libvirt
Replace your_username with your username.
Step 5: Log Out and Log Back In
Log out and log back in for the changes to take effect.
Step 6: Create Virtual Machine
- Open Virt-Manager from your applications menu.
- Click on the Create a new virtual machine button or go to File > New Virtual Machine.
- Choose the installation method (local ISO file, network boot, or importing an existing disk image), and click Next.
- Browse and select the ISO file (if applicable), and click Next.
- Allocate the memory (RAM) and CPU resources for the virtual machine, and click Next.
- Specify the disk size and storage location for the virtual machine, and click Next.
- Review the virtual machine settings, give it a name, and click Finish.
The virtual machine will now be created, and you can start it by selecting it and clicking on the Start button (the Play icon) in Virt-Manager.
Conclusion
In conclusion, creating a virtual machine on Linux is a straightforward process that can be achieved using popular tools like VirtualBox or QEMU, KVM, and Virt-Manager. Whether you’re testing new software, running multiple operating systems simultaneously, or using virtualization for development purposes, these tools provide a powerful and flexible solution for your needs.