How to change the root password in Linux?

Reading Time: 3 minutes

RHCSA: Changing the root password in Red Hat

This is part 4 of the series learning Red Hat. This process has been tested on RHEL8, it works on RHEL7 but at an earlier version, the process is a little different – often needing a boot disk. This guide will show you how to change the root password, and this procedure will work on any flavour of Linux, be it Debian, Ubuntu, or CentOS.

Summary of the rest of my RHCSA MiniSeries

Part 1 – How to use Grep and Regular Expressions (RegEx)

Part 2 – How to manipulate files in RedHat

Part 3 – Red Hat Permissions

Part 4 – How to change the root password on Red Hat

Part 5 – How to use Red Hat as a virtual machine host

Part 6 – How to configure local storage

Part 7 – how to manage users and groups in Red Hat Linux

Part 8 – how to configure NTP Network Time Protocol in Red Hat

The Linux boot process explained

To learn how to reset the root password, it is important to know how a Linux Operating System boots. This tutorial focuses on the GRUB2 bootloader and the startup sequence completed by systemd.

The Linux boot process is quite simple, you power on your PC or Server then:

  • The System loads BIOS or UEFI
    • BIOS – basic input/output system
    • UEFI – unified extensible firmware interface
  • Next Loads GRUB2 (Grand Unified Bootloader version 2)
  • Kernel starts
  • Systemd starts

To change the root password you have to break the boot process at GRUB2 stage.

How to break the Linux boot process

There are 3 ways to break the boot process on Linux. The first 2 ways are manually entered break commands added to the boot.cfg file. The final way is using a boot disk and using rescue mode .

  1. Rd.break – enters emegency mode with no system disk loaded
  2. emergency  – similar to rd.break but mounts system disk
  3. Rescue – like single user mode, disk mounted and services started

To change the root password you must use rdbreak because this process does not mount the system disk, mounting a system disk implements the file system permissions, meaning you cannot change the root password – instead you will be asked for the root password.

How to change the Linux root password:

Ensure you start with your Linux Server powered off.

  • Power on your server
  • At the GRUB2 boot screen press e to edit  boot.cfg
root password, How to change the root password in Linux?
  • Add the command rd.break after the boot image .img as per the picture above
  • Choose to continue the startup process.
  • the rd.break command will break the boot process and you will get a shell prompt
  • Mount the system root using the command
 mount -o rw,remount /sysroot/ 
  • Access the shell by typing
chroot sysroot

Now type passwd to change root password

passwd  

Important: now you have changed the root password, you must relabel the filesystem so that the files and folders are aware of the new password hashes. Simply type:

touch ./autorelabel

Now mount disks as read only

mount -o ro,remount /sysroot

Then reboot

reboot
root password, How to change the root password in Linux?
Example of the reboot process

Once the server reboots you will be able to log in with your newly changed root password. Its a little daunting when doing this for the first time, but after a while it will become second nature.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *