SSH hardening: the checklist that actually matters
Password login is on by default, root can still log in by key, and bots get six guesses per connection. Four changes, in order, turn an internet-facing SSH server from an open door into a locked gate.

On this page
In brief
- Password login is on by default and root can still log in with a key: the two settings to change first are PasswordAuthentication and PermitRootLogin.
- Set up key-only login and test it from a second session before disabling passwords, or the first mistake locks you out of your own server.
- AllowUsers names the one account allowed in; MaxAuthTries 2 and LoginGraceTime 30 shrink what a guessing bot gets per connection.
- fail2ban ships its sshd jail disabled: enable it in jail.local and five failed logins in ten minutes earn a ten-minute ban.
- Moving SSH off port 22 only quiets the logs; bots scan every port, so it is not a substitute for any step above.
Put a Linux server on the internet with SSH open and the guessing starts within minutes. Bots work through username and password lists around the clock. Most of them will never get in; the danger is not clever guessing. It is that the default configuration gives it a fair chance: password login is switched on, the root account accepts key logins, and each connection is allowed six authentication attempts before the server gives up.
The fix is four changes, in a specific order. The order matters more than any single setting, because the most common way people break their own server is to disable password login before proving the key login works. Follow the sequence, test at each step, and keep one session open until the end.
The defaults you are starting from
OpenSSH ships with password authentication enabled. The manual page for sshd_config states the default for PasswordAuthentication is yes, and keyboard-interactive authentication, the other route a password can take through PAM, also defaults to yes. PermitEmptyPasswords defaults to no, so passwordless accounts are excluded, but any weak password is a live target.
Root is more subtle. PermitRootLogin defaults to prohibit-password, which sounds like root is locked out. It is not: that setting only blocks password and keyboard-interactive login for root, and a key in root's authorized_keys file still opens the door.
The rate limits are generous. MaxAuthTries defaults to 6, so a bot gets six guesses per connection, and a new connection costs it almost nothing. LoginGraceTime defaults to 120 seconds. ClientAliveInterval defaults to 0, so the server never checks whether an idle session is still alive.
Before changing anything, see the configuration your server is actually running, not just the file you think it is running. The command sudo sshd -T prints the effective configuration, including defaults and any Match blocks. Run it first and keep the output; it is your baseline.
1. Key-only login, tested before you commit
A key pair replaces the password with a file. The private key stays on your machine, the public key goes on the server, and the server challenges your client to prove it holds the private half. A password can be guessed; a 256-bit key cannot, and it is never typed where it could be captured.
On your own machine, generate a key:
ssh-keygen -t ed25519
Accept the default location and set a passphrase on the key. Even if the private key file is copied, it cannot be used without it. Then copy the public key to the server:
ssh-copy-id user@your-server
Now log in with the key and confirm it works. Open a second terminal and log in again while the first session is still open. Only when key login is confirmed, edit /etc/ssh/sshd_config and set:
PasswordAuthentication no
KbdInteractiveAuthentication no
Both lines matter: setting only the first leaves the keyboard-interactive route open, and on systems where PAM handles authentication that route still accepts a password. Check the change with sudo sshd -t, which tests the configuration and keys without starting anything, then restart: sudo systemctl restart ssh on Debian and Ubuntu, sudo systemctl restart sshd on RHEL and Fedora. Do not close your existing session until a fresh key login succeeds.
2. Root stays out
With key-only login in place, close the root account to SSH entirely:
PermitRootLogin no
Admin work still happens through sudo from your own account. Root is the first username every bot tries, and its compromise means total control. Removing it from SSH does not remove it from the machine; it removes it from the network.
If a backup script genuinely needs root over SSH, the manual offers forced-commands-only, which allows root key login only when the key is restricted to a single command. That is a special case for automated backups, not a default.
3. Name who is allowed in
By default, every account on the machine can attempt an SSH login. Narrow that to the accounts that actually need it:
AllowUsers deploy
Everyone else is refused before authentication starts, and the server stops confirming whether a username exists.
Then tighten the guessing budget:
MaxAuthTries 2
LoginGraceTime 30
ClientAliveInterval 300
ClientAliveCountMax 2
Two attempts instead of six, thirty seconds instead of two minutes, idle sessions cut after about ten minutes. A legitimate user authenticates once and works; a bot's job gets slower and noisier.
If the server needs no forwarding at all, switch it off in one line with DisableForwarding, which covers X11, agent, TCP, and socket forwarding. If you do forward ports through SSH, leave it alone and restrict the individual options instead.
4. Ban the repeat guessers
The settings above limit each attempt. fail2ban limits the attempts themselves. It watches the authentication log and blocks, at the firewall, any address that fails too often.
Install it through your package manager, then enable its SSH jail, which ships disabled: the default configuration sets enabled to false for every jail. Create /etc/fail2ban/jail.local with:
[sshd]
enabled = true
Do not edit jail.conf itself; that file belongs to the package and your changes would be overwritten on upgrade. Start the service with sudo systemctl enable --now fail2ban, and check it with sudo fail2ban-client status sshd.
A host is banned after five failed logins within ten minutes, and the ban lasts ten minutes. Five failures absorbs a mistyped passphrase; a ten-minute ban makes a password list useless, since a bot waiting ten minutes between every five guesses will take years to work through one. If your logs show a persistent source, raise the ban time in jail.local rather than lowering the failure count.
What this checklist leaves out on purpose
Moving SSH off port 22 is not on the list. It cuts the noise in your logs dramatically, because most bots only knock on 22, but bots that scan the full port range will find the new port in seconds. Treat a port change as log hygiene, not security, and do it only after the steps above.
A firewall is a separate layer: it decides who can reach port 22 at all, while this checklist decides what happens once they do. Neither replaces the other.
Keeping OpenSSH itself updated matters as much as the configuration. The settings above assume a current release; the defaults quoted here come from the current manual pages. A server that has not been updated in two years may behave differently, so check its version first.
Verify it, then walk away
Run sudo sshd -T again and compare it with the baseline from the start. PasswordAuthentication and KbdInteractiveAuthentication should read no, PermitRootLogin should read no, AllowUsers should list only your accounts. Then log in fresh from another session and confirm the key works and a password login is refused. Check sudo fail2ban-client status sshd after a day; the banned list shows what the server was facing all along.
The whole job takes under an hour, and most of it is testing rather than changing. A server that refuses passwords, refuses root, refuses everyone but you, and bans anyone who keeps guessing, is a server you can stop worrying about.


