SSH (Secure Shell) is the primary way administrators and developers access their servers remotely. Yet it's also the most common attack vector for unauthorized access. Every day, millions of automated SSH brute-force attempts probe servers worldwide. Proper SSH hardening can turn your server into a fortress against these attacks.
This guide covers essential SSH security practices—from key-based authentication to advanced hardening techniques—that every server administrator should implement immediately.
SSH attacks are largely automated and indiscriminate. Botnets continuously scan the internet for SSH services, attempting thousands of password combinations per minute. Even servers on private networks get probed when they connect to the outside world.
Common attack vectors include:
Password brute-forcing: Automated tools trying every common password and dictionary word. Average servers receive 100-1000 failed login attempts daily.
Compromised credentials: Leaked SSH keys or passwords from other services. Once an attacker has valid credentials, password policies are irrelevant.
SSH vulnerabilities: Exploitable bugs in OpenSSH software, particularly in older versions that haven't been patched.
Configuration weaknesses: Default configurations that allow root login or password authentication by default.
The good news: Implementing the right SSH hardening measures can block 99% of automated attacks.
1. Use SSH Key Authentication (NOT Passwords)
This is the single most important security measure. Generate an SSH key pair on your client machine and use it exclusively for authentication.
How to set up key-based authentication:
# On your LOCAL machine (NOT the server)
ssh-keygen -t ed25519 -C "your_email@example.com"
# Copy public key to server
ssh-copy-id user@your-server
# Verify connection works with keys only
ssh user@your-server
Why keys over passwords?
2. Disable Password Authentication
Once you've verified key authentication works, completely disable password-based SSH login:
# Edit SSH configuration
sudo nano /etc/ssh/sshd_config
# Set these values:
PasswordAuthentication no
PermitRootLogin no
ChallengeResponseAuthentication no
Warning: Test your key-based access works BEFORE making changes. Always keep a second terminal open during changes so you don't lock yourself out.
3. Change the Default SSH Port
While this doesn't provide true security (it's "security through obscurity"), it dramatically reduces automated attack noise:
# In /etc/ssh/sshd_config:
Port 2222 # Choose any port between 1024-65535
Remember to update your firewall rules and any load balancer configurations before making this change!
4. Implement Fail2Ban for Brute-Force Protection
Fail2Ban monitors SSH logs and automatically blocks IPs after repeated failed login attempts:
# Install Fail2Ban
sudo apt install fail2ban
# Copy and customize the jail configuration
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
# Configure SSH protection
cat << EOF | sudo tee -a /etc/fail2ban/jail.local
[sshd]
enabled = true
maxretry = 3
bantime = 3600
findtime = 600
EOF
This configuration blocks IPs after 3 failed attempts within 10 minutes, with a 1-hour ban period.
5. Use SSH Certificate Authorities
For teams or automated systems, SSH certificates provide better credential management than individual keys:
# On your certificate authority machine
ssh-keygen -s /root/ca.key -I "user@host" /path/to/user.key
Certificates have expiration dates, making them ideal for temporary access or automated systems.
6. Enable SSH Agent Forwarding with Caution
SSH agent forwarding allows you to use your local keys on remote servers. Use it carefully:
# Enable in SSH config (~/.ssh/config)
Host remote-server
ForwardAgent yes
Risk: If a remote server is compromised, attackers can potentially use your local keys on other systems.
7. Implement SSH Logging and Monitoring
Keep detailed logs of all SSH activity:
# In /etc/ssh/sshd_config:
LogLevel VERBOSE
SyslogFacility AUTH
# Monitor failed attempts in real-time
sudo tail -f /var/log/auth.log | grep "Failed password"
Set up alerts for unusual patterns: multiple failed logins, logins from new countries, or attempts outside business hours.
8. Use SSH ProxyJump for Multi-Hop Access
For servers without public access, use bastion hosts as jump points:
# In ~/.ssh/config
Host bastion
HostName bastion.example.com
User admin
Host webserver
HostName 10.0.1.50
User webuser
ProxyJump bastion
This lets you SSH to the bastion and then easily hop to internal servers.
Before considering your SSH setup secure, verify these items:
✅ SSH key-based authentication enabled
✅ Password authentication disabled
✅ Root SSH login disabled
✅ SSH on non-standard port (optional but recommended)
✅ Fail2Ban or similar protection active
✅ SSH logging enabled and monitored
✅ OpenSSH updated to latest version
✅ Unused SSH protocols removed (only Protocol 2)
✅ Access limited to specific users or groups
✅ SSH keys have strong passphrases
✅ Private keys stored securely (never in version control)
Mistake #1: Using weak SSH keys
RSA keys shorter than 2048 bits or ECDSA keys below 256 bits are considered weak. Use ED25519 when possible, or RSA 4096-bit keys.
Mistake #2: Sharing SSH keys
Each person should have their own key pair. Sharing keys makes it impossible to track who made changes and complicates revocation.
Mistake #3: Not rotating keys
SSH keys can be compromised. For highly secure environments, rotate keys every 6-12 months. Revoke old keys immediately after rotation.
Mistake #4: Storing private keys insecurely
Never commit private SSH keys to version control. Use SSH agent to manage keys in memory rather than storing them on disk.
SSH Audit Tool: Scan your SSH configuration for security issues
ssh-audit your-server.com
Hydra: Test your own SSH password strength (ethical testing only!)
hydra -l username -P /path/to/passwords.txt ssh://your-server.com
Fail2Ban: Already covered above - essential for automated protection
SSH Hardening Guide: Use SSH's built-in security hardening documentation
SSH Certificates: For enterprise environments, consider OpenSSH's certificate authority feature for scalable credential management.
After implementing these security measures, you'll notice:
The effort required is minimal, but the security benefits are immediate and substantial.
SSH security isn't optional—it's fundamental to server administration. By implementing key-based authentication, disabling passwords, configuring fail2ban, and following these best practices, you transform your SSH from a security liability into a robust, secure access mechanism.
Take time today to audit your SSH configuration and implement these hardening measures. Your server security will thank you.
Need help hardening your server? Our team specializes in SSH security and server hardening. Contact SwissLayer for professional security consulting and implementation support.