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:
virsh shutdown
command:virsh shutdown vm_name
Replace vm_name
with the name of the VM.
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.
virsh start
command:virsh start new_vm_name
Replace new_vm_name
with the name of the new VM.
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.
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.
Log in as the root
user. You will not be prompted for a password, as the password for the root
user has been reset.
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.
virsh shutdown
command:virsh shutdown new_vm_name
Replace new_vm_name
with the name of the new VM.
virsh undefine
command:virsh undefine new_vm_name
Replace new_vm_name
with the name of the new VM.
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
.