Friday, 1 November 2019

How to Set root Password of CentOS 7 Cloud Image (guestfish)

Most of the cloud images protect by disabling root password and Disabling Remote Root and Other users login using their passwords. By default "guestfish" is an interactive shell that provides directory access the guest virtual machine file system access.

This article describes How to Set root Password of CentOS 7 Cloud Image (qcow2)

Step 1. Prerequisites: Install libguestfs-tools by issuing the following command:

[root@linuxcnf ~]# yum install libguestfs-tools
Loaded plugins: fastestmirror
  libguestfs-tools.noarch 1:1.40.2-5.el7_7.2

Dependency Installed:
  alsa-lib.x86_64 0:1.1.8-1.el7                                        attr.x86_64 0:2.4.46-13.el7
    usbredir.x86_64 0:0.7.1-3.el7                                        yajl.x86_64 0:2.0.4-4.el7

Dependency Updated:
  cryptsetup-libs.x86_64 0:2.0.3-5.el7    device-mapper.x86_64 7:1.02.158-2.el7_7.2    device-mapper-libs.x86_64 7:1.02.158-2.el7_7.2

[root@linuxcnf ~]#

Step 2. Encrypted Password: Run the following command to generate openssl encrypted password:

[root@linuxcnf ~]# openssl passwd -1 password
[root@linuxcnf ~]#

Step 3. Change in Password: Access the qcow2 image file system by guestfish tool and follow the commands given below.

[root@linuxcnf ~]# guestfish --rw -a /var/lib/libvirt/images/CentOS-7-x86_64-GenericCloud-1907.qcow2

Welcome to guestfish, the guest filesystem shell for
editing virtual machine filesystems and disk images.

Type: ‘help’ for help on commands
      ‘man’ to read the manual
      ‘quit’ to quit the shell


Use run or launch command to access image file system:

><fs> run
 100% ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ --:--

Run the following command to list out existing file system on the image:

><fs> list-filesystems
/dev/sda1: xfs

Run the mount command to mount the root partition:

><fs> mount /dev/sda1 /

Now partition is mounted, modify the /etc/shadow file and replace password (encrypted password generated in Step 2) in the field of password of the user:

><fs> vi /etc/shadow

Now run the following command to sync and quit from the shell:

><fs> sync
><fs> quit

[root@linuxcnf ~]#

Done!!! Now upload the image on glance and Launch an instance with image. Once instance launched try the login using root user from its console.

No comments:

Post a comment