SECURITY ENGINEERING · PART 1 · LINUX AND WINDOWS, FROM THE INSIDE · LINUX · 12 OF 229

SSH Hardening: Keys, Config, Brute-Force Defense

SSH is the main door into most Linux servers, so attackers hammer it. How to make it hard to break: keys instead of passwords, disabling root login, locking down sshd_config, limiting who can connect, defending against brute force with fail2ban and rate limits, bastions and port knocking, and watching the auth log for the attacks that will come.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 9
  9. 10
  10. 11
  11. 12
  12. 13
  13. 14
  14. 15
  15. 16
  16. 17
  17. 18
  18. 19
  19. 20
  20. 21
  21. 22
  22. 23
  23. 24
  24. 25
  25. 26
  26. 27
  27. 28
  28. 29
  29. 30
  30. 31
  31. 32
  32. 33
  33. 34
  34. 35
  35. 36
  36. 37
  37. 38
  38. 39
  39. 40
  40. 41
  41. 42
  42. 43
  43. 44
  44. 45
  45. 46
  46. 47
  47. 48
  48. 49
  49. 50
  50. 51
  51. 52
  52. 53
  53. 54
  54. 55
  55. 56
  56. 57
  57. 58
  58. 59
  59. 60
  60. 61
  61. 62
  62. 63
  63. 64
  64. 106
  65. 107
  66. 108
  67. 109
  68. 110
  69. 111
  70. 112
  71. 113
  72. 114
  73. 115
  74. 116
  75. 117
  76. 118
  77. 119
  78. 120
  79. 121
  80. 122
  81. 123
  82. 124
  83. 125
  84. 126
  85. 127
  86. 128
  87. 129
  88. 130
  89. 131
  90. 132
  91. 133
  92. 134
  93. 135
  94. 136
  95. 137
  96. 138
  97. 139
  98. 140
  99. 141
  100. 142
  101. 143
  102. 144
1 / 11

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Keys not passwords
  • no root login
  • AllowUsers
  • fail2ban
  • bastion or VPN
  • protect and rotate keys
  • watch auth.log

Notes

The threat

The threat is simple and relentless. Bots scan the entire internet for open SSH ports and try to log in, using common usernames like root and admin, common passwords, and passwords leaked in past breaches. Your auth log fills with failed password lines. It only takes one account with a weak or reused password for them to get in. So the whole strategy is to remove passwords from the equation and make the attempts cost nothing to you.

What hits port 22
  • bots scan the whole internet
  • they try root, admin, common users with common passwords
  • and reused passwords from past breaches
  • auth.log fills with "Failed password for ..."
  • one weak password = game over

Keys, not passwords

The single biggest win is public-key authentication instead of passwords. You generate a key pair, put the public half on the server and keep the private half on your own machine, protected and ideally passphrase-locked. The server challenges you to prove you hold the private key, which never travels. Then you turn passwords off entirely with PasswordAuthentication no. Now brute force cannot work, because there is no password to guess. This one change defeats almost all of the attacks from the last slide.

Public-key auth
ssh-keygen -t ed25519
copy the *public* key to the
  server (~/.ssh/authorized_keys)
the private key never leaves
  your machine; protect it 600
in sshd_config:
  PasswordAuthentication no
  PubkeyAuthentication yes

Lock down sshd

Next, lock down the SSH daemon's config. Disable direct root login, so an attacker must first compromise a normal account and then escalate, which gives you two chances to catch them. Keep password and keyboard-interactive auth off. Restrict who may connect with AllowUsers or AllowGroups, so only named accounts can even try. Lower MaxAuthTries and the login grace time to cut off slow attacks, and turn off forwarding features you do not use. After any change, test in a second session before you log out.

Key sshd_config lines
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
AllowUsers deploy admin
  (or AllowGroups ssh-users)
MaxAuthTries 3
LoginGraceTime 20
X11Forwarding no

Brute-force defense

Even with passwords off, you do not want endless noise and connection load. fail2ban watches the auth log and temporarily bans an address after a few failures, which stops the hammering. You can also rate-limit new connections to port twenty two at the firewall. Changing the port only reduces noise, not risk, so do not rely on it. The strongest move is not to expose SSH to the whole internet at all, which the next slide covers.

Slow the bots down
  • fail2ban: ban an IP after N failures for a while
  • firewall rate-limit new connections to port 22
  • or: do not expose 22 at all move to key-only + a bastion
  • crowdsec is a modern alternative to fail2ban

Reduce exposure

The best hardening is to shrink exposure. Put servers behind a single hardened bastion or jump host, so only that one box accepts SSH and everything else is reachable only through it. Or require a VPN or zero-trust access, so port twenty two is never open to the public internet. In the cloud, lock the security group to your own address ranges. Advanced teams issue short-lived SSH certificates instead of long-lived keys, so access expires automatically. Each of these removes most of the internet from the picture.

