519 Words

🛡️ Harden an SSH server

Every server with port 22 open to the internet gets thousands of login attempts a day. These four steps make them harmless.

⚠️ Keep your current SSH session open while you change these settings, and test with a second session. If something is wrong, you can still fix it.


1. Log in with a key first

Make sure you can log in with an SSH key before you turn passwords off:

ssh-copy-id -i ~/.ssh/id_ed25519.pub me@server     # see "Best practice: generate an SSH key pair"
ssh -o PasswordAuthentication=no me@server           # must work without a password

See generate an SSH key pair, or even better, a key on your YubiKey.


2. sshd settings

Don’t edit /etc/ssh/sshd_config itself: put your settings in a separate file in /etc/ssh/sshd_config.d/. Updates then never overwrite them.

sudo nano /etc/ssh/sshd_config.d/10-hardening.conf:

# Keys only
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
AuthenticationMethods publickey

# No root login (log in as yourself and use sudo)
PermitRootLogin no

# Only these users may log in over SSH
AllowUsers benjamin

# Fewer chances, less time
MaxAuthTries 3
LoginGraceTime 30

# Things most servers don't need
X11Forwarding no
AllowAgentForwarding no

💡 Why 10-? sshd uses the first value it finds, and reads the files in alphabetical order. Ubuntu cloud images have a 50-cloud-init.conf that turns passwords back on; 10-hardening.conf comes first and wins.

Check and reload:

sudo sshd -t                                   # no output = no errors
sudo sshd -T | grep -Ei 'passwordauth|permitroot|allowusers'   # the settings that are really used
sudo systemctl reload ssh                      # Debian/Ubuntu
sudo systemctl reload sshd                     # Fedora

Now test a new login in a second terminal.


3. Firewall

Only open what you need.

Fedora (firewalld):

sudo firewall-cmd --list-all                          # what is open now?
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --permanent --remove-service=cockpit   # example: close what you don't use
sudo firewall-cmd --reload

Ubuntu (ufw):

sudo ufw allow OpenSSH        # first allow SSH, or you lock yourself out!
sudo ufw limit OpenSSH        # max 6 new connections per 30 s from one address
sudo ufw enable
sudo ufw status verbose

4. fail2ban: block addresses that keep trying

fail2ban reads the logs and blocks an IP address in the firewall after too many failed logins.

sudo apt install fail2ban     # Debian/Ubuntu
sudo dnf install fail2ban     # Fedora

sudo nano /etc/fail2ban/jail.local:

[DEFAULT]
bantime  = 1h
findtime = 10m
maxretry = 5
# never block yourself (your home or office address)
ignoreip = 127.0.0.1/8 ::1 192.0.2.0/24

[sshd]
enabled = true
backend = systemd
sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd                    # who is blocked?
sudo fail2ban-client set sshd unbanip 203.0.113.7   # unblock an address

5. Keep it up to date

Most break-ins use old, known bugs. Let the server install security updates by itself:

sudo apt install unattended-upgrades && sudo dpkg-reconfigure -plow unattended-upgrades   # Debian/Ubuntu
sudo dnf install dnf-automatic && sudo systemctl enable --now dnf-automatic.timer         # Fedora

(On Fedora, set apply_updates = yes in /etc/dnf/automatic.conf.)


Extra

  • Get an email at every login: login notifications with PAM.
  • Changing the port (Port 2222) only makes the logs quieter; it is not real security, so do the steps above first.