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

Predict, compare, and reduce costs with our S3 cost calculator

Previously I have written about how useful public cloud storage can be when starting a…

20 hours ago

One Thread to Poll Them All: How a Single Pipe Made WaterDrop 50% Faster

This is Part 2 of the "Karafka to Async Journey" series. Part 1 covered WaterDrop's…

24 hours ago

A year of documentation-driven development

For many software teams, documentation is written after features are built and design decisions have…

2 days ago

Announcing FIPS 140-3 for Ubuntu Core22

With the release of the FIPS 140-3 certified cryptographic modules for Ubuntu 22.04 LTS, Canonical…

3 days ago

The foundations of software: open source libraries and their maintainers

Open source libraries are repositories of code that developers can use and, depending on the…

6 days ago

From inspiration to impact: design students from Regent’s University London explore open design for their dissertation projects

Last year, we had the opportunity to speak at Regent’s UX Conference (Regent’s University London’s…

7 days ago