Run Amazon Linux 2 on KVM using Qcow2 image

Amazon Linux 2 is an operating system created and optimized specifically by Amazon for use in Amazon Web Services (AWS) Cloud platform. Amazon Linux 2 is designed with security focus in mind, to be stable, and fit for high-performance execution environment where you can develop and run cloud applications. You can run Amazon Linux 2 at no additional charge. AWS is responsible for the provision of ongoing security and maintenance updates for Amazon Linux 2.

In this article we shall discuss on how you can run Amazon Linux 2 on KVM virtualization platform. We won’t be performing the installation from ISO file but rather create a running instance using provided Qcow2 image. Before you can install Amazon Linux 2 on KVM, you need KVM installed and configured on a system with CPU virtualization extension enabled.

Refer to our guides below on how to install KVM and configure it.

Download Amazon Linux 2 Qcow2 for KVM

Download latest Qcow2 image to your local system with the commands below.

wget https://cdn.amazonlinux.com/os-images/2.0.20250113.0/kvm/amzn2-kvm-2.0.20250113.0-x86_64.xfs.gpt.qcow2

Check file format it should show as QEMU QCOW.

$ file amzn2-kvm-2.0.20250113.0-x86_64.xfs.gpt.qcow2
amzn2-kvm-2.0.20250113.0-x86_64.xfs.gpt.qcow2: QEMU QCOW Image (v3), 26843545600 bytes (v3), 26843545600 bytes

Create directory on your KVM host that will contain Virtual Machine templates.

sudo mkdir /var/lib/libvirt/images/templates

Let’s move the image downloaded to created directory

 sudo mv amzn2-kvm-2.0.20250113.0-x86_64.xfs.gpt.qcow2 /var/lib/libvirt/images/templates/amzn2-template.qcow2

Listing directory contents should show amzn2-template.qcow2 available.

$ ls /var/lib/libvirt/images/templates/
amzn2-template.qcow2

Run Amazon Linux 2 on KVM using Qcow2 image

Set the name of the virtual machine to be created.

export VM_NAME="Amazon-Linux-2"

Convert template we created into Virtual Machine image.

sudo qemu-img convert \
  -f qcow2 \
  -O qcow2 \
  /var/lib/libvirt/images/templates/amzn2-template.qcow2 \
  /var/lib/libvirt/images/$VM_NAME-root-disk.qcow2

Check if the file was created inside /var/lib/libvirt/images directory.

$ file  /var/lib/libvirt/images/$VM_NAME-root-disk.qcow2
/var/lib/libvirt/images/Amazon-Linux-2-root-disk.qcow2: QEMU QCOW Image (v3), 26843545600 bytes (v3), 26843545600 bytes

Check the virtual disk size of the image:

$ qemu-img info /var/lib/libvirt/images/$VM_NAME-root-disk.qcow2
image: /var/lib/libvirt/images/Amazon-Linux-2-root-disk.qcow2
file format: qcow2
virtual size: 25 GiB (26843545600 bytes)
disk size: 1.28 GiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
    extended l2: false
Child node '/file':
    filename: /var/lib/libvirt/images/Amazon-Linux-2-root-disk.qcow2
    protocol type: file
    file length: 1.28 GiB (1374945280 bytes)
    disk size: 1.28 GiB

You can extend to a higher value depending on your needs.

# I'm setting mine to 30GB - set yours accordingly
export VM_ROOT_DISK_SIZE=40G

# Resize Debian 11 VM disk
sudo qemu-img resize \
  /var/lib/libvirt/images/$VM_NAME-root-disk.qcow2 \
  $VM_ROOT_DISK_SIZE

The output should look similar to below.

Image resized.

We can confirm the new virtual disk size.

$ qemu-img  info /var/lib/libvirt/images/$VM_NAME-root-disk.qcow2
image: /var/lib/libvirt/images/Amazon-Linux-2-root-disk.qcow2
file format: qcow2
virtual size: 30 GiB (32212254720 bytes)
disk size: 1.28 GiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
    extended l2: false
Child node '/file':
    filename: /var/lib/libvirt/images/Amazon-Linux-2-root-disk.qcow2
    protocol type: file
    file length: 1.28 GiB (1374945792 bytes)
    disk size: 1.28 GiB

Choose a network to use while creating a Virtual Machine on your KVM host.

$ sudo virsh net-list
 Name              State    Autostart   Persistent
