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

https://w‮uttual.ww‬ri.com
How to reset forgotten root password for Linux KVM qcow2 image/vm

If you have forgotten the root password for a Linux KVM virtual machine (VM) that uses a qcow2 image, you can reset the password using the following steps:

  1. Shut down the VM using the virsh shutdown command:
virsh shutdown vm_name

Replace vm_name with the name of the VM.

  1. Create a new virtual machine using the same qcow2 image as the original VM, but with a different name. This can be done using the virt-install command, like this:
virt-install --name new_vm_name --disk path=path_to_qcow2_image,format=qcow2 --memory 512 --vcpus 1 --network bridge=br0 --graphics vnc --import

Replace new_vm_name with the name you want to give the new VM, and path_to_qcow2_image with the path to the qcow2 image for the original VM. This command will create a new VM using the same qcow2 image as the original VM, but with a different name.

  1. Start the new VM using the virsh start command:
virsh start new_vm_name

Replace new_vm_name with the name of the new VM.

  1. Connect to the new VM using a VNC client, such as vncviewer. You will need to know the VNC port number for the new VM, which you can find using the virsh vncdisplay command:
virsh vncdisplay new_vm_name

Replace new_vm_name with the name of the new VM. This will display the VNC port number for the new VM.

  1. Connect to the new VM using the VNC port number displayed by the virsh vncdisplay command. You will be prompted to enter a password. Leave the password blank, and press Enter.

  2. Log in as the root user. You will not be prompted for a password, as the password for the root user has been reset.

  3. Use the passwd command to set a new password for the root user:

passwd

Follow the prompts to enter and confirm a new password for the root user.

  1. Shut down the new VM using the virsh shutdown command:
virsh shutdown new_vm_name

Replace new_vm_name with the name of the new VM.

  1. Delete the new VM using the virsh undefine command:
virsh undefine new_vm_name

Replace new_vm_name with the name of the new VM.

  1. Start the original VM using the virsh start command:
virsh start vm_name

Replace vm_name with the name of the original VM.

You should now be able to log in to the original VM using the new root password you set in step 7.

Keep in mind that these steps will only work if the original VM was configured to allow logging in as the root.

Created Time:2017-10-28 21:39:07  Author:lautturi