GICT knowledge base
How to Disable Root Login in Linux
Published
In every Linux-based system, the root user holds the highest administrative privileges. This account can perform any operation, modify any configuration, and access any part of the system. However, while powerful, the root account is also a major security risk. If an attacker gains access to it, they can completely compromise your server.
Disabling the root login helps tighten security by ensuring that only specific users — typically administrators with sudo privileges — can perform system-level tasks. With this configuration, users must authenticate themselves through sudo before executing any privileged command, providing an additional layer of protection and accountability.
⚠️ Important: Before disabling the root account, make sure you already have a non-root user with sudo privileges. Without it, you could lose access to critical administrative capabilities.
Prerequisites
Before you begin, ensure the following conditions are met:
- You have access to your Linux instance through SSH or a local terminal.
- You are logged in as a non-root user with sudo privileges.
- You understand basic Linux command-line operations.
Step 1: Disable Root SSH Login
The Secure Shell (SSH) protocol is the standard method for remotely connecting to Linux systems. By default, many systems allow the root user to log in directly over SSH, which is a security concern. Disabling root SSH login ensures that even if someone obtains the root password, they cannot connect remotely as the root user.
Follow these steps:
- Open the SSH configuration file with a text editor:
sudo nano /etc/ssh/sshd_config - Locate the following line:
PermitRootLogin yes - Change its value to:
PermitRootLogin no - Save and close the file by pressing Ctrl + O, Enter, and then Ctrl + X.
- Restart the SSH service to apply the changes:
sudo systemctl restart sshd
Tip: Some distributions name the service ssh instead of sshd. You can verify by running:
systemctl status sshd
Once this change is applied, your system will reject all SSH login attempts using the root account.
Step 2: Disable Access to the Root User Shell
Disabling SSH access isn’t enough — users could still gain root access locally or by using sudo su. To fully restrict root access, you can modify the system shell associated with the root user. This change ensures that whenever someone tries to switch to the root account, they’ll be greeted with an error message instead of an interactive shell.
- Open the /etc/passwd file:
sudo nano /etc/passwd - Locate the line starting with root and replace the shell path
/bin/bash(or similar) with/sbin/nologin:
root:x:0:0:root:/root:/sbin/nologin - Save and close the file.
The /sbin/nologin shell prevents interactive logins and displays a default message:
This account is currently not available.
To protect the /etc/passwd file from unauthorized modifications, make it immutable:
sudo chattr +i /etc/passwd
This command applies an immutable attribute, which prevents any process — even the root user — from modifying or deleting the file.
⚠️ Note: To make changes in the future, you must first remove the immutable flag:
sudo chattr -i /etc/passwd
Test the change by running:
sudo su
You should see the following message:
This account is currently not available.
This confirms that the root shell is successfully disabled.
Step 3: Create a Custom Message of the Day (MOTD)
The Message of the Day (MOTD) is displayed whenever a user logs into a Linux system. It can be customized to provide security reminders, usage policies, or administrative instructions. In this case, you can use it to inform users that the root account has been disabled and that they should use sudo for administrative commands.
- Open the /etc/motd file:
sudo nano /etc/motd - Add a custom message, for example:
Welcome to the server! Note: The root account is disabled for security reasons. Please use sudo to execute administrative commands. - Save and close the file.
Now, whenever someone logs in, they will see this message — a helpful reminder to follow proper administrative practices.
Step 4: Verify Root Login is Disabled
After applying all the above changes, you should verify that root login is fully disabled across all access methods.
- Test via Terminal
Try switching to the root account:
sudo su
You should see:
This account is currently not available. - Test via SSH
Attempt to log in using SSH as the root user:
ssh root@your_server_ip
Expected output:
Permission denied, please try again. Received disconnect from your_server_ip port 22:2: Too many authentication failures Disconnected from your_server_ip port 22 - Test via Console or Display Manager
If you attempt to log in as root locally (through tty1 or a graphical display manager), the session should immediately terminate, confirming that the account is disabled.
Step 5 (Optional): Re-enable Root Access (If Needed)
If you need to temporarily re-enable the root account for maintenance or recovery, follow these steps:
- Remove immutability from /etc/passwd:
sudo chattr -i /etc/passwd - Change the root shell back to /bin/bash:
sudo nano /etc/passwd
Replace:
/sbin/nologin
with:
/bin/bash - Re-enable root SSH access (if required):
sudo nano /etc/ssh/sshd_config
Change:
PermitRootLogin no
to:
PermitRootLogin yes - Restart SSH:
sudo systemctl restart sshd
Remember to disable root login again after completing your administrative tasks to maintain security best practices.
Conclusion
You have now successfully disabled the root user login on your Linux workstation. This configuration enhances your system’s overall security by limiting administrative access to verified sudo users.
By disabling direct root logins — both through SSH and the local shell — and protecting the /etc/passwd file, you significantly reduce the attack surface available to malicious actors. The addition of a custom Message of the Day (MOTD) further improves clarity for users, ensuring they know how to perform privileged actions safely.
Regularly enforcing such security measures helps maintain a hardened and secure Linux environment, where privileges are tightly controlled and unauthorized access is effectively prevented.
Related guides
How to Restrict an SSH Key to One Command
Restrict an SSH key to one command with authorized_keys options. Protect the command and check that unwanted shell and forwarding access are denied.
Linux and CommandsSelf-Hosted Video Processing With FFmpeg: A Practical Guide
Learn how to self-host video processing on a VPS using FFmpeg, with practical commands for conversion, compression, thumbnails, and automated workflows.
Linux and CommandsHow to use nohup: Commands in Linux That Survive Terminal Closes
Keep commands running after you close the terminal. nohup examples with wget, Python, rsync, and Docker on any VPS.
Linux and CommandsHow to Fix “Too Many Open Files” Error on Linux Servers
Learn how to fix the "Too Many Open Files" error on Linux servers by understanding file descriptors, adjusting limits, and diagnosing leaks effectively.
