Categories: TutorialsUbuntu

Methods to disable root account in Linux

In Linux, the root user has access to everything and can do many things. It has access to each and every command of the linux system and can delete,edit, update, execute, read, write to all the available files and folders. It has all the permission so it is the supreme account on linux.

Such access might be troublesome in most cases. As some users might execute wrong commands at the wrong time which will directly affect the system.

In most cases, new users are created and given the limited privileges so the system will not be affected by troublesome commands. To perform the critical tasks. such users are given the privilege of root user by using sudo command.

Sponsored
Create a user with sudo privilege

In this article, we are going to use ubuntu20.04 LTS. Let’s create one user with the privilege of root user by providing sudo access. We can create such user by following the below command:

# adduser test

Now, add this user to the sudo group by following the below command so it can perform tasks with the root privilege.

# usermod -aG sudo test

Here, a means append and G means a group. We append the test user to the sudo group.

Now let’s switch to the test user which has sudo privilege.

We are going to discuss different methods to disable root account in Linux. Some of the methods are discussed with the points below.

Changing shell of root user to disable root login

One of the methods to disable root account in linux is to change the shell of the root user. First, Open the /etc/passwd file and change the /bin/bash or /bin/sh to /sbin/nologin. You can follow the below instruction to do so.

$ sudo vim /etc/passwd

Change the line as per above screenshot and save it.

Now, the root user cannot login and certain messages will be displayed as shown in the below screenshot.

Sponsored

Disabling Root Login for SSH

When you try to login to servers, you can easily login to it through SSH. But, if you want to disable root login to that server, you can simply edit the /etc/ssh/sshd_config file. You can edit with your favorable editor. Here, we are using nano.

$ sudo nano /etc/ssh/sshd_config

Add the line as shown on screenshot or edit the line just above it to “PermitRootLogin no” to disable root login to that specific server.

Using passwd command to disable root login

It is easy to disable root login by using the passwd command.

$ sudo passwd -l root

Here, after running the above command, we cannot login to the root user until the new password is set for the root user.

Using usermod command to disable root login

It is similar to the passwd command with little change. We can run the following command to disable root login.

$ sudo usermod -L root

Same as above method, after running the usermod command, we cannot login to the root user until the new password is set for the root user.

Conclusion

We have discussed four methods to disable the root login in Linux. You can use any of these methods as per your requirement or purpose. We hope you enjoyed reading the article. Thank you!

Ubuntu Server Admin

Recent Posts

Canonical Releases Ubuntu 25.04 Plucky Puffin

The latest interim release of Ubuntu introduces “devpacks” for popular frameworks like Spring, along with…

2 days ago

Ubuntu 25.04 (Plucky Puffin) Released

Ubuntu 25.04, codenamed “Plucky Puffin”, is here. This release continues Ubuntu’s proud tradition of integrating…

3 days ago

Extended Security Maintenance for Ubuntu 20.04 (Focal Fossa) begins May 29, 2025

Ubuntu released its 20.04 (Focal Fossa) release 5 years ago, on March 23, 2020. As…

3 days ago

Ubuntu 20.04 LTS End Of Life – activate ESM to keep your fleet of devices secure and operational

Focal Fossa will reach the End of Standard Support in May 2025, also known as…

4 days ago

Ubuntu MATE 25.04 Release Notes

Ubuntu MATE 25.04 is ready to soar! 🪽 Celebrating our 10th anniversary as an official…

4 days ago

Ubuntu Weekly Newsletter Issue 887

Welcome to the Ubuntu Weekly Newsletter, Issue 887 for the week of April 6 –…

6 days ago