How-to

Harden SSH on a Fresh Linux Machine

An internet-facing SSH server meets bots within minutes. A short, ordered checklist turns it from a target into a dead end, without risking your own access.

What matters most when hardening SSH?

Getting rid of passwords. Almost every successful SSH break-in on servers comes from guessed or reused passwords, not from flaws in the protocol. Once only keys can log in, automated attacks become noise in the log. The other steps on this page narrow who may try, slow down repeat offenders and keep the software current. Work through them in order, keep one session open, and test each change from a second terminal before moving on.

Step 1: Which accounts should be allowed at all?

Make a group for people who may use SSH and allow only that group. Service accounts, database users and application users then cannot log in even if someone sets a password on them.

groupadd sshlogin
usermod -aG sshlogin mara
cat > /etc/ssh/sshd_config.d/30-who.conf <<'EOF'
AllowGroups sshlogin
PermitRootLogin prohibit-password  # root by key during setup only
EOF

If root should keep key access during setup, add root to the group or use AllowGroups sshlogin root. Change root to no later, as covered in SSH root login settings.

Step 2: How do I switch to keys only?

Confirm key login works for every account in the group, then turn off the password methods. KbdInteractiveAuthentication is the current name for what older guides call ChallengeResponseAuthentication.

cat > /etc/ssh/sshd_config.d/31-keys.conf <<'EOF'
PasswordAuthentication no          # no typed secrets at all
KbdInteractiveAuthentication no    # closes the PAM prompt path too
PubkeyAuthentication yes
AuthenticationMethods publickey    # one method, nothing else
EOF
sshd -t && sshd -T | grep -E '^(passwordauthentication|kbdinteractive)'

If sshd -T still prints passwordauthentication yes, an earlier drop-in sets it; first value wins. Fix that file rather than fighting it.

Step 3: How do I slow down guessers?

Lower the number of attempts per connection and the time allowed to finish logging in:

MaxAuthTries 3
LoginGraceTime 20
MaxStartups 10:30:60

Then add a rate limiter. Debian 13 ships OpenSSH 10.0, which includes PerSourcePenalties, switched on by default since release 9.8: addresses that fail repeatedly are refused for growing periods, all inside sshd. On Ubuntu 24.04 and the Enterprise Linux images, whose OpenSSH versions are older, install fail2ban and enable its sshd jail. Either one turns a scanner hammering your port into a few log lines.

Step 4: Should SSH be open to the whole internet?

Only if you log in from changing places. If you work from fixed addresses, an office line or a VPN endpoint, allow SSH only from those in the firewall and drop everything else. It is the single most effective step after keys. With firewalld:

firewall-cmd --permanent --new-zone=sshadmins
firewall-cmd --permanent --zone=sshadmins --add-source=198.51.100.0/24
firewall-cmd --permanent --zone=sshadmins --add-service ssh   # only listed sources reach port 22
firewall-cmd --permanent --remove-service=ssh   # default zone stops answering
firewall-cmd --reload

Before running the last line, make sure your own address is inside the trusted range. Moving the port, described in Change the SSH port, is a lighter alternative when address limits are impractical.

Step 5: Is a second factor worth it?

For machines that hold customer data, yes. The cleanest option is a hardware security key: OpenSSH supports ed25519-sk keys stored on FIDO2 tokens, so a stolen laptop file is useless without the physical key and a touch. Create one with ssh-keygen -t ed25519-sk -O resident. Alternatively, a TOTP app through the libpam-google-authenticator module adds a code prompt after the key, set with AuthenticationMethods publickey,keyboard-interactive.

Step 6: How do I trim what a session may do?

  • X11Forwarding no unless you run graphical tools.
  • AllowAgentForwarding no on machines that never jump to others.
  • AllowTcpForwarding no for accounts that only need a shell; set it per group with a Match Group block.
  • ClientAliveInterval 300 and ClientAliveCountMax 2 to close dead sessions.

Step 7: How do I check the result?

Run an outside audit. The open source ssh-audit tool connects to your server and grades its key exchange, ciphers and host keys:

pipx install ssh-audit
ssh-audit -p 22 203.0.113.25

Current packages on our images pass with few or no warnings. Finally, keep OpenSSH updated: unattended-upgrades on Debian and Ubuntu, dnf-automatic on Enterprise Linux, and freebsd-update plus pkg upgrade on FreeBSD.

What does the network already do for me?

Edge DDoS filtering sits in front of every machine and absorbs floods that try to fill the port. It does not judge login attempts, so the steps above remain your job. The table below lists the machines where people most often run this checklist as a first task.

Machines Where This Checklist Is Common

ServerProcessorCores / threadsMemoryStorageNetworkPer monthSmallest order
E1Intel Xeon E3-1240 v34 / 816 GB DDR32x 240 GB SSD2x 1 Gbps$30.52$91.56 (3 months)
S1Intel Xeon E-2276G6 / 1264 GB DDR41x 1 TB NVMe2x 10 Gbps$53.32$53.32 (1 month)
S13AMD EPYC 4345P8 / 16128 GB DDR52x 2 TB NVMe2x 25 Gbps$237.92$237.92 (1 month)

Keep Reading

Change the SSH port

Move sshd off port 22 on Ubuntu, Debian, Rocky, AlmaLinux and FreeBSD without locking yourself out of the machine.

Open

SSH root login settings

What each PermitRootLogin value does, how to move daily work to a sudo user and when key-only root login is reasonable.

Open

Edge DDoS filtering

How common volumetric attacks are filtered at the network edge before they reach your ports, and what you still handle on the box.

Open

SSH keys

How SSH key pairs replace passwords, how to make an ed25519 key and how to have it placed on your machine at install time.

Open

FAQ

Quick Answers

Disabling password authentication once key login works. It stops nearly all automated break-in attempts.

OpenSSH 9.8 and later include PerSourcePenalties, which covers the basics. fail2ban is still useful on older versions and for custom ban rules.

Yes, if you connect from fixed addresses. It is one of the strongest protections available.

Yes. OpenSSH supports ed25519-sk keys on FIDO2 security tokens.

Run ssh-audit against your server to grade its algorithms and host keys.