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.

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.

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

Meet the wildlife conservation AI 5G hotspot at MWC Barcelona 2026

From March 2-5 in Barcelona, Canonical will present a working wildlife conservation platform that combines…

1 day ago

Canonical and Ubuntu RISC-V: a 2025 retro and looking forward to 2026

2025: From RISC-V enablement to real execution  2025 was the year that RISC-V readiness gave…

2 days ago

Unmasking the Resolute Raccoon

You’ve almost certainly seen them… In the forest, rummaging through a dumpster, in poorly aging…

2 days ago

Bad Voltage Season 1 Episode 43: Got The Om On

Jono Bacon, Stuart Langridge and myself present Bad Voltage, in which Bryan is sadly unavoidably…

4 days ago

2011 LinuxQuestions.org Members Choice Award Winners – Thoughts

The polls are closed and the official results are in. You can view the detailed…

4 days ago

Building quantum-safe telecom infrastructure for 5G and beyond

coRAN Labs and Canonical at MWC Barcelona 2026 At MWC Barcelona 2026, coRAN Labs and…

5 days ago