How to Enable and Disable Root User Account in Ubuntu?

In Ubuntu, different users can work on the same computer. All the users can be managed by one user, which is the administrative user, also known as the root user. The root user can decide with which users the administrative privileges can be shared. Other than the root user, none of the users are allowed to run the administrative privileges. 

This blog explains the methods of enabling and disabling the root user account for Ubuntu 22.04.

What are the Sudo Users in Ubuntu?

In Ubuntu, the sudo users are those users allowed by the root user to perform administrative tasks such as making changes in the configuration files. To add the user to the sudo group, simply follow the general syntax mentioned below:

$ usermod -aG sudo [username]

When the user is added to the sudo group, then open the sudoers file with the nano text editor:

$ sudo nano /etc/sudoers

And add the below-mentioned line at the end of the file:

username ALL=(ALL) NOPASSWD: ALL

Now all the users have sudo privileges, and they can use the administrative privileges without providing any passwords. 

How to Enable the Root User Account in Ubuntu?

To enable the root user account in Ubuntu, we must set a strong password by appending the above-made changes. Different methods to enable the root user account in Ubuntu are as follows:

Method 1: Enabling the Root Account Password

This method involves setting a password for the root account, which is disabled by default in Ubuntu for security reasons. To do this, execute the below script:

$ sudo passwd root

When the password is set, the root user account has been enabled. 

Method 2: Temporarily Switch to the Root User Account

This method allows you to temporarily switch to the root user account and perform actions as the root user. To do this, open a terminal window and type the below script:

$ sudo -i

The output shows that the root user account has been switched.

Method 3: Allowing Root Login via SSH 

This method involves editing the SSH configuration file located at /etc/ssh/sshd_config to allow the root account to log in to the system via SSH: 

$ sudo nano /etc/ssh/sshd_config file

Search the “PermitRootLogin” variable and assign the values “yes” as below: 

After that, save and close the file, 

Method 4: Temporarily Switch to a Root Shell

This method allows you to temporarily switch to a root shell and perform actions as the root user. To do this, execute the below script: 

$ sudo -s

You will then be prompted to enter your password to confirm the action, after which you will have a root shell and be able to perform actions as the root user. 

How to Disable the Root User Account in Ubuntu?

To disable the root user account, follow the multiple methods given below:

Method 1: Removing the Root Account Password

This method involves using the “passwd” command in the terminal to remove the root account password:

$ sudo passwd -dl root

In this way, the root account cannot be used to log in to the system and is disabled. 

Method 2: Disabling Root Login via SSH

This method involves editing the SSH configuration file located at /etc/ssh/sshd_config to prevent the root account from being used for logging in to the system via SSH: 

$ sudo nano /etc/ssh/sshd_config

To do this, open the file using a text editor with elevated privileges, such as, find the line that says “PermitRootLogin yes” and change it to “PermitRootLogin no”. 

Save the file and restart the SSH service using the command “sudo service ssh restart” to apply the changes.

Method 3: Temporary Disabling the Root Account

This method involves using the sudo usermod -L root command in the terminal to lock the root account:

$  sudo usermod -L root

Now, the root account cannot be used to log in to the system. 

Conclusion

To enable the root user account in Ubuntu, utilize the “sudo -i” and “sudo -s”, “passwd” commands, and “sshd_config” files. For disabling it, “passwd”, “usermod” commands, and “sshd_config” files are utilized. In this blog, all possible methods of enabling and disabling passwords for the root user have been explained in detail for Ubuntu 22.04.