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!