Disable root login and password authentication over SSH

Stop brute-force attempts by switching off root and password logins with a small SSH drop-in file, then verify the change from a second session.

  • Intermediate
  • Linux VPS
  • 3 min read

By RunRacks Team · Published

Bots try to guess root passwords on every public server, all day. Once you log in with a key as a normal sudo user, you can switch off both root logins and password logins, and those attempts become harmless.

Before you start

You need all of these, or you risk locking yourself out:

1. Check where the current settings come from

Ubuntu 22.04 and 24.04, Debian 12 and AlmaLinux/Rocky 9 read extra files from /etc/ssh/sshd_config.d/ before the main file, and the first value found wins. Some images ship a file there that turns passwords on. See what is set:

Terminal
sudo grep -rEi 'permitrootlogin|passwordauthentication' /etc/ssh/sshd_config /etc/ssh/sshd_config.d/

2. Add a drop-in file

Files are read in alphabetical order, so a name starting with 01- takes priority over files such as 50-cloud-init.conf:

Terminal
sudo nano /etc/ssh/sshd_config.d/01-hardening.conf

Add these lines and save:

Text
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes

If you still need root for automated tools that log in with a key, use PermitRootLogin prohibit-password instead of no.

3. Check the configuration, then reload

sshd -t prints nothing when the file is valid. sshd -T shows the settings SSH will actually use:

Terminal
sudo sshd -t
sudo sshd -T | grep -Ei 'permitrootlogin|passwordauthentication|kbdinteractive'

Reload the service. It is called ssh on Ubuntu and Debian, and sshd on AlmaLinux and Rocky Linux:

Terminal
sudo systemctl reload ssh     # Ubuntu / Debian
sudo systemctl reload sshd    # AlmaLinux / Rocky Linux

Reloading does not drop your current session.

4. Test from a second terminal

Terminal
ssh [email protected]
ssh [email protected]

The first should log in with your key. The second should fail with Permission denied. To prove passwords are off, try ssh -o PubkeyAuthentication=no [email protected]: it should be refused without asking for a password.

Troubleshooting

  • Root can still log in: another file sets the value first. Re-run the grep from step 1 and make sure your file sorts before it.
  • sshd -t reports an error: fix the line it names before reloading. A reload with a broken file keeps the old settings, but a later restart or reboot would fail.
  • Locked out: log in through the browser console (Open Control Panel on your service page), remove /etc/ssh/sshd_config.d/01-hardening.conf and reload SSH.

Next steps

Was this guide helpful?

Still stuck?

Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.

Security

Check the Windows Firewall rule for RDP port 12230

Find and verify the inbound TCP 12230 rule that lets Remote Desktop reach your RunRacks server, recreate it if it was deleted and scope it without locking yourself out.

  • Intermediate
  • Windows VPS · Admin RDP
  • 5 min read

Security

iptables basics: list, allow, drop and save rules

Learn how iptables chains and rule order work, then build a small ruleset that allows SSH and web traffic, drops everything else and can be edited by line number.

  • Intermediate
  • Linux VPS
  • 6 min read