keepcalculator

06 Numbers and Powers

SSH Hardening Best Practices

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

SSH hardening means configuring your SSH server and clients to resist unauthorized access, credential theft, and brute-force attacks. This article covers the essential steps: key-based authentication, disabling password logins, changing the default port, and more. Below is a practical guide you can apply to any Linux or Unix-like server running OpenSSH.

Why SSH Security Matters

SSH (Secure Shell) is the standard remote administration protocol for Linux and Unix systems. It encrypts all traffic, but the default configuration is often too permissive. Attackers constantly scan the internet for port 22, the default SSH port, and attempt dictionary attacks or exploit misconfigurations. A single weak password or an exposed root account can lead to a full system compromise. Hardening SSH reduces the attack surface and ensures that only authorized users can connect.

Core Hardening Steps

The following steps form the foundation of SSH hardening. Apply them in order, and test each change before moving to the next.

  1. Use SSH key authentication – Generate a key pair on your client (ssh-keygen) and copy the public key to the server’s ~/.ssh/authorized_keys file. This replaces password logins with cryptographic authentication.
  2. Disable password authentication – In /etc/ssh/sshd_config, set PasswordAuthentication no. This stops brute-force password guessing entirely.
  3. Disable root login – Set PermitRootLogin no (or prohibit-password if you need key-based root). Always log in as a regular user and use sudo for administrative tasks.
  4. Change the default port – Set Port 2222 (or another high port) to reduce automated scans. This is not security by itself, but it cuts down on log noise.
  5. Limit user access – Use AllowUsers or AllowGroups to specify exactly who can log in.
  6. Set idle timeout – ClientAliveInterval 300 and ClientAliveCountMax 0 will disconnect idle sessions after 5 minutes.
  7. Use a firewall – Restrict SSH access to specific IP addresses using iptables, nftables, or a cloud security group.
  8. Enable fail2ban – This tool bans IPs after repeated failed login attempts, adding another layer of brute-force protection.

Understanding SSH Configuration Files

The main server configuration file is /etc/ssh/sshd_config. Client-side settings are in ~/.ssh/config. After any change to the server file, restart the service with sudo systemctl restart sshd (or sudo service ssh restart on older systems). Always keep a second terminal session open when testing changes, so you can revert if you lock yourself out.

Common Mistakes and Pitfalls

  • Locking yourself out – Forgetting to add your public key before disabling passwords. Always test key login first.
  • Using weak key types – RSA keys with less than 2048 bits are considered weak. Use Ed25519 or RSA 4096.
  • Ignoring SSH agent forwarding – This can expose your private keys on intermediate servers. Use ssh -A only when necessary, or use ProxyJump instead.
  • Not updating OpenSSH – Old versions may have known vulnerabilities. Keep your system updated.
  • Setting permissions incorrectly – The ~/.ssh directory should be 700, and authorized_keys should be 600. Otherwise SSH may refuse to use them.

Comparing Authentication Methods

Passwords are the weakest link because they can be guessed or phished. Key-based authentication uses a private key stored on your client and a public key on the server. It is resistant to brute-force because the key is mathematically paired. Two-factor authentication (2FA) adds a time-based one-time password (TOTP) as a second factor, but it requires additional setup. For high-security environments, consider using SSH certificates, which allow centralized management and revocation.

Advanced Hardening Techniques

  • Restrict SSH to specific interfaces – If you only need SSH from a VPN, bind SSH to the VPN interface with ListenAddress.
  • Use a jump host (bastion) – Place a hardened SSH server in a DMZ that forwards connections to internal servers, so internal machines are not directly exposed.
  • Disable unused features – Turn off X11 forwarding (X11Forwarding no) and agent forwarding (AllowAgentForwarding no) unless you need them.
  • Set a custom SSH banner – A legal warning banner can deter some attackers, though it is not a technical control.
  • Use a non-standard cipher and MAC list – Modern OpenSSH defaults are already strong, but you can disable weak algorithms like hmac-sha1 or aes128-cbc.

Monitoring and Auditing SSH

Regularly check authentication logs (e.g., /var/log/auth.log on Debian/Ubuntu, /var/log/secure on RHEL/CentOS) for failed attempts. Use tools like auditd to track changes to SSH configuration files. Consider setting up a central log server to collect SSH logs from multiple machines. Also, periodically review your authorized_keys files to remove unused or outdated keys.

Frequently Asked Questions

Is changing the SSH port necessary?

It is not a security control, but it reduces automated scans and log noise. Attackers can still find your SSH on any port with a port scan, so it should be combined with other measures like firewalling and key authentication.

Can I use SSH keys with a passphrase?

Yes, and it is recommended. A passphrase encrypts your private key, so even if someone steals the key file, they cannot use it without the passphrase. You can use ssh-agent to cache the passphrase for convenience.

What is the difference between RSA and Ed25519 keys?

RSA is widely supported but slower and requires larger key sizes. Ed25519 is a modern elliptic-curve algorithm that is faster, more secure, and uses smaller keys. It is supported by OpenSSH 6.5 and later. For maximum compatibility, RSA 4096 is safe, but Ed25519 is preferred on modern systems.

How do I copy my public key to a server?

Use ssh-copy-id user@server on Linux/macOS. On Windows, you can manually append the public key to ~/.ssh/authorized_keys on the server. Ensure the directory and file permissions are correct.

What should I do if I get “Permission denied (publickey)”?

Check that your private key is in the correct location (~/.ssh/id_ed25519 or similar), that you specified the correct user, and that the public key is in the server’s authorized_keys file with correct permissions. Also verify that PubkeyAuthentication yes is set in sshd_config.

Is SSH tunneling safe?

SSH tunneling (port forwarding) is generally safe if used correctly. It encrypts traffic between your client and server, but it can also be used to bypass firewalls. Only enable forwarding if you need it, and use AllowTcpForwarding no on servers that do not require it.

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 →