Hardening SSH on a Linux server: keys only, no root, a safe rollout
Switch a Linux server to key-only SSH, disable root login and limit who can connect, using an sshd_config.d drop-in, sshd -t and sshd -T, without locking yourself out.
Contents
Why SSH is worth ten minutes of your time
On almost every Linux server I look after, SSH is the one service that is open to the internet on purpose. Within minutes of a new VPS getting a public IP, the auth log starts filling with login attempts for users like root, admin and test. Most of them are dumb password guessing. The simplest way to make all of that irrelevant is to stop accepting passwords at all and only allow key-based logins for a short list of named users.
This post is the routine I follow on Debian and Ubuntu servers. It works the same way on RHEL-family systems, except that the service is called sshd instead of ssh.
Step 1: create a key and install it
On your own machine, create an Ed25519 key if you do not have one yet. The -a flag sets the number of key derivation rounds that protect the passphrase.
ssh-keygen -t ed25519 -a 100 -C "mahmud@laptop"
ssh-copy-id -i ~/.ssh/id_ed25519.pub deploy@203.0.113.10
Then log in with the key and confirm it really is the key doing the work:
ssh -v deploy@203.0.113.10 2>&1 | grep "Authenticated"
You want to see a line ending in using "publickey". Make sure this user can use sudo before you go further, because root login is about to go away.
Step 2: put your settings in a drop-in file
Current Debian and Ubuntu releases ship an /etc/ssh/sshd_config that starts with this line:
Include /etc/ssh/sshd_config.d/*.conf
I prefer to leave the main file alone and put my changes in a drop-in. That keeps package upgrades clean, because there is no modified config file to merge.
One detail matters here: for most keywords, sshd uses the first value it reads, not the last. The included files are read in lexical order and before the rest of the main file. Some cloud images ship a file like 50-cloud-init.conf that sets PasswordAuthentication yes. If your own file is named 99-hardening.conf, the cloud-init value wins and your setting is silently ignored. Give your file a low number:
sudo tee /etc/ssh/sshd_config.d/10-hardening.conf > /dev/null <<'EOF'
# Keys only
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitEmptyPasswords no
# No direct root login
PermitRootLogin no
# Only members of this group may log in
AllowGroups sshusers
# Less time and fewer tries for unauthenticated clients
LoginGraceTime 30
MaxAuthTries 3
# Features most servers do not need
X11Forwarding no
EOF
Create the group and add every account that should keep access, before you apply the change:
sudo groupadd --system sshusers
sudo usermod -aG sshusers deploy
AllowGroups is a powerful line. Anyone who is not in that group is refused even with a valid key, so double-check the membership with getent group sshusers.
Step 3: test before you reload
Never reload sshd with a config you have not checked. sshd -t parses the configuration and exits with an error if something is wrong:
sudo sshd -t && echo "config OK"
A syntax check does not tell you which value actually won, though. For that, sshd -T prints the effective configuration after all includes are resolved:
sudo sshd -T | grep -Ei '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin|allowgroups|maxauthtries)'
The output should show passwordauthentication no, permitrootlogin no and your group. If the password line still says yes, another file read earlier is setting it. Find it with:
sudo grep -ri passwordauthentication /etc/ssh/sshd_config /etc/ssh/sshd_config.d/
Step 4: reload with a safety net
Keep your current SSH session open. A reload does not drop existing connections, so that session is your way back in if something goes wrong.
sudo systemctl reload ssh # "sshd" on RHEL, Fedora and friends
Now open a second terminal and test a fresh login with your key. Then confirm that passwords are really refused by forcing the client to try one:
ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password deploy@203.0.113.10
The expected answer is Permission denied (publickey). The word in brackets is the list of methods the server still offers, and publickey should be the only one. Only after both checks pass do I close the original session.
If you are on a cloud VM, also make sure you know where the provider’s serial or web console is. It is the last way in if you ever lock yourself out completely.
Optional: restrict individual keys
The authorized_keys file accepts options in front of each key. Two that I use for automation keys:
from="198.51.100.0/24",restrict ssh-ed25519 AAAAC3Nza... ci-deploy
restrict,command="/usr/local/bin/run-backup" ssh-ed25519 AAAAC3Nza... backup-job
restrict turns off port forwarding, agent forwarding, X11 forwarding and the PTY for that key. from= limits the source addresses, and command= forces a single command no matter what the client asks to run. A leaked CI key with these options can do very little.
What this does and does not solve
Key-only authentication removes password guessing as an attack, but it does not make the noise in the logs disappear, and it does not protect a key that sits unencrypted on a stolen laptop. Use a passphrase or a hardware key, and combine this with a host firewall that only exposes port 22 where it needs to be reachable. Moving SSH to another port reduces log noise, but it is not a security control on its own.
The whole change is one small file, two test commands and a reload. It is one of the first things I do on any new server, and it is easy to repeat with a configuration tool once you have more than a handful of machines.