Skip to content

SSH hardening checklist for Linux servers

To harden SSH, allow key-based logins only, disable direct root login, restrict who may log in and from where, and keep OpenSSH patched. Put the settings in a drop-in file under /etc/ssh/sshd_config.d/, check them with sshd -t and sshd -T, and test a second login before you close your current session.

A hardened SSH service accepts keys instead of passwords, does not let root log in directly, is reachable only for the users and networks that need it, and runs a patched OpenSSH. On current Debian and Ubuntu you can get there in well under an hour. The one real risk is locking yourself out, so this checklist starts with how to test safely.

Before you change anything: how do you avoid a lockout?

Three habits make SSH changes safe:

  • Keep your current session open. A running session survives a reload of the service. It is your way back if the new configuration is wrong.
  • Check the syntax first. sudo sshd -t prints nothing when the configuration is valid and names the file and line when it is not.
  • Test with a second login. Open a new terminal and connect again. Close the old session only after that works.

If your provider offers a rescue or serial console, find out how to open it before you need it.

Where do the settings belong?

On Debian 12 and Ubuntu 24.04 the main file /etc/ssh/sshd_config starts with this line:

Include /etc/ssh/sshd_config.d/*.conf

Put your own settings into a file there instead of editing the main file. Package updates then never ask you to merge changes, and everything you changed is in one place.

One rule matters here: for each option, sshd uses the first value it finds. The drop-in files are read at the top of the main file, in alphabetical order. A file named 10-hardening.conf therefore wins over 50-cloud-init.conf, which many cloud images ship with PasswordAuthentication yes, and over anything further down in sshd_config itself.

Step 1: Are your keys in place?

Create a key pair on your own computer, not on the server, and protect it with a passphrase:

ssh-keygen -t ed25519 -a 100 -C "laptop-2026"
ssh-copy-id -i ~/.ssh/id_ed25519.pub deploy@web-01

Then log in with the key and confirm that sudo works for that user. Do this before you disable passwords. Every person gets their own key and, ideally, their own account; shared keys cannot be withdrawn from one person only.

Step 2: Which sshd settings should you set?

Create /etc/ssh/sshd_config.d/10-hardening.conf:

# Keys only
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no

# No direct root login
PermitRootLogin no

# Only these accounts may log in
AllowUsers deploy admin

# Less attack surface
MaxAuthTries 3
LoginGraceTime 30
X11Forwarding no

What the lines do:

  • PasswordAuthentication no and KbdInteractiveAuthentication no together switch off password logins. The second one matters because PAM can otherwise still ask for a password.
  • PermitRootLogin no forces a personal account plus sudo, so the log shows who did what. The default prohibit-password already blocks root passwords but still allows root with a key.
  • AllowUsers is a whitelist. Warning: anyone not listed is locked out immediately after the reload, including you if you mistype your own user name. AllowGroups with a dedicated group is easier to maintain on servers with many accounts.
  • MaxAuthTries 3 ends a connection after three failed attempts. Note that every key your client offers counts as an attempt.
  • X11Forwarding no removes a feature that servers rarely need. Leave TCP forwarding alone unless you know nobody uses tunnels or jump hosts.

If you need SFTP-only accounts, backup users or automation keys, restrict them with a Match User block or with options in authorized_keys rather than loosening the global rules.

Step 3: How do you check and activate the configuration?

sudo sshd -t
sudo sshd -T | grep -Ei '^(permitrootlogin|passwordauthentication|kbdinteractiveauthentication|maxauthtries|x11forwarding|allowusers)'

sshd -t stays silent on success. sshd -T prints the effective configuration after all includes:

maxauthtries 3
permitrootlogin no
passwordauthentication no
kbdinteractiveauthentication no
x11forwarding no
allowusers deploy
allowusers admin

If a line still shows the old value, another file sets the option earlier. Find it with grep -ri passwordauthentication /etc/ssh/sshd_config /etc/ssh/sshd_config.d/.

Then reload the service and test from a second terminal:

sudo systemctl reload ssh
ssh deploy@web-01

On Debian and Ubuntu the unit is called ssh; on RHEL, AlmaLinux and Rocky Linux it is sshd. Ubuntu 24.04 starts the service through ssh.socket. That makes no difference for the options above, but if you change Port or ListenAddress there, run sudo systemctl daemon-reload and sudo systemctl restart ssh.socket afterwards, because the listening socket belongs to systemd.

To confirm that passwords are really refused, force a password attempt from your computer:

ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password deploy@web-01

The expected answer is Permission denied (publickey).

Step 4: Who can reach port 22 at all?

The strongest protection is not being reachable. If your team connects from fixed addresses or through a VPN, allow only those. With ufw:

sudo ufw allow from 203.0.113.10 to any port 22 proto tcp
sudo ufw status numbered

Warning: add and verify the allow rule before you run sudo ufw enable or delete a broader rule, otherwise the firewall cuts your own connection. If addresses change too often for a whitelist, sudo ufw limit 22/tcp at least slows down repeated connection attempts.

Moving SSH to another port reduces log noise from automated scans. It is cosmetic, not protection, and it adds one more thing every colleague and every script has to know.

Step 5: Do you still need fail2ban?

With passwords disabled, guessing cannot succeed any more. fail2ban still has a use: it blocks addresses that keep hammering the service, which keeps logs readable. Install it with sudo apt install fail2ban and enable the jail in /etc/fail2ban/jail.local:

[sshd]
enabled = true
backend = systemd
maxretry = 5
bantime = 1h

backend = systemd is needed on systems that no longer write /var/log/auth.log, such as a default Debian 12. Check the result with sudo fail2ban-client status sshd.

Step 6: Is OpenSSH itself up to date?

Configuration does not help against a flaw in the software. Install security updates promptly, ideally automatically, and reload or restart the service when the package asks for it:

apt list --upgradable 2>/dev/null | grep -i openssh
ssh -V

How do you see who logged in?

sudo journalctl -u ssh --since today | grep -E 'Accepted|Failed|Invalid'
last -n 10

Accepted publickey for deploy lines are your successful logins. Failed and Invalid user lines from unknown addresses are normal background noise on any public server; what deserves attention is an Accepted line you cannot explain.

Common mistakes

  • Disabling passwords before the key login was tested. Always test the key first, with the account you will use afterwards.
  • Editing only the main file. A drop-in that is read earlier keeps the old value. Trust sshd -T, not the file you edited.
  • Closing the session right after the reload. The reload does not prove that a new login works.
  • One shared key for the whole team. When someone leaves, you have to replace it everywhere.
  • Private keys without a passphrase on laptops. A stolen laptop then equals server access.
  • Forgetting old entries in authorized_keys. Review the files once in a while and remove keys nobody can attribute.

Checklist

  1. Personal accounts and ed25519 keys for every admin, key login tested.
  2. Drop-in file with PasswordAuthentication no, KbdInteractiveAuthentication no, PermitRootLogin no.
  3. AllowUsers or AllowGroups set, own account included.
  4. sshd -t silent, sshd -T shows the expected values.
  5. Service reloaded, second login tested, password login refused.
  6. Port 22 limited in the firewall where possible.
  7. Automatic security updates active.
  8. authorized_keys reviewed, old keys removed.

How Lynx Heartbeat helps

Lynx Heartbeat does not audit your sshd_config; the steps above stay your job. What it does is watch the surroundings: the agent checks every minute whether the SSH service is running, and it reports pending updates and known vulnerabilities in installed packages, OpenSSH included. A TCP check from outside can tell you when port 22 stops answering. The agent itself opens no port on your server and only makes an outbound connection. More on the features page. If you also want fewer, better alerts, read how to avoid alert fatigue, and for the other classic that expires unnoticed, TLS certificate expiry monitoring.

Frequently asked questions

What are the most important SSH hardening steps?

Use key-based authentication and switch off password logins, disable direct root login, limit which users may log in, restrict port 22 in the firewall where you can, and install security updates for OpenSSH promptly. Everything else is fine-tuning on top of these five.

Does changing the SSH port make a server more secure?

Not really. A different port reduces noise from automated scans in your logs, but anyone who scans all ports still finds the service. It does not replace keys, a firewall rule or updates.

How do I avoid locking myself out when changing sshd_config?

Keep your current session open, run sshd -t to check the syntax, reload the service, and then open a second connection in a new terminal. Close the first session only when the new login works. If the provider offers a rescue console, make sure you know how to reach it.

Why does my PasswordAuthentication no setting have no effect?

sshd uses the first value it reads for each option. Files in /etc/ssh/sshd_config.d/ are included at the top of sshd_config and read in alphabetical order, so an earlier file, for example one written by cloud-init, can win. sshd -T shows the value that is actually in effect.

Is fail2ban still necessary when passwords are disabled?

It is no longer essential, because guessing passwords cannot succeed against key-only logins. It still cuts down log noise and load from repeated attempts, so many admins keep it as a second layer.

Security

Related articles

All articles