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!

I am a professional WordPress & Graphic designer. I have more than 6 years of experience in this field.

Ubuntu Server Admin

Recent Posts

Ubuntu Weekly Newsletter 866

Welcome to the Ubuntu Weekly Newsletter, Issue 866 for the week of November 10 –…

7 hours ago

Ubuntu vs Debian: Linux Distributions Compared Deep Dive

Debian and Ubuntu are two popular Linux distributions. In this deep dive we will guide…

16 hours ago

How to Install Google Cloud BigQuery Python client library on Linux

In this article, we will see how to Install Google Cloud BigQuery Python client library…

3 days ago

Wallpaper Contest for Xfce 4.20 open for voting

Nov 15,2024 Wallpaper Contest for Xfce 4.20 open for voting The submission phase for the…

3 days ago

Canonical announces the first MicroCloud LTS release

MicroCloud 2.1.0 LTS is now available, expanding the number of Canonical infrastructure solutions with a…

4 days ago

Join Canonical in Paris at Dell Technologies Forum

Canonical is thrilled to be joining forces with Dell Technologies at the upcoming Dell Technologies…

5 days ago