Practical SSH hardening for production Linux servers — key-based auth, sshd_config, 2FA, host-based auth, fail2ban, and log monitoring.
The default SSH configuration on most distributions is functional but not production-safe. After managing Linux infrastructure for several years — and finding over 50,000 failed login attempts in a single day’s auth log early in my career — I apply the same hardening steps to every server I manage.
Warning
Never lock yourself out. Always test each change in a separate SSH session before closing your original connection.
Guide Structure
Key-Based Authentication — replace password auth with cryptographic keys
Configure automated server-to-server authentication for backup systems, monitoring, and CI/CD pipelines
Host-based authentication lets one server authenticate to another based on the client machine’s host key rather than user keys. I use it for automated backup systems, Ansible/Puppet, monitoring that executes remote commands, database replication, and CI/CD pipelines.
Warning
Only use this in controlled environments where you fully trust the client machines. It’s a complement to user key auth for specific automation use cases, not a replacement.
Prerequisites: Root access on both machines, DNS or /etc/hosts entries for hostname resolution.
Combining with user key auth is the safest approach:
Terminal window
AuthenticationMethodspublickey,hostbased
Review /etc/ssh/shosts.equiv monthly. Ensure LogLevel VERBOSE is set so host-based authentications are logged. Restrict SSH access by firewall to trusted client IPs only.
for key in/etc/ssh/ssh_host_*_key.pub; dossh-keygen-lf $key; done
# Anomalies in auth logs
sudogrep-i"POSSIBLE BREAK-IN"/var/log/auth.log
# Users with empty passwords
sudoawk-F:'($2 == "") {print $1}'/etc/shadow
Key Rotation
I rotate SSH keys annually: generate new pair → deploy to all servers → test → remove old public key → update documentation.
Enterprise Documentation
Track: SSH configuration changes, authorized users and their keys, justification for any non-standard settings, incident response procedures, key rotation schedule.