Use this file to discover all available pages before exploring further.
Previously, a minimal virtual machine was created without an attached virtual disk, meaning it couldn’t host an operating system. That setup was useful for demonstrating basic management commands using bash. In this guide, we will build a complete virtual machine—with an operating system installed—to simulate real-world scenarios and introduce additional commands.
The first step is to download a disk image. Ubuntu provides pre-configured cloud images that are ideally suited for virtual machines. Although similar images power services like Google Cloud, we will download the minimal cloud image using wget.Before you proceed, note that you may have encountered alternative commands similar to:
To ensure that the downloaded image is authentic and uncorrupted, verify its checksum using the SHA-256 checksum provided on the Ubuntu page. Two files are released: SHA256SUMS (which contains checksums for all images) and SHA256SUMS.gpg (which is used to verify the checksum file itself). First, download the checksum file:
Although the virtual disk is set to 3.5 GiB, only 227 MiB is currently allocated. To install additional software, a larger disk is necessary. Expand the virtual disk to approximately 10 GiB with:
Creating a Storage Pool and Copying the Disk Image
Virtualization tools typically use storage pools to store virtual disk images, snapshots, and other data. By default, the storage pool is located at /var/lib/libvirt. To confirm, check the directory contents:
jeremy@kodekloud:~$ ls /var/lib/libvirt/boot dnsmasq images qemu sanlock
Then, copy the disk image to the images subdirectory:
jeremy@kodekloud:~$ sudo cp ubuntu-24.04-minimal-cloudimg-amd64.img /var/lib/libvirt/images/[sudo] password for jeremy:
Now comes the more advanced step: creating a virtual machine and specifying the image file as the virtual disk. The virt-install utility makes this straightforward. If parameters are missing, an error will appear indicating that required options (such as the operating system name) must be provided.For example, running:
jeremy@kodekloud:~$ virt-installERROR--os-variant/--osinfo OS name is required, but no value was set or detected....
This error reminds you to specify the OS name. To list available operating system variants, use:
jeremy@kodekloud:~$ virt-install --osinfo list
Scroll through the output to locate the variant you plan to use (in this case, Ubuntu 24.04) and note its identifier (for example, ubuntu24.04).
Use the following complete command to create a VM running Ubuntu 24.04 with 1024 MiB memory, 1 vCPU, and the pre-copied cloud image while disabling graphical output:
When you run the command, you may observe output similar to this:
WARNING Requested memory 1024 MiB is less than the recommended 3072 MiB for OS ubuntu24.04Starting install...Creating domain...Running text console command: virsh --connect qemu:///system console ubunt1Connected to domain 'ubunt1'Escape character is ^] (Ctrl + })...Domain creation completed.
This output indicates that you are connected directly to the VM’s text console—similar to having a monitor and keyboard attached. To exit the console, press Control + the closing square bracket (Ctrl + ]).To list all virtual machines, run:
jeremy@kodekloud:~$ virsh list --all Id Name State------------------------ 1 ubunt1 runningjeremy@kodekloud:~$
Shut down the VM by running:
jeremy@kodekloud:~$ virsh shutdown ubunt1Domain 'ubunt1' is being shutdownjeremy@kodekloud:~$ virsh list --all Id Name State------------------------ - ubunt1 shut off
If necessary, force a shutdown using:
jeremy@kodekloud:~$ virsh destroy ubunt1
Once shut down, you can remove the VM’s definition and all associated storage (including the copied image):
jeremy@kodekloud:~$ virsh undefine ubunt1 --remove-all-storageDomain 'ubunt1' has been undefinedVolume 'vda'(/var/lib/libvirt/images/ubuntu-24.04-minimal-cloudimg-amd64.img) removed.
The Ubuntu cloud image includes cloud-init automation scripts that allow you to set a root password automatically. To recreate the VM with cloud-init that generates a random root password, append the --cloud-init root-password-generate=on option.
jeremy@kodekloud:~$ virt-install --osinfo ubuntu24.04 --name ubunt1 --memory 1024 --vcpus 1 --import --disk /var/lib/libvirt/images/ubuntu-24.04-minimal-cloudimg-amd64.img --graphics none --cloud-init root-password-generate=onWARNING Requested memory 1024 MiB is less than the recommended 3072 MiB for OS ubuntu24.04Starting install...Password for first root login is: wMgdynhWmsdluzjHInstallation will continue in 10 seconds (press Enter to skip)...
Once the installation begins and the VM boots, you will eventually see a login prompt on the console. The boot output (including kernel messages and cloud-init logs) will scroll until you reach:
Ubuntu 24.04 LTS ubuntu ttyS0ubuntu login:
Log in using “root” as the username, and paste the generated password (note that nothing appears as you paste). On first login, you will be required to change the password:
You are required to change your password immediately (administrator enforced).Changing password for root.Current password:
After updating the password, you’ll be logged into the Ubuntu operating system running on your virtual machine. Verify the virtual disk size is approximately 10 GiB:
Then update the package lists to ensure network connectivity:
root@ubuntu:~# apt update...Fetched 24.5 MB in 11s (2206 kB/s)Reading package lists... DoneBuilding dependency tree... DoneReading state information... Done17 packages can be upgraded. Run 'apt list --upgradable' to see them.
To exit the VM’s console, press Control + ].You can reconnect at any time with:
Sometimes the operating system inside a virtual disk image may be a newer or unlisted variant in the virt-install OS database. Here are some options to handle such cases:
Use --osinfo detect=on to let virt-install automatically determine the OS type
Use --osinfo detect=on to let virt-install automatically determine the OS type.Example:
Inspecting and resizing the virtual disk using QEMU
Setting up a storage pool by copying the image to /var/lib/libvirt/images/
Creating a virtual machine with virt-install, including essential options such as name, memory, vCPUs, disk, and graphics
Utilizing the --import option to deploy a guest from an existing disk image
Incorporating cloud-init to automatically generate a root password
Connecting to and managing the VM console using virsh
With these steps, you now have a fully functional virtual machine running Ubuntu 24.04. In the next lesson, we’ll explore additional configuration and management options.