SSH Hardening: Securing a Server's sshd_config on the Internet
Harden any internet-facing SSH server by disabling password logins, restricting users, adding fail2ban, and applying a proven best-practices checklist.
A server exposed to the internet will be hammered by relentless login attempts from bots. Securing SSH is not optional but mandatory, especially once public keys are in place. This article lays out hardening steps from the highest impact to the more advanced ones.
Basic Hardening Principles
SSH hardening aims to shrink the attack surface: fewer ways to log in, fewer users allowed in, fewer weak algorithms, and detection for suspicious attempts. It all starts with a single file: sshd_config.
Step 1: Disable Password Login
Once public keys are installed and verified on all your devices, turn off passwords:
PasswordAuthentication no
Make sure you can really log in with a key before reloading, and keep direct console access (for example from your VPS/Proxmox dashboard) as an emergency door. Disabling passwords removes the main brute-force attack: bots guessing passwords.
Step 2: Restrict Users and Root
Prohibit direct root login and restrict which users may use SSH:
PermitRootLogin no
AllowUsers alif deployserver admin
AllowUsers lists the permitted users. All other accounts are rejected from the very start of authentication. For a mandatory pattern, use AllowGroups ssh-users.
Do not allow root with PermitRootLogin yes unless you have a strong reason; use sudo for administrative tasks.
Step 3: Restrict Protocols and Tools
Some older devices support an old SSH version. Make sure only the new SSH version is active:
Protocol 2
Also restrict forwarding capabilities you do not use, so a session cannot be abused as an entry point:
AllowAgentForwarding no
AllowTcpForwarding no
Re-enable them only when needed (for example for port tunnels), because both can become a path into the internal network.
Step 4: Strong Keys Only
Restrict host key and client key types to modern algorithms:
HostKeyAlgorithms ssh-ed25519
KexAlgorithms curve25519-sha256
Ciphers [email protected],[email protected]
Do not copy cipher and KEX lists from random articles without understanding them. Current versions of OpenSSH already choose secure algorithms by default; manual restrictions are most useful for banning leftover legacy algorithms.
Step 5: Anticipate Brute Force with fail2ban
fail2ban monitors logs and blocks IPs that repeatedly fail to log in:
sudo apt install fail2ban
Create /etc/fail2ban/jail.local:
[sshd]
enabled = true
port = ssh
maxretry = 5
bantime = 3600
findtime = 600
After 5 failed attempts within 10 minutes, the IP is blocked for 1 hour. If you log in repeatedly from a changing IP (for example a dynamic home connection), adjust maxretry so you do not lock yourself out.
Step 6: Move the Port? Think Twice
Moving Port from 22 to another number (for example 2222) does dampen most bot attacks, because bots scan port 22. But this is not a safeguard: an attacker targeting you will find the new port quickly. Treat a port change as noise reduction, not as your main defense.
Step 7: Secure Notes on the Server Side
Even so, no attack is more dangerous than a leaked key. Note the possible risks on the server side:
- Rotate keys periodically (create a new pair, delete the old one).
- Regularly audit
authorized_keysand active sessions. - Monitor
journalctl -u sshfor suspicious login patterns. - Enable MFA for admins where possible (for example the pam_google_authenticator module or yakopfh).
Closing
SSH hardening is the minimum standard for any server on the internet: passwords off, root banned, users restricted, failures monitored. With these steps, bot attack noise drops sharply and the remaining problem shifts to proper key management.
Next in the curriculum: the secure connection is ready to be used for tunneling and port forwarding, one of SSH’s most useful capabilities.
References
- The
sshd_config(5)man page, OpenSSH. - OpenSSH FAQ, openssh.com/faq.html (the security recommendations and configuration sections).
- EOF Tools, SSH best practices, sshconfig.dev (a community-verified collection of secure practices).
- fail2ban documentation, fail2ban.org/wiki/index.php/Fail2ban.
- SSH, The Secure Shell: The Definitive Guide, O’Reilly Media (the security and hardening chapters).
Related Articles
Hardening Homelab Server Security: A Complete Guide
Secure your homelab server with essential hardening steps from SSH key authentication to service isolation and automated updates using this practical checklist.
SecurityLinux Kernel Hardening: How to Make Your Server Truly Secure
A complete guide to Linux kernel hardening for server security, covering sysctl, AppArmor, SELinux, SSH hardening, Fail2Ban, and firewall configuration.
SecuritySSH Public Key Authentication with ssh-keygen
Learn how to replace SSH passwords with an ed25519 key pair, install the public key on a server, use a passphrase and ssh-agent, and avoid common security pitfalls.
Your ideas deserve to be read by thousands of people.
ByteNesia is built for everyone. If you have experience, tutorials, or opinions about technology, write and share them here. Your name will be credited as a contributor.
Write anything: tool reviews, homelab tutorials, security tips, or your tech stories.