Ubuntu Server 18.04 – Managing virtual machines via the command line

Install PHP on CentOS 8

In this chapter, I showed you how to manage virtual machines with virt-manager. This is great if you have a secondary machine with a graphical user interface running Linux as its operating system. But what do you do if such a machine isn’t available, and you’d like to perform simple tasks such as rebooting a virtual machine or checking to see which virtual machines are running on the server?

On the virtual machine server itself, you have access to the virsh suite of commands, which will allow you to manage virtual machines even if a GUI isn’t available. To use these commands, simply connect to the machine that stores your virtual machines via SSH. What follows are some easy examples to get you started. Here’s the first one:

virsh list
Showing running virtual machines with the virsh list command

With one command, we were able to list which virtual machines are running on the server. In the example screenshot, which is taken from my very own KVM server, you can see that I have three virtual machines running there. If you’d also like to see non-running instances, simply add the --all option to the command.

We can manage the state of our virtual machines with any of the following commands:

virsh start my_vm
virsh shutdown my_vm
virsh suspend my_vm
virsh resume my_vm
virsh destroy my_vm

The command syntax for virsh is extremely straightforward. By looking at the previous list of commands, you should be able to glean exactly what they do. The virsh commands allow us to do things such as start, shutdown, suspend, and resume a virtual machine. The last command is very destructive, as we’d use it when we want to permanently delete a VM from our server. You should be VERY careful with that command.

That’s not all virsh can do, however. We can actually create a virtual machine with the virsh suite of commands as well. Learning how to do so is a good idea if you don’t use Linux as your workstation operating system, or you don’t have access to virt-manager for some reason. However, manually creating VM disk images and configuration is outside the scope of this chapter. The main goal is for you to familiarize yourself with managing VMs via virsh, and these simple basics will allow you to expand your knowledge further.

Comments are closed.