----------------------------------------------------
 bridged-network   active   yes         yes
 default           active   yes         yes

Save network to variable

export NET=bridged-network

With the network identified we can proceed to provision the operating system. Change values required respectively.

sudo virt-install \
    --memory 4096 \
    --vcpus 2 \
    --name $VM_NAME \
    --disk /var/lib/libvirt/images/$VM_NAME-root-disk.qcow2,device=disk,bus=virtio,format=qcow2 \
    --os-variant centos7.0 \
    --network network=$NET,model=virtio \
    --virt-type kvm \
    --graphics none \
    --import

VM installation should begin shortly.

Starting install...
Creating domain...                                                                                                                                      |    0 B  00:00:00     
Running text console command: virsh --connect qemu:///system console Amazom-Linux-2
Connected to domain 'Amazom-Linux-2'
Escape character is ^] (Ctrl + ])

The VM should initialize and install:

...
[  OK  ] Started Notify NFS peers of a restart.
[  OK  ] Started System Logging Service.
[  OK  ] Started Permit User Sessions.
         Starting Wait for Plymouth Boot Screen to Quit...
[  OK  ] Started Command Scheduler.
[  OK  ] Started Job spooling tools.
         Starting Terminate Plymouth Boot Screen...

Amazon Linux 2
Kernel 4.14.355-275.570.amzn2.x86_64 on an x86_64

localhost login: 

See next section for how to enable VNC console.

Enable VNC on existing VM instance

List domains on KVM

$ virsh list --all

Stop the instance

virsh shutdown <domain-name>

Edit the VM domain config using virsh edit command.

$ virsh edit <domain-name>

Add below XML contents within <devices> block (Accessible from outside)

<graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>
  <listen type='address' address='0.0.0.0'/>
</graphics>

Update root user password

Reboot the server and Press “e” in edit menu.

Edit linux16 line to add rd.break. Add at the end of linux16 line “rd.break

Press “Ctrl+x” to reboot the server

Remount /sysroot with rw and chroot to it, then set new root password.

mount -o remount,rw /sysroot
chroot /sysroot
passwd root

See screenshot below:

Add .autorelabel empty file inside / to reconfigure SELinux on reboot.

touch /.autorelabel 
exit

Login with the username root and password set earlier.How To Install Terraform on Amazon Linux 2

You should now have access to Amazon Linux console. Server IP address can be checked with ip ad command. Root user login and password authentication can be enabled by changing PermitRootLogin and PasswordAuthentication parameters.

# vi /etc/ssh/sshd_config
PermitRootLogin yes
PasswordAuthentication yes

Restart sshd service:

sudo systemctl restart sshd

We can test ssh login from our Workstation.

$ ssh [email protected]
Last login: Thu Jan 16 19:32:13 2025 from 192.168.1.182
   ,     #_
   ~\_  ####_        Amazon Linux 2
  ~~  \_#####\
  ~~     \###|       AL2 End of Life is 2025-06-30.
  ~~       \#/ ___
   ~~       V~' '->
    ~~~         /    A newer version of Amazon Linux is available!
      ~~._.   _/
         _/ _/       Amazon Linux 2023, GA and supported until 2028-03-15.
       _/m/'           https://aws.amazon.com/linux/amazon-linux-2023/

[root@amazonlinux ~]#

Set hostname of your server

hostnamectl set-hostname amazonlinux.cloudspinx

Set correct timezone to ensure apps dare synchronization is accurate.

sudo timedatectl set-timezone Africa/Nairobi

Upgrade your Amazon Linux 2 server to ensure all packages are latest.

yum -y update

Conclusion

In this article we’ve been able to deploy and install Amazon Linux 2 on KVM using Qcow2 image file downloaded from official project website. We hope this guide was helpful. If you encounter any issue kindly drop a comment for us.

Your IT Journey Starts Here!

Ready to level up your IT skills? Our new eLearning platform is coming soon to help you master the latest technologies.

Be the first to know when we launch! Join our waitlist now.

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Post

For most system admins, their day-to-day life activities revolve around having access to remote systems.VNC an acronym for Virtual Network […]

PostgreSQL is an open-source object-relational database management system (ORDBMS) based on POSTGRES, Version 4.2. Postgresql was developed at the University […]

Today’s tutorial will show you how to install WordPress with Apache and Let’s Encrypt on an Ubuntu 24.04|22.04 Linux system […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.