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

I have Debian Linux VM running on KVM. I think I forgotten the password for the root account and I am no longer able to run ‘su -‘ command. How do I reset the password for the root account for KVM VM which is in qcow2 format? You can modify images with guestfish. It is a shell and command-line tool for examining and editing virtual machine filesystems. It uses libguestfs and exposes all of the functionality of the guestfs API. This page shows how to use guestfish to change the root account password.

Step 2 – Find location of KVM VM image

Step 3 – Reset/change the root password using guestfish

First generate new root user account password by typing the following command:# openssl passwd -1 mySecretRootAccountPasswordHere $1$M1bf5Y3T$p2CYEz8vlUD2R.fXydTLt. Please copy $1$M1bf5Y3T$p2CYEz8vlUD2R.fXydTLt. password. You need to use this one in next few steps.

How to reset forgotten root password for Linux KVM vm

Let us start the procedure by running the following guestfish command:# guestfish --rw -a /var/lib/libvirt/images/debian9-vm1.qcow2 You will see a prompt as follows:

><fs>

To launch the backend either type ‘run’ or ‘launch’ command:

><fs> launch

To list partitions type:

><fs> list-filesystems

Now mount whatever disk you found it. For example, I found /dev/sda1, so I run the following mount command:

><fs> mount /dev/sda1 /

Edit the /etc/shadow file using a text editor such as vi command or emacs command:

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Your support makes a big difference:

I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft takes a lot of my time and hard work to produce. If everyone who reads nixCraft, who likes it, helps fund it, my future would be more secure. You can donate as little as $1 to support nixCraft: