Skip to content
Back to Series SSH from Scratch
Security

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
Alif Afriza Author
· · 4 min read
SSH Hardening: Securing a Server's sshd_config on the Internet

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_keys and active sessions.
  • Monitor journalctl -u ssh for 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).
Open for Contributors

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.

Discussion & Comments