How to Install and Manage Virtual Machines and Containers

Virtualization and containers are hot topics in today’s IT industry. In this article we will list the necessary tools to manage and configure both in Linux systems.

For many decades, virtualization has helped IT professionals to reduce operational costs and increase energy savings. A virtual machine (or VM for short) is an emulated computer system that runs on top of another system known as host.

VMs have limited access to the host’s hardware resources (CPU, memory, storage, network interfaces, USB devices, and so forth). The operating system running on the virtual machine is often referred to as the guest operating system.

CPU Extensions

Before we proceed, we need to check if the virtualization extensions are enabled on our CPU(s). To do that, use the following command, where vmx and svm are the virtualization flags on Intel and AMD processors, respectively:

# grep --color -E 'vmx|svm' /proc/cpuinfo

No output means the extensions are either not available or not enabled in the BIOS. While you may continue without them, performance will be negatively impacted.

Install Virtualization Tools in Linux

To begin, let’s install the necessary tools. In CentOS you will need the following packages:

# yum install qemu-kvm libvirt libvirt-client virt-install virt-viewer

whereas in Ubuntu:

$ sudo apt-get install qemu-kvm qemu virt-manager virt-viewer libvirt-bin libvirt-dev

Next, we will download a CentOS 7 minimal ISO file for later use:

# wget http://mirror.clarkson.edu/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1804.iso

At this point we are ready to create our first virtual machine with the following specifications:

  • RAM: 512 MB (Note that the host must have at least 1024 MB)
  • 1 virtual CPU
  • 8 GB disk
  • Name: centos7vm
# virt-install --name=centos7vm --ram=1024 --vcpus=1 --cdrom=/home/user/CentOS-7-x86_64-Minimal-1804.iso --os-type=linux --os-variant=rhel7 --network type=direct,source=eth0 --disk path=/var/lib/libvirt/images/centos7vm.dsk,size=8

Depending on the computing resources available on the host, the above command may take some time to bring up the virtualization viewer. This tool will enable you to perform the installation as if you were doing it on a bare metal machine.

How to Manage Virtual Machines in Linux

After you have created a virtual machine, here are some commands you can use to manage it:

List all VMs:

# virsh --list all

Get info about a VM (centos7vm in this case):

# virsh dominfo centos7vm

Edit the settings of centos7vm in your default text editor:

# virsh edit centos7vm

Enable or disable autostart to have the virtual machine boot (or not) when the host does:

# virsh autostart centos7vm
# virsh autostart --disable centos7vm

Stop centos7vm:

# virsh shutdown centos7vm

Once it is stopped, you can clone it into a new virtual machine called centos7vm2:

# virt-clone --original centos7vm --auto-clone --name centos7vm2

And that’s it. From this point on, you may want to refer to the virt-install, virsh, and virt-clone man pages for further info.

Gabriel Cánepa
Gabriel Cánepa is a GNU/Linux sysadmin and web developer from Villa Mercedes, San Luis, Argentina. He works for a worldwide leading consumer product company and takes great pleasure in using FOSS tools to increase productivity in all areas of his daily work.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

4 thoughts on “How to Install and Manage Virtual Machines and Containers”

  1. I want to create VM and join interface of the VM to a OVS bridge i.e. eth0 on VM and TAP interface on Bridge. Please let me know the commands

    Reply
    • @DarkSun,

      Deleting a VM Guest, also remove its configuration stored under /etc/ directory, along with its storage files.

      To remove VM, first list the running VM guests, once you the name of the VM, shutdown and delete using following commands.

      # virsh list
      # virsh shutdown VM_NAME
      # virsh undefine VM_NAME
      
      Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.