DEV Community

M. Oly Mahmud
M. Oly Mahmud

Posted on

Day-3: Disable SSH Root Login on Linux | 100 Days Of DevOps

Securing SSH access is one of the simplest yet most effective ways to protect your Linux servers. By default, many servers allow root login via SSH, which can be risky. Disabling root login ensures that administrative access is only possible through non-root users with sudo privileges. Here’s a straightforward guide.

Step 1: Log in as a Non-Root User

Before disabling root login, make sure you have a non-root user with sudo privileges. For example, if you don’t already have one, you can create it like this:

sudo adduser yourusername
sudo usermod -aG sudo yourusername
Enter fullscreen mode Exit fullscreen mode

Then log in using that user:

ssh yourusername@server_ip
Enter fullscreen mode Exit fullscreen mode

Step 2: Edit the SSH Configuration

Open the SSH daemon configuration file:

sudo nano /etc/ssh/sshd_config
Enter fullscreen mode Exit fullscreen mode

Look for the line:

#PermitRootLogin yes
Enter fullscreen mode Exit fullscreen mode

Change it to:

PermitRootLogin no
Enter fullscreen mode Exit fullscreen mode

This disables root login via SSH.

Step 3: Restart the SSH Service

After editing the configuration, restart SSH to apply the changes:

sudo systemctl restart sshd
Enter fullscreen mode Exit fullscreen mode

On some systems (like Ubuntu/Debian), the service may be called ssh instead of sshd:

sudo systemctl restart ssh
Enter fullscreen mode Exit fullscreen mode

Step 4: Verify Non-Root Access

Before closing your session, test that your non-root user can log in and use sudo:

ssh yourusername@server_ip
sudo whoami
Enter fullscreen mode Exit fullscreen mode

It should return root. This confirms that administrative access is still available without using the root account.

Step 5: Optional Security Checks

For extra security, you can review recent login attempts to detect any failed root access:

sudo journalctl -u sshd | grep "root"
Enter fullscreen mode Exit fullscreen mode

Conlcusion

Disabling root login reduces the risk of brute-force attacks and limits the number of accounts attackers can target. Always make sure at least one non-root user has sudo privileges to manage the system safely.

Top comments (0)