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:
- 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- 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-imgcommand to do this:
qemu-img convert -f qcow2 -O raw .qcow2 .raw- 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 .raw /mnt- Change the password: To change the password, you need to edit the
/etc/shadowfile in the mounted directory. You can use thechrootcommand to run a shell in the mounted directory:
chroot /mntThen use the passwd command to change the root password:
passwd root- 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 /mntrnqemu-img convert -f raw -O qcow2 .raw .qcow2- Start the virtual machine: Finally, you can start the virtual machine again using the following command:
virsh startThese 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.
