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.
- 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.
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.
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.
- 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.
- 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.
- 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.
- 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.
- 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.
- "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."