Linux & ServersSecurity

SSH Hardening: Keys Instead of Passwords, plus fail2ban

Three small changes that neutralise most automated SSH attacks, with the classic warning about locking yourself out.

SSH Hardening: Keys Instead of Passwords, plus fail2ban
Table of contents
  1. 1. Keys instead of passwords
  2. 2. Turn off password logins
  3. 3. fail2ban for the remaining noise
  4. A non-standard port?
  5. Final test

SSH is the front door of your server, and most automated attacks try exactly that door. Three small changes neutralise most of that noise.

1. Keys instead of passwords

ssh-keygen -t ed25519 -a 100
ssh-copy-id user@server-ip

Ed25519, with its short and fast keys, is OpenSSH’s own recommended algorithm. -a 100 raises the number of key-derivation rounds, so if the key file ever leaks it takes much longer to crack its passphrase.

2. Turn off password logins

Before making this change, keep an SSH session open; a mistake here can lock you out of your own server:

sudo nano /etc/ssh/sshd_config
#   PasswordAuthentication no
#   PermitRootLogin no
sudo systemctl restart ssh

As long as the first session stays open and key login works, the change is safe. Afterwards, test a second session with your key before you close the first one.

3. fail2ban for the remaining noise

sudo apt install fail2ban
sudo systemctl enable --now fail2ban

The sshd jail is enabled by default: several failed attempts in a row get the IP temporarily banned. The defaults are fine to start with; customise settings such as maxretry and bantime in jail.local, not the main file, so your changes survive every fail2ban update.

A non-standard port?

Moving SSH off port 22 cuts the noise from bots that only scan port 22 and keeps the logs cleaner, but it is worthless against a targeted attacker. Cleaner logs are nice; calling it “security” is an exaggeration. A firewall matters more; if you run Docker, read why UFW doesn’t block Docker’s ports.

Final test

ssh -v user@server-ip

If the verbose output shows authentication succeeding with publickey and no password prompt, you are done.

About the author

Arash Karimian

Network and infrastructure engineer with years of day-to-day work on Linux servers, firewalls and network tuning. Writes about networking, security and server troubleshooting.

All articles by Arash Karimian

Comments

No comments yet. For technical questions, include the details (OS version, the exact error and the command you ran) to get a precise answer.

Leave a comment