Why Turn Root Login Off
Every server has a root account, so bots try it first. With direct root login off, an attacker needs both a user name and that user's key, and your logs show which person ran a command through sudo.
Before You Start
This change can lock you out. Do these steps first, in this order:
- Create a normal user with sudo rights: see how to create a sudo user.
- Copy your key to that user with ssh-copy-id.
- In a second terminal, log in as that user with the key and run
sudo -v. - Keep your current session open until the end.
- Find the server console in your provider's control panel, in case you need it.
The Three Values
PermitRootLogin no: root cannot log in over SSH at all. The recommended setting once you have a sudo user.PermitRootLogin prohibit-password: root may log in with a key, never with a password. This is the OpenSSH default when nothing is set, and suits automation that must connect as root.PermitRootLogin yes: root may log in with a password. Do not use it on a server reachable from the internet.
Step 1: See the Current Value
sudo sshd -T | grep -i permitrootlogin
sudo grep -ri permitrootlogin /etc/ssh/sshd_config /etc/ssh/sshd_config.d/
The first command prints the value sshd uses. The second shows where it is set. sshd uses the first value it reads, and on Ubuntu, Debian, AlmaLinux, and Rocky Linux the files in /etc/ssh/sshd_config.d/ are read, in lexical order, before the rest of the main file. Some installers and images place a drop-in there that sets PermitRootLogin yes. On AlmaLinux and Rocky Linux, the installer writes 01-permitrootlogin.conf when root login with a password was allowed during setup. Change that line to no too.
Step 2: Set the New Value
sudo nano /etc/ssh/sshd_config.d/00-root-login.conf
PermitRootLogin no
A drop-in whose name sorts first is read before the others. Editing the line in /etc/ssh/sshd_config also works when no drop-in sets the keyword.
Step 3: Test and Reload
sudo sshd -t
sudo sshd -T | grep -i permitrootlogin
No output from the first command means the files are valid; the second should print permitrootlogin no. Then reload, which leaves open sessions connected:
sudo systemctl reload ssh # Ubuntu, Debian
sudo systemctl reload sshd # AlmaLinux, Rocky Linux
Step 4: Test from a Second Terminal
ssh user@your_server_ip
ssh -l root your_server_ip
The first login must work. The second must be refused with "Permission denied". Close the old session only after both results are right.
Working Without Root Login
- Single commands: prefix them with
sudo. - A root shell:
sudo -i, thenexitwhen done. - Files for root-owned folders: upload to your home folder with scp, then move them with
sudo mv. - Emergencies: the provider's console still accepts root, because it does not use SSH.
What About the Root Password?
This setting only affects SSH. Root can still log in on the provider's console and through su, so keep a strong root password if you rely on the console for emergencies; see how to change the root password. If a tool must connect as root, use prohibit-password with a key for that purpose rather than allowing root passwords, and test with sudo sshd -t before you reload.
Combine this with key-only logins: see how to disable SSH password authentication.