Reset the Password of a KVM Guest VM
Change the root password of a guest VM if you’ve forgotten the password.
This post covers resetting a forgotten root password for a KVM virtual machine on CentOS/RHEL/Ubuntu Linux. If you forget your KVM virtual machine root password, you can follow these steps to reset it.
To change the root password for a VM on a KVM server, you can use a tool called guestfish. Guestfish is a shell and command-line tool that allows you to examine and modify virtual machine file systems. So, you can use this tool to edit the virtual machine file system and then modify the /etc/shadow file to change the root password.
Install Guestfish
If you don’t already have guestfish installed, use these commands to install it:
- For CentOS/RHEL/Rocky/AlmaLinux
# yum install libguestfish-tools- For Fedora or newer versions of CentOS or RHEL derivatives:
# dnf install libguestfs-tools- For Ubuntu/Debian:
# apt-get install libguestfs-toolsShutdown the VM
# virsh shutdown myvmGet the path of the storage device for the guest OS
virsh dumpxml | egrep 'source(file|dev)'
<source dev='/dev/build1vg/myvm'/>Create a new encrypted password
If you already have an encrypted password hash, you can use that and skip this step.
If you need to encrypt a password, use this OpenSSL command:
$ openssl passwd -1 1234568093843924
$1$LXERBPx7$NRLqg7WWlOS688TRMWbmk/Use the guestfish utility to access the device
Use guestfish to open the file or device in read-write mode.
# guestfish --rw -a /dev/build1vg/myvmUse guestfish to mount the device
><fs> launch
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00><fs> list-filesystems
/dev/sda1: ext3
/dev/sda2: ext4
/dev/sda3: ext4
/dev/sda4: unknown
/dev/sda5: swap
/dev/sda6: ext4
><fs> mount /dev/sda2 /In the example above, the ‘/’ file system is /dev/sda2 so the last command mounts it at ‘/’.
Change the password
To change the password for the root user, you will need to edit the ‘/etc/shadow’ file with either vi or emacs.
Find the line containing the root user and replace the old password with the new password. Save and close the file.
Quit Guestfish
><fs> quitStart the VM back up
virsh start myvmYou should be able to login using your new password.
Please consider joining Medium as a paying member. It’s $5 a month OR $50 a year for unlimited access. And if you use the following link it will go a long way in supporting me as a writer. Thank you!




