Operating System Linux

How to Disable or Restrict Root Login via SSH on Linux

Stop password attacks on the root account: turn off root SSH login, allow keys only, or limit root to a single IP address, without locking yourself out.

Every server on the public internet gets a steady stream of automated login attempts against the root account. Root exists on every Linux system and has no limits, so it is the one username an attacker never has to guess. Turning off direct root login over SSH, or limiting it to keys and known IP addresses, removes that target. You then log in as a normal user and use sudo when you need administrator rights.

This guide works on current Ubuntu, Debian, AlmaLinux, Rocky Linux, CentOS Stream and Fedora servers running OpenSSH.

Before you change anything

Locking root out before you have another way in is the most common way people lose access to a VPS. Do these three things first:

  1. Create a regular user that can use sudo. On Ubuntu and Debian:

    adduser deploy
    usermod -aG sudo deploy

    On AlmaLinux, Rocky Linux, CentOS Stream and Fedora the admin group is wheel:

    adduser deploy
    passwd deploy
    usermod -aG wheel deploy
  2. Open a second terminal, log in as that user and confirm that sudo -v accepts the password.

  3. Keep your current root session open until you have finished testing. If something goes wrong you can still fix it from there.

Check the current setting

Ask the SSH daemon which value is actually in effect, instead of reading the config file by eye:

sudo sshd -T | grep -i permitrootlogin

PermitRootLogin accepts four values:

ValueWhat root can do
yesLog in with a password or a key.
prohibit-passwordLog in with an SSH key only. Password and keyboard-interactive logins are refused. This is the OpenSSH default.
forced-commands-onlyLog in with a key only, and only to run the single command attached to that key. Useful for automated backups.
noNot log in over SSH at all.

Many VPS images ship with yes so that the password from the control panel works on first boot.

Disable root login completely

Modern distributions load extra settings from /etc/ssh/sshd_config.d/. Put your change in its own file there, so that a package update never overwrites it:

echo "PermitRootLogin no" | sudo tee /etc/ssh/sshd_config.d/00-root-login.conf

For each setting, OpenSSH uses the first value it reads, and the files in that directory are read in alphabetical order. That is why the file name starts with 00-: it has to come before files such as 50-cloud-init.conf that your provider may have added.

If your system has no sshd_config.d directory, edit /etc/ssh/sshd_config instead, find the PermitRootLogin line, remove the leading # if there is one and set it to no.

Check the configuration for mistakes, then restart the service:

sudo sshd -t

# Ubuntu and Debian
sudo systemctl restart ssh

# AlmaLinux, Rocky Linux, CentOS Stream, Fedora
sudo systemctl restart sshd

sshd -t prints nothing when the configuration is valid. Run the check from the previous section again to confirm that the value is now no.

Allow root with SSH keys only

If scripts or backup tools still need to connect as root, keep key logins and refuse passwords. Use this line in the same file:

PermitRootLogin prohibit-password

Password guessing against root stops working, while a client holding the right private key can still connect.

Restrict root login to one IP address

A Match block changes a setting only for connections that meet a condition. This example refuses root everywhere except from one office address, where key login is allowed:

PermitRootLogin no

Match Address 203.0.113.10
    PermitRootLogin prohibit-password

Replace 203.0.113.10 with your own address. You can list several, separated by commas, or use a range such as 203.0.113.0/24. A Match block applies to every line after it until the next Match or the end of the file, so keep it at the bottom.

Test it

From your own computer, try to log in as root:

ssh root@your-server-ip

You should see Permission denied. Then confirm that your regular user can still log in and run sudo. Only after both checks pass should you close the root session you kept open.

If you lock yourself out

These settings only affect SSH. The web console in your hosting control panel connects to the machine directly, so you can still log in there as root, correct the file and restart the SSH service.

Frequently asked questions

Does this make root unusable?

No. The account still exists. You reach it with sudo for single commands or sudo -i for a root shell.

Is disabling root login enough to secure SSH?

It closes one door. Use SSH keys for your regular user as well, turn off password logins with PasswordAuthentication no once keys work, and keep the system updated.

My change has no effect. Why?

Another file sets the same option earlier. Run sudo grep -ri permitrootlogin /etc/ssh/ to find it, then either edit that file or give yours a name that sorts first.

Share story:

Continue reading other guides on How VPS.