ARTICLE - Security Hardening

SSH Hardening: Key Authentication and Disabling Passwords

SSH is the only remote management channel for most servers, VPS, and cloud instances, and it is the entry point most frequently scanned and brute-forced.

2026-08-06 Mozilla OpenSSH Security Guidelines

SSH Hardening: Key Authentication and Disabling Passwords

SSH is the only remote management channel for most servers, VPS, and cloud instances, and it is the entry point most frequently scanned and brute-forced. According to Mozilla's security guidelines, SSH hardening does not require complex tooling. The core tasks are a handful: switch to key-based authentication, disable password login, block direct root login, tighten the algorithm set, and add Fail2ban for brute-force protection. These changes are completed once and add almost no daily overhead, yet they block the most common weak-password and dictionary attacks at the door.

If you just received a new server, complete the basics in the Server Initialization Security Script article first, then harden SSH step by step.

Why brute force is so common

Most VPS instances are discovered by scanning tools within minutes of boot. /var/log/auth.log often shows records like these:

Feb  3 02:14:31 host sshd[1234]: Failed password for root from 185.220.101.34 port 51234 ssh2
Feb  3 02:14:33 host sshd[1236]: Failed password for root from 185.220.101.34 port 51236 ssh2
Feb  3 02:14:35 host sshd[1238]: Failed password for root from 45.155.205.12 port 40211 ssh2

Most of these come from botnet pools that rotate IPs and keep trying. Pure brute force rarely succeeds against a long, random password — but attackers also combine it with credential stuffing, trying passwords leaked from other platforms. Disabling password authentication kills this attack outright, which is why it offers the best return on effort.

Step 1: Generate and deploy a key pair

Generate a key pair on your workstation, preferring ed25519; use RSA 4096 only when you must support legacy systems.

ssh-keygen -t ed25519 -a 100 -f ~/.ssh/id_ed25519 -C "your-comment"

Then copy the public key to the server (still using a password for the first login):

ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server-ip

Usage notes:

  • Protect the private key with a strong passphrase and never copy it to other machines.
  • Set 600 on the public key file and 700 on the ~/.ssh directory.
  • Use separate keys for separate purposes instead of one key everywhere, and audit existing keys regularly with ssh-keygen -lf.

Step 2: Disable password and direct root login

Edit /etc/ssh/sshd_config and confirm the following settings:

PermitRootLogin no
PasswordAuthentication no
AuthenticationMethods publickey
PubkeyAuthentication yes
LogLevel VERBOSE

Note that PasswordAuthentication no alone does not fully prevent password authentication through PAM. Check /etc/pam.d/sshd as well to make sure password auth is actually disabled. Validate the syntax with sshd -t and restart with systemctl restart sshd. Before confirming the key works, keep an active login session so you do not lock yourself out.

A common practice is to set PasswordAuthentication no first while keeping root login, wait a day or two for key login to stabilize, then add PermitRootLogin no to finish the job with minimal risk.

Step 3: Tighten algorithms and key strength

Mozilla's Modern configuration keeps only ed25519/rsa/ecdsa host keys, uses curve25519 for key exchange, and prefers chacha20-poly1305 and AES-GCM ciphers. It also removes short Diffie-Hellman parameters: all moduli should be at least 3072 bits.

awk '$5 >= 3071' /etc/ssh/moduli > /etc/ssh/moduli.tmp && mv /etc/ssh/moduli.tmp /etc/ssh/moduli

The narrower the algorithm set, the lower the compatibility with older clients. Validate in a test environment before rolling out. The open-source ssh-audit tool quickly checks whether your current sshd algorithm suite matches expectations:

pip install ssh-audit
ssh-audit your-server-ip

Step 4: Fail2ban and access control

Even with key authentication enabled, deploy Fail2ban to block IPs that repeatedly probe the service, and use Match blocks to restrict automation accounts to trusted networks. You can also limit port 22 to specific IPs in your cloud security group or in the firewall guides within the Security Hardening category.

A minimal Fail2ban configuration (/etc/fail2ban/jail.local):

[sshd]
enabled = true
port = ssh
maxretry = 3
bantime = 3600
findtime = 600
ignoreip = 127.0.0.1/8 your-static-ip

Common configuration mistakes and a go-live checklist

These are the most common pitfalls:

  • Changing sshd_config without checking PAM, leaving password auth active.
  • Disconnecting the session before verifying the key, locking yourself out.
  • Overly permissive key file permissions, readable by other users.
  • Changing to a non-default port but forgetting to update firewall and cloud security group rules.

Before going live, verify with the checklist: sshd -T to inspect effective settings, ssh -v to confirm key login, ufw status to confirm which sources can reach port 22, and journalctl -u ssh for login logs. Fold the configuration into the Server Initialization Security Script so it is applied once, and periodically audit authorized_keys with ssh-keygen -lf, removing keys no longer in use.

  • What if I travel without my private key after disabling passwords:Keep a spare key, or have a colleague temporarily allowlist your IP and switch back to keys afterward.
  • How do I manage keys across many servers:Organize aliases with SSH config and manage private keys centrally through Secrets Management with HashiCorp Vault instead of letting them scatter.
  • Can I disable passwords for some accounts only:Yes — use Match User or Match Group blocks to apply per-user settings.
  • Should I add a second factor on top:For stricter setups, layer TOTP or a hardware key onto key login (via PAM MFA); it is especially worthwhile on shared or SSO entry points.

