How To Change A Root Password In Linux

6 min read

Changing the root password in Linux is a fundamental system administration task that every user managing a server or desktop environment must master. Whether you are securing a fresh installation, rotating credentials for compliance, or recovering access after a forgotten password, understanding the correct procedures ensures system integrity and prevents accidental lockouts. This guide covers the standard method for authenticated users, the single-user mode recovery technique for lost credentials, and critical security considerations to keep your system protected.

Understanding the Root Account and Privileges

Before diving into the commands, Make sure you understand what the root account represents. So it matters. In Linux, the root user—often called the superuser—holds unlimited privileges across the entire filesystem. This account can modify any file, start or stop any service, install software, and manage other user accounts. Because of this immense power, the root password is the single most sensitive credential on the system Most people skip this — try not to..

Modern Linux distributions like Ubuntu, Linux Mint, and elementary OS often disable the root account by default, encouraging the use of sudo for administrative tasks. On top of that, in these systems, the root account exists but has no valid password set (indicated by an exclamation mark or asterisk in the /etc/shadow file). Conversely, distributions such as CentOS, RHEL, Debian, Fedora, and Arch Linux typically enable the root account during installation and require a strong password immediately. Knowing which model your distribution follows dictates the exact steps you need to take.

Method 1: Changing the Password While Logged In (Standard Method)

If you currently have root access—either by logging in directly as root or by using a user account with sudo privileges—changing the password is straightforward. This is the standard workflow for routine password rotation Worth keeping that in mind..

Using the passwd Command as Root

If you are already logged in as the root user (indicated by the # prompt), simply type:

passwd

The system will prompt you to enter the new password twice. Note that no characters will appear on the screen while you type; this is a security feature to prevent shoulder surfing. After the second successful entry, you will see a confirmation message: passwd: password updated successfully And it works..

Using sudo to Change the Root Password

On distributions where the root account is locked (like Ubuntu) or if you are logged in as a standard user with sudo rights, you must invoke the passwd command with elevated privileges targeting the root user specifically:

sudo passwd root

You will first be asked for your user password (the sudo authentication). Day to day, once verified, you will be prompted to set the new root password twice. This command effectively unlocks the root account by assigning it a valid password hash Simple as that..

Changing the Password for Another User

System administrators often need to reset passwords for other users. The syntax remains consistent:

sudo passwd username

Replace username with the actual account name. This is useful when a standard user forgets their credentials and cannot authenticate via sudo themselves.

Method 2: Resetting a Forgotten Root Password (Recovery Mode)

The most stressful scenario is losing the root password entirely with no other sudo users available. That said, linux provides a built-in recovery mechanism via the GRUB bootloader and single-user mode (or emergency target in systemd). This process requires physical access or console access (via KVM, IPMI, or hypervisor console) to the machine Worth knowing..

Step 1: Interrupt the GRUB Boot Process

  1. Reboot the server.
  2. As soon as the GRUB menu appears (the list of kernels), press the e key to edit the boot entry for the default kernel.
    • Note: On some systems, you may need to hold Shift (BIOS) or press Esc (UEFI) during boot to reveal the GRUB menu.

Step 2: Modify Kernel Parameters

  1. Use arrow keys to handle to the line starting with linux or linuxefi / linux16. This line defines the kernel and boot parameters.
  2. Go to the very end of that line.
  3. Append the following parameter: rd.break (for RHEL/CentOS/Fedora) or init=/bin/bash (for Debian/Ubuntu/Arch and generic systemd systems).
    • Alternative for systemd: Append systemd.unit=emergency.target.
  4. Press Ctrl+x or F10 to boot with these modified parameters.

Step 3: Remount the Filesystem with Write Permissions

By default, the root filesystem (/) is mounted as read-only in these emergency environments to prevent corruption. You must remount it with read-write access Which is the point..

For rd.break (RHEL/CentOS/Fedora): The system drops you into a switch_root prompt. The real root filesystem is mounted at /sysroot That's the part that actually makes a difference. Simple as that..

mount -o remount,rw /sysroot
chroot /sysroot

For init=/bin/bash (Debian/Ubuntu/Arch): You are dropped directly into a bash shell with / as the root, but it is read-only And that's really what it comes down to..

mount -o remount,rw /

Step 4: Change the Password

Now that the filesystem is writable, run the standard command:

passwd root

Enter the new password twice.

Step 5: The Critical SELinux Relabeling Step (RHEL/CentOS/Fedora)

This step is mandatory on systems running SELinux (Enforcing mode). Because the password change occurred outside the standard SELinux policy context (inside a chroot or emergency shell), the new /etc/shadow file will have an incorrect security context. If you skip this, the system will fail to boot or deny login immediately after reboot.

Type the following before exiting:

touch /.autorelabel

This creates a flag file telling the kernel to perform a full filesystem relabel on the next boot Simple as that..

Step 6: Exit and Reboot

Type exit twice (once to leave chroot, once to leave the emergency shell) or press Ctrl+D. The system will continue booting. If you triggered the autorelabel, the reboot process will take significantly longer as it relabels every file. Do not interrupt this process Small thing, real impact..

Short version: it depends. Long version — keep reading Small thing, real impact..

Method 3: Using a Live USB (Alternative Recovery)

If the GRUB menu is password-protected or the bootloader is corrupted, a Live USB (like Ubuntu Desktop, Fedora Live, or SystemRescueCd) provides a universal recovery path Took long enough..

  1. Boot the Live USB and select "Try" or "Live Environment".
  2. Open a terminal and identify the target system partition (use lsblk or fdisk -l). Assume it is /dev/sda2.
  3. Mount the target partition:
    sudo mount /dev/sda2 /mnt
    
  4. If you have a separate /boot or /var partition, mount those too under /mnt/boot, /mnt/var, etc.
  5. Bind mount system directories to allow chroot:
    for d in /dev /dev/pts /proc /sys /run; do sudo mount --bind $d /mnt$d; done
    
  6. Chroot into the installed system:
    sudo chroot /mnt
    
  7. Run passwd root.
  8. Exit chroot (exit), unmount partitions (sudo umount -R /mnt), and reboot.

Verifying the Change and Testing Access

After the system reboots normally, verify the change immediately. Do not close your current remote session (if applicable) until

New Releases

What's Dropping

Close to Home

Continue Reading

Thank you for reading about How To Change A Root Password In Linux. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home