Use Guestfish to reset the forgotten root password for the virtual machine in qcow2 format on KVM

Suppose you set a root password for the virtual machine on KVM, but forgot it. So, how to gain root access? Please use Guestfish to reset the root password in qcow2 format on KVM.

Suppose you set a root password for the virtual machine on KVM, but forgot it. So, how to gain root access? Please use Guestfish to reset the root password in qcow2 format on KVM. Here is what you need to prepare and do.

Prepare LAB lesson

  1. On a VMware environment, use a CentOS 7 virtual machine with KVM installed.
  2. Create a CentOS 7.0 virtual machine with qcow2 format

Proceed to set the root password

Step 1: Install Guestfish

 yum install libguestfs-tools -y 

Step 2: Shutdown the virtual machine

Use Guestfish to reset the forgotten root password for the virtual machine in qcow2 format on KVM Picture 1Use Guestfish to reset the forgotten root password for the virtual machine in qcow2 format on KVM Picture 1

 virsh shutdown tên_máy_ảo 

Step 3: Reset, change the root password using Guestfish

Note: First create a new root user password by typing the following command and then using the encrypted password to change.

 openssl passwd -1 pass_mới 

Use Guestfish to reset the forgotten root password for the virtual machine in qcow2 format on KVM Picture 2Use Guestfish to reset the forgotten root password for the virtual machine in qcow2 format on KVM Picture 2

Then run the following command, for the Centos7 virtual machine:

 guestfish --rw -a /var/lib/libvirt/images/centos7.0.qcow2 

Use Guestfish to reset the forgotten root password for the virtual machine in qcow2 format on KVM Picture 3Use Guestfish to reset the forgotten root password for the virtual machine in qcow2 format on KVM Picture 3

Next use the following commands

 > launch > list-filesystems #liệt kê các phân vùng /dev/sda1: xfs /dev/centos/root: xfs /dev/centos/swap: swap > mount /dev/centos/root / #lưu ý lựa chọn mount vào phân vùng của hệ thống có chứa thư mục /etc. > vi /etc/shadow #chỉnh sửa file shadow 

Use Guestfish to reset the forgotten root password for the virtual machine in qcow2 format on KVM Picture 4Use Guestfish to reset the forgotten root password for the virtual machine in qcow2 format on KVM Picture 4

For example: The same for ubuntu virtual machines

Use Guestfish to reset the forgotten root password for the virtual machine in qcow2 format on KVM Picture 5Use Guestfish to reset the forgotten root password for the virtual machine in qcow2 format on KVM Picture 5

After running the vi / etc / shadow command, fix the encrypted pass at root with the new encrypted pass from the openssl command above and save it:

Use Guestfish to reset the forgotten root password for the virtual machine in qcow2 format on KVM Picture 6Use Guestfish to reset the forgotten root password for the virtual machine in qcow2 format on KVM Picture 6

Then exit Guestfish:

 > quit 

Step 4: Open the virtual machine with virt-manager and enter the new password

So you have finished resetting the forgotten root password for the virtual machine in qcow2 format on KVM.

4.2 ★ | 13 Vote