16IDC Observation

For site owners and small teams, SSH hardening is the highest-ROI security step: a few lines of configuration remove the most common brute-force path. The real pitfalls are "losing your session after the change" and "keys scattered everywhere". Consider folding key management into a Secrets Management with HashiCorp Vault approach so access and credentials are consolidated end to end.

Source: https://infosec.mozilla.org/guidelines/openssh
Reference: OpenSSH manual https://www.openssh.com/manual.html
Reference: ssh-audit https://github.com/jtesta/ssh-audit

Configuration & Integration Examples

Example 1: The sshd settings that matter

# /etc/ssh/sshd_config.d/99-hardening.conf
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AuthenticationMethods publickey
MaxAuthTries 3
LoginGraceTime 20
ClientAliveInterval 300
ClientAliveCountMax 2
AllowUsers deploy ops
# Keep only modern algorithm families
KexAlgorithms curve25519-sha256,[email protected]
Ciphers [email protected],[email protected]

Keep an existing session open before you run sshd -t and reload; if the config is wrong you can still get back in.

Example 2: Validate and audit before you call it done

# Syntax check, then reload without dropping sessions
sudo sshd -t && sudo systemctl reload ssh

# Verify from the outside: password and root logins must both be refused
ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no \
    -o BatchMode=yes [email protected]

# Algorithm and configuration audit
ssh-audit example.com

Validate from the outside; local sshd -T only proves the file was read, not that the attack surface actually shrank.

Parameter Values and External Verification

Editing the config file is not the same as applying it. sshd -T prints the parsed runtime values, and those decide the outcome, not whether a line looks right in the file.

Hardening item Key parameters (example) Verification command Pass criterion
Key authentication AuthenticationMethods publickey ssh -v deploy@host The handshake logs publickey and never prompts for a password
Passwords disabled PasswordAuthentication no, KbdInteractiveAuthentication no ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no -o BatchMode=yes Connection refused
Direct root login PermitRootLogin no, AllowUsers deploy ops sshd -T Prints permitrootlogin no
Auth attempts and grace MaxAuthTries 3, LoginGraceTime 20 sshd -T Values match the config file
Connection keepalive ClientAliveInterval 300, ClientAliveCountMax 2 sshd -T Prints 300 and 2
Algorithm set Ciphers [email protected], KexAlgorithms curve25519-sha256 ssh-audit host Only the configured families appear

Passwords and root login have to be verified from an external connection; a local test reuses the already authenticated session and hides the problem.

A passing sshd -t proves syntax only. Whether the algorithm set really narrowed shows up in the ssh-audit listing, and moduli entries below 3072 bits should disappear from it.

Order the work so systemctl reload ssh runs before verification; the old session stays usable if the reload fails, so do not close it until verification passes.

Limitations

  • According to Mozilla's security guidelines, SSH hardening does not require complex tooling.
  • Generate a key pair on your workstation, preferring ed25519; use RSA 4096 only when you must support legacy systems.
  • Before confirming the key works, keep an active login session so you do not lock yourself out.
  • It also removes short Diffie-Hellman parameters: all moduli should be at least 3072 bits.

FAQ

How should key file permissions and passphrases be set?

A strong passphrase, then 600 and 700. Generate with ed25519 and fall back to RSA 4096 only for legacy systems; use a separate key per purpose, audit existing keys with ssh-keygen -lf, and rotate keys whenever a laptop goes missing or a teammate leaves the team.

Why can't I log in after disabling password authentication?

Check the current session first and do not close it. Run sshd -t, then confirm PubkeyAuthentication yes and AuthenticationMethods publickey are set; also inspect /etc/pam.d/sshd, because editing sshd_config alone can leave password authentication alive at the PAM layer, Close the old session after 1 successful key login.

Should Fail2ban replace the firewall, or work alongside it?

Alongside. Fail2ban reacts to log events and bans an IP after maxretry 3 failures within findtime 600, for bantime 3600; the firewall still does the static work of allowing only the ports you need, and both must be updated when the SSH port changes on either side.

Related Providers

  • Vault: when private keys and sudo credentials should be centrally stored and issued by role, manage the secrets here.
  • Tailscale: when port 22 must not face the public internet and several people still log into the same hosts, build a private network and SSH over it.

Community & Resources

Official docs and communities

  • OpenSSH manual — Entry point for command and configuration manuals.
  • sshd_config manual — Semantics and defaults for every option.
  • ssh-audit — Automated algorithm and configuration auditing.
  • Fail2ban — Brute-force protection to pair with key-based auth.

Learning resources and tools

Original source: Mozilla OpenSSH Security Guidelines ↗