This guide covers troubleshooting common SSH issues, compliance requirements for enterprise environments, and best practices for ongoing maintenance.
Troubleshooting Common Issues
Can’t Connect After Changes
- Check if SSH is running:
sudo systemctl status sshd- Verify firewall rules:
sudo ufw status # or firewall-cmd --list-all- Test configuration:
sudo sshd -t- Check logs:
sudo journalctl -u sshd -n 50Permission Denied (publickey)
This usually means SSH keys aren’t configured correctly:
# Check permissionsls -la ~/.ssh
# Should be:# .ssh directory: 700# authorized_keys: 600# private keys: 600
# Fix permissionschmod 700 ~/.sshchmod 600 ~/.ssh/authorized_keysToo Many Authentication Failures
If you have multiple keys in your SSH agent:
# Clear agentssh-add -D
# Add only the required keyssh-add ~/.ssh/id_prod_server
# Or specify in connectionssh -o IdentitiesOnly=yes -i ~/.ssh/id_prod_server user@server2FA Code Not Working
- Check time synchronization on server:
timedatectl status- If time is off, sync it:
sudo systemctl restart chrony # or ntpdCompliance and Best Practices
Regular Security Audits
I run these checks monthly:
# Review authorized_keyscat ~/.ssh/authorized_keys
# Check for weak keysfor key in /etc/ssh/ssh_host_*_key.pub; do ssh-keygen -lf $key; done
# Review SSH logs for anomaliessudo grep -i "POSSIBLE BREAK-IN" /var/log/auth.log
# Check for users with empty passwordssudo awk -F: '($2 == "") {print $1}' /etc/shadowDocumentation Requirements
For enterprise environments, document:
- SSH configuration changes
- List of authorized users and their keys
- Justification for any non-standard settings
- Incident response procedures
- Key rotation schedule
Key Rotation Policy
I rotate SSH keys annually:
- Generate new key pair
- Deploy new public key to all servers
- Test new key works on all systems
- Remove old public key
- Update documentation
Conclusion
SSH hardening isn’t optional for production servers. The techniques in this guide have kept my infrastructure secure across multiple companies and thousands of servers. I’ve prevented countless intrusion attempts simply by following these practices.
The most important takeaways:
- Never rely on passwords alone
- Change default configurations
- Monitor everything
- Test before you deploy
- Always have a backup access method
Remember that security is a process, not a destination. Stay updated on new vulnerabilities, regularly audit your configurations, and never get complacent.
If you implement even half of these recommendations, you’ll be ahead of 90% of servers on the internet. Start with key-based authentication and work your way through the other sections as time allows.
Have questions about implementing these changes? Found a configuration that works better in your environment? Drop a comment below – I’d love to hear about your experiences with SSH hardening.