keepcalculator

06 Numbers and Powers

Secure OpenSSH Server: Hardening Steps That Work

Card No. 06·74325 · Updated · Standard formula, shown below

Securing an OpenSSH server means reducing its attack surface through configuration changes, key-based authentication, and network-level controls. This guide explains the essential hardening steps, why they matter, and how to implement them without breaking your workflow.

Why OpenSSH Needs Hardening

OpenSSH is the standard remote administration tool on Linux and macOS, and its default configuration prioritizes compatibility over security. Out of the box, it allows password authentication, listens on all interfaces, and accepts protocol versions that are decades old. Attackers constantly scan for SSH servers on port 22 and attempt brute-force password guessing, so leaving defaults in place is risky. Hardening is the process of changing those defaults to match your actual usage, eliminating unused features, and enforcing stronger authentication.

Core Configuration Changes

The main configuration file is /etc/ssh/sshd_config. After any change, restart with sudo systemctl restart sshd (or sudo service ssh restart on older systems). Always keep a second terminal session open while testing changes so you don’t lock yourself out.

  • Disable root login: Set PermitRootLogin no. Root should be accessed via sudo after logging in as a normal user.
  • Use key-based authentication only: Set PasswordAuthentication no and PubkeyAuthentication yes. Generate a key pair with ssh-keygen -t ed25519 on your client, then copy the public key to the server using ssh-copy-id before disabling passwords.
  • Change the listening port: Set Port 2222 (or any unprivileged port above 1024). This reduces automated scan noise, though it is not real security—obscurity alone is insufficient.
  • Limit allowed users: Add AllowUsers alice bob to restrict who can log in. This blocks accounts that exist on the system but are not meant for SSH.
  • Set idle timeout: ClientAliveInterval 300 and ClientAliveCountMax 0 will disconnect idle sessions after 5 minutes.
  • Disable empty passwords: Set PermitEmptyPasswords no (this is default, but explicit is safer).

Authentication Methods and Key Management

SSH keys come in several types. Ed25519 is the modern default—fast, secure, and supported by all current OpenSSH versions. RSA keys with 4096 bits are still acceptable but slower. DSA and ECDSA with small keys should be avoided. Store private keys locally with chmod 600 permissions, and never share them. For additional security, protect the private key with a passphrase, or use an SSH agent to avoid typing it repeatedly.

On the server, the authorized_keys file (usually ~/.ssh/authorized_keys) must have permissions 600 and its parent directory 700. If permissions are too open, OpenSSH will refuse to use the keys. You can also add restrictions directly in the authorized_keys file, such as no-port-forwarding,no-agent-forwarding for keys used only for tunneling.

Network-Level Protections

Even with strong authentication, you can reduce exposure at the network layer:

  • Firewall rules: Use ufw or iptables to allow SSH only from specific IP ranges. For example, sudo ufw allow from 192.168.1.0/24 to any port 22.
  • Fail2ban: This tool monitors auth logs and temporarily bans IPs after repeated failed attempts. Configure it to watch the sshd log and set a ban time of 10 minutes or more.
  • TCP wrappers: On older systems, /etc/hosts.allow and /etc/hosts.deny can restrict access, but modern distributions have moved away from this in favor of firewalls.
  • VPN or port knocking: For high-security environments, place SSH behind a VPN or use port knocking to hide the port entirely. These add complexity but are effective against scanners.

Common Mistakes and Misconceptions

One frequent error is disabling password authentication before copying your public key—this locks you out. Another is changing the port and forgetting to update firewall rules, which makes the server unreachable. A common misconception is that changing the port is enough; it only stops casual scans. Also, some administrators disable password auth but leave PermitRootLogin prohibit-password, which still allows root with a key. If you want no root logins, use no.

Another mistake is ignoring SSH protocol versions. OpenSSH 8.8 and later disable the old SSH1 protocol by default, but if you run an older version, ensure Protocol 2 is set. Finally, many people forget to update OpenSSH itself. Security patches are released regularly, so use your package manager to keep it current.

Comparing Security Options

There is no single “secure” setting; it depends on your threat model. For a home server on a residential connection, disabling password auth and changing the port may be sufficient. For a corporate server exposed to the internet, you should add fail2ban, restrict by IP, and consider a VPN. Key-based auth alone is a major improvement over passwords because keys are not susceptible to brute-force guessing. Multi-factor authentication (e.g., TOTP) is possible with PAM modules but adds complexity and is rarely needed for SSH if keys are used properly.

Monitoring and Auditing

After hardening, verify that your changes work. Check sudo tail -f /var/log/auth.log (or journalctl -u ssh) for login attempts. Look for unusual patterns like many failed logins from the same IP. You can also run ssh -v from a client to see the authentication steps. Periodically review the list of authorized keys on the server—remove any you don’t recognize. Use sudo sshd -t to test the configuration file syntax before restarting.

Frequently Asked Questions

Is changing the SSH port really useful?

It reduces automated attacks from bots that scan port 22, but it is not a security measure by itself. A determined attacker can scan all ports. Combine it with key-only auth and firewall rules for real protection.

What is the difference between ed25519 and RSA keys?

Ed25519 is faster, smaller, and considered more secure for the same key size. RSA with 4096 bits is still secure but slower to generate and use. Ed25519 is the recommended default in modern OpenSSH.

Can I use SSH keys with a passphrase on the server?

No, the passphrase protects the private key on your client machine. The server only sees the public key. You enter the passphrase when using the key locally, not when connecting.

Why does my server refuse key authentication after I set permissions correctly?

Check the home directory permissions. The user’s home directory must not be writable by others (typically 755 or 700). Also ensure the .ssh directory is 700 and authorized_keys is 600. If using SELinux, restore contexts with restorecon -R -v ~/.ssh.

Should I disable password authentication entirely?

Yes, if you have set up key-based access and tested it. This eliminates the risk of brute-force password guessing. Keep a backup console access (like a physical keyboard or out-of-band management) in case you lose your keys.

What is fail2ban and do I need it?

Fail2ban is a log-monitoring tool that bans IPs after repeated failures. It is useful on internet-facing servers, but if you already restrict by firewall to trusted IPs, it may be redundant. It adds a layer of defense against new attack sources.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →