Within minutes of going online, a new server is being probed by bots: the log fills with thousands of attempts to log in as root, admin, ubuntu. Most of them are harmless if the server is set up properly. Here is what really protects SSH — and what only feels like protection.

Keys instead of passwords

The main measure is keys instead of passwords. An Ed25519 key cannot be brute-forced, and the private half never leaves your computer. Create a pair with the SSH key generator or ssh-keygen -t ed25519, protect the private key with a passphrase and add the public key to ~/.ssh/authorized_keys on the server (ssh-copy-id user@server).

Even better is a hardware key (YubiKey and similar) with the ed25519-sk type: the private key lives in the device, and you touch it to log in.

sshd settings

Once key login works, turn off passwords and root login. The tidiest way is a separate file in /etc/ssh/sshd_config.d/ — the sshd settings generator builds it for your OpenSSH version:

PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
MaxAuthTries 3
LoginGraceTime 30

One subtlety: sshd uses the first value it finds, and on cloud servers the 50-cloud-init.conf file often turns passwords back on. So your file’s name should start with 00-. After any change, check the syntax with sshd -t and see the effective values with sshd -T.

Never close your current session until you have tested logging in from a new terminal window: a mistake in the settings can lock you out.

Firewall and attempt limits

What barely helps

MeasureWhat it really gives
A non-standard portLess noise in the logs, but a scanner finds SSH on any port. No substitute for keys.
A complex root passwordBetter than a simple one, but a password can be stolen or shoulder-surfed. Keys are safer.
Port knockingHides the port but complicates life and breaks if the setup is lost. Allowing SSH only from your own addresses is more reliable.
fail2ban with key-only loginAdds little protection, but lowers the load and the noise.

Updates and a second factor

In 2024 the regreSSHion vulnerability (CVE-2024-6387) was found in OpenSSH: it allowed running code on the server without a password. The only defence against such bugs is regular updates, ideally automatic (unattended-upgrades on Debian and Ubuntu). For especially important servers, add a second factor: a hardware key or one-time codes through PAM — easy to cross-check with the TOTP code generator.

Do not forget the client

On the first connection, compare the server’s key fingerprint instead of typing “yes” blindly: this protects you from a substituted server. Do not enable agent forwarding (ForwardAgent) to servers you do not trust, and keep private keys only on your own devices. If a key leaks, remove it from authorized_keys on every server.