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
..................
Installed:
  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

Complete!
[root@linuxcnf ~]#

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

[root@linuxcnf ~]# openssl passwd -1 password
$1$yW/yGil3$9STRKah1asX0MDhyvz48H1
[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

><fs>

Use run or launch command to access image file system:

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

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

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

Run the mount command to mount the root partition:

><fs> mount /dev/sda1 /
><fs>

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
root:$1$yW/yGil3$9STRKah1asX0MDhyvz48H1:18116:0:99999:7:::
bin:*:17834:0:99999:7:::
daemon:*:17834:0:99999:7:::
................
postfix:!!:18116::::::
chrony:!!:18116::::::
><fs>

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

><fs> sync
><fs>
><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