The root user has full access to your server, making it a prime target for attackers. Disabling direct root login is a critical security measure that reduces the risk of unauthorized access and brute-force attacks. This guide will walk you through the steps to disable root login on your Linux server and explain why it’s important.
Why Disable Root Login?
- Prevent Brute-Force Attacks: Attackers often target the root account because it has unrestricted access. Disabling root login makes it harder for them to gain access.
- Limit Damage: If a non-root account is compromised, the attacker won’t have immediate root privileges, reducing the potential damage.
- Audit Trails: Using
sudocreates logs of commands executed with root privileges, making it easier to track changes or identify unauthorized activity.
Step 1: Create a Non-Root User with Sudo Privileges
Before disabling root login, you must create a non-root user with sudo privileges. This ensures you can still perform administrative tasks after root login is disabled.
- Log in to your server as the root user.
- Create a new user:
Replaceadduser usernameusernamewith your desired username. - Add the user to the
sudogroup:usermod -aG sudo username - Test the new user:
- Log out of the root account:
exit - Log in with the new user:
ssh username@your_server_ip - Test
sudoaccess by running:
If prompted for a password, enter the new user’s password.sudo ls /root
- Log out of the root account:
Important: Ensure the new user can successfully use sudo before proceeding. If sudo access is not working, do not disable root login.
Step 2: Disable Root Login
Once you’ve confirmed the new user has sudo access, you can safely disable root login.
- Open the SSH configuration file:
sudo nano /etc/ssh/sshd_config - Find the line that says:
or#PermitRootLogin yesPermitRootLogin yes - Change it to:
PermitRootLogin no - Save the file and exit the editor (in
nano, pressCTRL + X, thenY, thenEnter). - Restart the SSH service to apply the changes:
(On some systems, the service may be calledsudo systemctl restart sshsshdinstead ofssh.)
Warning: Do not close your current SSH session until you’ve verified that you can log in with the new user. If something goes wrong, you can still fix it using your current session.
Step 3: Verify the Changes
- Open a new terminal window and attempt to log in as root using SSH:
You should receive an error like:ssh root@your_server_ipPermission denied (publickey). - Log in with your non-root user:
ssh username@your_server_ip - Use
sudoto perform administrative tasks:sudo ls /root
Optional: Use SSH Keys for Authentication
For added security, disable password authentication and use SSH keys instead. This prevents brute-force attacks on your non-root account.
- Generate an SSH key pair on your local machine (if you don’t already have one):
Follow the prompts to save the key pair.ssh-keygen -t rsa -b 4096 - Copy the public key to your server:
ssh-copy-id username@your_server_ip - Open the SSH configuration file:
sudo nano /etc/ssh/sshd_config - Ensure the following lines are set:
PasswordAuthentication noPubkeyAuthentication yes - Restart the SSH service:
sudo systemctl restart ssh
Important: Before disabling password authentication, ensure you can log in using your SSH key. If the key fails, you may lock yourself out of the server.
What to Do If You Lose Access
If you accidentally lock yourself out of your server, follow these steps to regain access:
- Contact us by opening a support ticket to explain the issue.
- Our team will assist you in re-enabling root login temporarily by editing the SSH configuration file:
and setting:/etc/ssh/sshd_configPermitRootLogin yes - Once root login is re-enabled, you can log in and fix the issue.
Conclusion
Disabling root login is a simple yet effective way to enhance your server’s security. By requiring users to log in with a non-root account and use sudo for administrative tasks, you reduce the risk of unauthorized access and brute-force attacks. Combine this with other security measures like SSH keys and a firewall to keep your server safe.