Shrink the attack surface
  • bastion / jump host: SSH only to one hardened box, then inward
  • or a VPN / zero-trust access, so 22 is never public
  • cloud: lock the security group to your IP ranges
  • short-lived certs instead of long-lived keys (advanced)

Protect the keys

With passwords gone, the private key is the credential, so protect it. Keep it at mode six zero zero, passphrase-protected, loaded into an agent so you type the passphrase once. Use one key per person and device, never a shared key, so you can revoke one without disrupting everyone. Audit the authorized keys files and remove keys when people leave, which teams forget constantly. And never commit a private key to git, a leak Part nine catches with secret scanning.

Key hygiene
  • private key at 600, passphrase protected, in an agent
  • one key per person/device, not shared
  • rotate and remove keys when people leave (audit authorized_keys)
  • no keys committed to git (secret scanning, Part 9)

Watch the door

Hardening stops entry; monitoring tells you about the attempts and catches the one that slips through. Watch the auth log for accepted versus failed logins and where they come from. Alert on a successful login from a new address or country, and on a new key appearing in an authorized keys file, which could be an attacker adding their own access. A sudden spike in failures against one account means you are being targeted specifically. Ship all of this off the box, as the last episode covered.

Monitor SSH
  • auth.log: accepted vs failed, from where
  • alert on a successful login from a new IP or country
  • alert on a new key added to authorized_keys
  • spikes in failures = targeted
  • ship it off-box (last episode)

Common mistakes

Mistakes that keep SSH an easy door. Leaving password login on, so brute force still works. Allowing direct root login. Exposing SSH to the entire internet instead of a bastion or VPN. Sharing one key across a team and never rotating it. Private keys with no passphrase, so a stolen laptop is instant access. Relying on a non-standard port as if it were security. And running no fail2ban and no monitoring, so attempts are neither slowed nor seen.

Avoid these
  • password login left on
  • root login allowed
  • SSH open to the whole internet
  • shared keys, never rotated
  • keys with no passphrase
  • relying on a non-standard port
  • no fail2ban, no monitoring

Say it like this

In the interview: I harden SSH with key-only authentication, passwords and direct root login disabled, AllowUsers or AllowGroups to name exactly who can connect, and a low MaxAuthTries. I add fail2ban or firewall rate limiting for brute force, and ideally put servers behind a bastion or VPN so port twenty two is never public. I protect private keys with passphrases, rotate and remove them, and alert on successful logins from new locations and on new keys added to authorized_keys.

In the interview
  • "Key-only auth, passwords and root login off, AllowUsers to name who can connect, and low
  • MaxAuthTries. fail2ban for brute force, and ideally a bastion or VPN so 22 is not public. Protect and rotate
  • keys, and alert on new logins
  • and new authorized_keys."

Interview questions

Q1Why is public-key authentication better than passwords for SSH?

The private key never leaves your machine and is far too long to brute-force, so password-guessing attacks fail. With PasswordAuthentication disabled there is no password to guess, which defeats almost all automated SSH attacks.

Q2What are the most important sshd_config hardening settings?

PermitRootLogin no, PasswordAuthentication no, KbdInteractiveAuthentication no, AllowUsers or AllowGroups to restrict who can connect, a low MaxAuthTries and LoginGraceTime, and disabling unused forwarding. Always test in a second session before logging out.

Q3Why disable direct root login over SSH?

It forces an attacker to compromise a named account first and then escalate, giving two chances to detect them, and it removes the universal "root" username from brute-force target lists. Admins log in as themselves and use sudo.

Q4How do you defend SSH against brute force?

Use key-only auth so guessing cannot work, add fail2ban (or CrowdSec) to ban IPs after repeated failures, rate-limit new connections at the firewall, and ideally avoid exposing port 22 to the internet at all via a bastion or VPN.

Q5Does changing the SSH port improve security?

Only slightly: it reduces automated noise but is not a real control, since a port scan finds the new port quickly. Treat it as tidiness, not security, and rely on key-only auth, access restrictions and reduced exposure instead.

Q6What is a bastion (jump) host?

A single hardened server that is the only machine accepting SSH from outside; all other servers are reachable only through it. It concentrates monitoring and hardening on one box and keeps the rest off the public internet.

Q7How should SSH private keys be managed?

One key per person and device (never shared), stored at 600 with a passphrase and loaded into an agent, never committed to git, rotated periodically, and removed from authorized_keys when someone leaves or a device is lost.

Q8What SSH activity should you monitor and alert on?

Accepted versus failed logins and their source, successful logins from new IPs or countries, new keys added to authorized_keys, and spikes of failures against a single account. Ship the auth log off-box so it cannot be erased.