How to Set or Change the Root Password in Linux – Complete Guide

 

In Linux, the root user has the highest level of system privileges. Setting or changing the root password is crucial for system security and administrative control. However, some Linux distributions disable the root account by default, requiring sudo access instead.

In this guide, you will learn how to set, change, or reset the root password in Linux using the terminal.

Checking If Root Account is Enabled

Before setting or changing the root password, check if the root account is active by running:

sudo -i

If you can access the root shell, the account is enabled. Otherwise, you may need to enable it first.

How to Set or Change the Root Password

Method 1: Using the passwd Command

The easiest way to change or set the root password is by using the passwd command. Run the following command:

sudo passwd root

You will be prompted to enter a new password and confirm it.

Method 2: Changing the Password While Logged in as Root

If you are already logged in as root, simply type:

passwd

Enter the new password when prompted.

Resetting the Root Password If Forgotten

Method 1: Using Recovery Mode (Ubuntu/Debian)

If you forgot your root password, follow these steps:

    1. Restart your computer and hold Shift to open the GRUB menu.
    2. Select Advanced options for Ubuntu and choose Recovery mode.
    3. Select root - Drop to root shell prompt.
    4. Remount the filesystem with write access:
mount -o remount,rw /
    1. Reset the password using:
passwd root
    1. Reboot the system:
reboot

Method 2: Booting Into Single-User Mode

For other Linux distributions:

    1. Reboot the system and access the GRUB menu.
    2. Edit the kernel parameters by pressing e.
    3. Find the line starting with linux and add:
init=/bin/bash
    1. Press Ctrl + X or F10 to boot.
    2. Once in single-user mode, reset the password:
passwd root
    1. Reboot the system:
exec /sbin/init

Disabling the Root Account

If you want to disable the root account for security reasons, use:

sudo passwd -l root

To re-enable it later, use:

sudo passwd root

FAQs

What if my Linux system does not have a root password?

Some distributions like Ubuntu disable the root account by default. Use sudo for administrative tasks instead.

How can I check if my root password was changed successfully?

Try switching to the root user using:

su -

Is it safe to enable the root account?

It depends. Enabling root access can be a security risk if not handled properly. If you don’t need it, it’s better to use sudo instead.

Can I recover my root password without GRUB access?

If you don’t have access to GRUB, you may need to use a Linux live USB to reset the password.

Conclusion

Setting or changing the root password in Linux is essential for system security and administrative control. Whether you are enabling, resetting, or disabling the root account, the steps above provide a safe and efficient way to manage your system access.

Have questions? Let us know in the comments! 🚀