linuxconfig.org
Open in
urlscan Pro
2606:4700:3108::ac42:2b5c
Public Scan
URL:
https://linuxconfig.org/how-to-create-and-manage-kvm-virtual-machines-from-cli
Submission: On June 24 via manual from IL — Scanned from DE
Submission: On June 24 via manual from IL — Scanned from DE
Form analysis
1 forms found in the DOMGET https://linuxconfig.org/
<form method="get" class="search-form navigation-search" action="https://linuxconfig.org/">
<input type="search" class="search-field" value="" name="s" title="Search">
</form>
Text Content
WE VALUE YOUR PRIVACY We and our partners store and/or access information on a device, such as cookies and process personal data, such as unique identifiers and standard information sent by a device for personalised ads and content, ad and content measurement, and audience insights, as well as to develop and improve products. With your permission we and our partners may use precise geolocation data and identification through device scanning. You may click to consent to our and our partners’ processing as described above. Alternatively you may access more detailed information and change your preferences before consenting or to refuse consenting. Please note that some processing of your personal data may not require your consent, but you have a right to object to such processing. Your preferences will apply to this website only. You can change your preferences at any time by returning to this site or visit our privacy policy. MORE OPTIONSAGREE Skip to content Menu * Ubuntu * Ubuntu 20.04 Guide * Ubuntu 18.04 * Debian * Redhat / CentOS / AlmaLinux * Fedora * Kali Linux Menu Menu * Linux Tutorials * System Admin * Programming * Multimedia * Forum * Linux Commands * Linux Jobs HOW TO CREATE AND MANAGE KVM VIRTUAL MACHINES FROM CLI 26 July 2018 by Egidio Docile OBJECTIVE Learn how to create and manage KVM virtual machines from command line OPERATING SYSTEM AND SOFTWARE VERSIONS * Operating System: – All Linux distributions REQUIREMENTS * Root access * Packages: * qemu-kvm – The main package * libvirt – Includes the libvirtd server exporting the virtualization support * libvirt-client – This package contains virsh and other client-side utilities * virt-install – Utility to install virtual machines * virt-viewer – Utility to display graphical console for a virtual machine DIFFICULTY MEDIUM CONVENTIONS * # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command * $ – requires given linux commands to be executed as a regular non-privileged user INTRODUCTION Knowing how to create and manage KVM virtual machines from command line can be really useful in certain scenarios: when working on headless servers, for example. Nonetheless, being able to script interactions with virtual machines can greatly improve our productivity. In this tutorial you will learn how to create, delete, clone and manage KVM machines with the help of few utilities. -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- SOME TERMINOLOGY Before we start working, it would be useful to define what KVM and Qemu are and how they interact. KVM stands for Kernel Virtual Machine, and it is a module of the Linux kernel which allows a program to access and make use of the virtualization capabilities of modern processors, by exposing the /dev/kvm interface. Qemu is, instead, the software which actually performs the OS emulation. It is and open source machine emulator and virtualizer which can use the acceleration feature provided by KVM when running an emulated machine with the same architecture of the host. PRELIMINARY SETUP First thing we have to do, is to check that the CPU we are using has support for virtualization. Unless you are running on a very old machine, this will surely be the case, but to verify it we simply run: $ cat /proc/cpuinfo Scroll down the output of the command above until you see the list of CPU ‘flags’: among them you should see svm if you are using an Amd processor, or vmx if the CPU vendor is Intel. The second thing we have to do, is to make sure that the needed kernel modules have been loaded, to check this, we run: # lsmod | grep kvm kvm_intel 200704 0 kvm 598016 1 kvm_intel irqbypass 16384 1 kvm -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- I’m running on an Intel CPU, therefore, in addition to the kvm module, also the kvm_intel one has been loaded. If you are using an Amd processor, the kvm_amd module will be loaded instead. If the modules are not loaded automatically, you can try to load them manually by using the modprobe command: # modprobe kvm_intel Finally, we have to start the libvirtd daemon: the following command both enables it at boot time and starts it immediately: # systemctl enable --now libvirtd CREATE THE NEW VIRTUAL MACHINE Now that we installed and started the libvirtd service, we can use the virt-install command to setup our virtual machine. The syntax of the program is really straightforward. The following linux command must be executed as root, or, if you want to launch it as a normal user, as a member of the kvm group. The syntax of the program is the following: # virt-install --name=linuxconfig-vm \ --vcpus=1 \ --memory=1024 \ --cdrom=/tmp/debian-9.0.0-amd64-netinst.iso \ --disk size=5 \ --os-variant=debian8 Let’s analyze the command above: First of all we used the --name option: this is mandatory and it is used to assign a name to the new virtual machine. The next option is the --vcpus one. We use it to specify the number of virtual cpus to configure for the guest. The --memory option is used to select the amount of memory reserved for the guest machine in MiB and --cdrom lets us specify the path to a file or a device to be used as a virtual CD-ROM: it can be an ISO image, a CDROM device or a URL from which to access a boot ISO image. -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- The --disk flag is used to configure the media storage for the guest. Various comma-separated options can be specified, for example: size which is used to specify the size of the virtual disk in GB and path which is used to specify a path to use for the disk (it will be created if doesn’t already exists). If this options is specified, you must make sure that the target path is accessible and has the right SELinux context (to know more about SELinux you can read this article). If the path option is not specified, the disk will be created in $HOME/.local/share/libvirt/images if the command is executed as normal user (member of the kvm group) or in /var/lib/libvirt/images if running it as root. Next we passed the --os-variant option. While this is not mandatory, is highly recommended to use it, since it can improve performance of the virtual machine. The option will try to fine tune the guest to the specific OS version. If the option is not passed, the program will attempt to auto-detect the correct value from the installation media. To obtain a list of all supported systems you can run: $ osinfo-query os If everything went well and the virt-viewer package is installed, a window will appear showing the guest OS installer. THE VIRSH UTILITY The virsh utility can be used to interact with virtual machines. For example, say you want to list all configured guests, using virsh you can simply run: # virsh list --all The output will show the id, name and state of all the configured guests, whether they are running or not. But what if you want to change some guest machine parameters? You can use virsh to accomplish this task, for example: # virsh edit linuxconfig-vm Here is a screenshot of the command output: -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- As you can see the output is an xml representation of the virtual machine properties, or, using virsh terminology, a domain. If you want to change, for example, the number of vcpus, you just have to find the relevant tag and change the value. In this case, we have: <vcpu placement='static'>1</vcpu> We want to add 1 vcpu, so we will change it to: <vcpu placement='static'>2</vcpu> All we have to do now, is just to reboot the virtual machine for the settings to be applied: # virsh reboot linuxconfig-vm If we now run lscpu in the guest console, we should see the increased number of cpus: The virsh command can also be used to do other common operations: for example, virsh shutdown can be used to shut down the guest, virsh destroy is the equivalent of a brute force shutdown (therefore it can be dangerous) and virsh undefine can be used to delete a guest machine (to undefine a domain). AUTOSTART A VIRTUAL MACHINE ON BOOT You can take advantage of the virsh command also if you want certain guests to be started automatically when the host system boots: the syntax it’s, again, very intuitive: # virsh autostart linuxconfig-vm To disable this option, we run: # virsh autostart --disable linuxconfig-vm -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- CLONING A GUEST Another utility, virt-clone can be used to create a new virtual machine by cloning an existing one. To proceed, we must first ensure that the guest to be cloned is down, than we run: virt-clone \ --original=linuxconfig-vm \ --name=linuxconfig-vm-clone \ --file=/var/lib/libvirt/images/linuxconfig-vm.qcow2 What we have here is very simple to understand: we specified the guest to be cloned using the --original option and the name of the new guest using --name as if we were installing it from scratch. With the --file option, instead, we reference all the virtual hard disks associated with the original guest that we want to clone. The program will do its job, and, if successful, will create a new domain named linuxconfig-vm-clone. We already know how to verify it: # virsh list --all Id Name State ---------------------------------------------------- - linuxconfig-vm shut off - linuxconfig-vm-clone shut off FINAL THOUGHTS In this tutorial we configured a new virtual machine, and we saw how to interact with it. The options we specified at creation time, are just the minimal needed for a working setup. A lot of other options can be used to adjust several aspects of the guest machine and they are really well described in the virt-install manpage. As always, the best possible advice is: read the manual. RELATED LINUX TUTORIALS: * PDF viewer list on Ubuntu 22.04 Jammy Jellyfish Linux * PDF viewer list on Ubuntu 20.04 Focal Fossa Linux * How to run the Raspberry Pi Os in a virtual machine with… * How to work with dnf package groups * How to use bridged networking with libvirt and KVM * Things to install on Ubuntu 20.04 * Okular PDF viewer installation on Ubuntu 20.04 Focal Fossa * Install And Set Up KVM On Ubuntu 20.04 Focal Fossa Linux * Access and modify virtual machines disk images with… * How to setup a OpenVPN server on Ubuntu 20.04 Categories System Administration Post navigation How to Install The Latest Mesa Version On Debian 9 Stretch Linux Protect Your System. Run Your Browser In Firejail -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- Comments and Discussions -------------------------------------------------------------------------------- NEWSLETTER Subscribe to Linux Career Newsletter to receive latest news, jobs, career advice and featured configuration tutorials. SUBSCRIBE WRITE FOR US LinuxConfig is looking for a technical writer(s) geared towards GNU/Linux and FLOSS technologies. Your articles will feature various GNU/Linux configuration tutorials and FLOSS technologies used in combination with GNU/Linux operating system. When writing your articles you will be expected to be able to keep up with a technological advancement regarding the above mentioned technical area of expertise. You will work independently and be able to produce at minimum 2 technical articles a month. APPLY NOW CONTACT US web ( at ) linuxconfig ( dot ) org TAGS 18.04 administration apache applications bash beginner browser centos centos8 commands database debian desktop development docker fedora filesystem firewall gaming gnome Hardware installation java kali manjaro multimedia mysql networking nvidia programming python redhat rhel8 scripting security server ssh storage terminal ubuntu ubuntu 20.04 video virtualization webapp webserver FEATURED TUTORIALS * Ubuntu 22.04 * How to install the NVIDIA drivers on Ubuntu 20.04 Focal Fossa Linux * Bash Scripting Tutorial for Beginners * How to check CentOS version * How to find my IP address on Ubuntu 20.04 Focal Fossa Linux * Ubuntu 20.04 Remote Desktop Access from Windows 10 * Howto mount USB drive in Linux * How to install missing ifconfig command on Debian Linux * AMD Radeon Ubuntu 20.04 Driver Installation * Ubuntu Static IP configuration * How to use bash array in a shell script * Linux IP forwarding – How to Disable/Enable * How to install Tweak Tool on Ubuntu 20.04 LTS Focal Fossa Linux * How to enable/disable firewall on Ubuntu 18.04 Bionic Beaver Linux * Netplan static IP on Ubuntu configuration * How to change from default to alternative Python version on Debian Linux * Set Kali root password and enable root login * How to Install Adobe Acrobat Reader on Ubuntu 20.04 Focal Fossa Linux * How to install the NVIDIA drivers on Ubuntu 18.04 Bionic Beaver Linux * How to check NVIDIA driver version on your Linux system * Nvidia RTX 3080 Ethereum Hashrate and Mining Overclock settings on HiveOS Linux LATEST TUTORIALS * GNOME login as root * Check GNOME version * How to check battery life on Ubuntu * How to install Monero Wallet on Linux (GUI & CLI) * How to install and configure Starship on Linux * ssh_exchange_identification read connection reset by peer * 3 Methods to install PyCharm Community Edition on Linux * How to manage git repositories with Python * Introduction to terminal multiplexer Tmux * How to manage Vim plugins natively * Ubuntu 22.04 on WSL (Windows Subsystem for Linux) * Ubuntu 22.04 not booting: Troubleshooting Guide * Ubuntu 22.04 Enable full disk encryption * How to mount a Samba shared directory at boot * How to define a custom Firewalld zone * Ubuntu 22.04 Change login screen background * Things to install on Ubuntu 22.04 * Ubuntu 22.04 GPG error: The following signatures couldn’t be verified * Ubuntu 22.04 list services * Ubuntu 22.04 NTP server © 2022 TOSID Group Pty Ltd - LinuxConfig.org Close * Linux Tutorials * System Admin * Programming * Multimedia * Forum * Linux Commands * Linux Jobs