How to reset forgotten root password for Linux KVM qcow2 image/vm

Here’s how to reset the root password for a Linux KVM virtual machine in a qcow2 image:

  1. Stop the virtual machine: To reset the root password, you need to stop the virtual machine first. You can stop it using the following command:
virsh shutdow [VM_NAME]
  1. Convert the qcow2 image to raw format: To reset the password, you need to convert the qcow2 image to a raw format. You can use the qemu-img command to do this:
qemu-img convert -f qcow2 -O raw [VM_NAME].qcow2 [VM_NAME].raw
  1. Mount the raw image to a temporary directory: To reset the password, you need to mount the raw image to a temporary directory. You can do this using the following command:
mount -o loop [VM_NAME].raw /mnt
  1. Change the password: To change the password, you need to edit the /etc/shadow file in the mounted directory. You can use the chroot command to run a shell in the mounted directory:
chroot /mnt

Then use the passwd command to change the root password:

passwd root
  1. Unmount the raw image and convert it back to qcow2 format: Once you have changed the password, you need to unmount the raw image and convert it back to the qcow2 format. You can do this using the following commands:
umount /mnt
qemu-img convert -f raw -O qcow2 [VM_NAME].raw [VM_NAME].qcow2
  1. Start the virtual machine: Finally, you can start the virtual machine again using the following command:
virsh start [VM_NAME]

These steps should help you reset the forgotten root password for a Linux KVM virtual machine in a qcow2 image. Keep in mind that you should always backup your virtual machine images before making any changes, as there is always a risk of data loss or corruption when working with disk images.

Leave a Comment