Hardening: SSH Keys Only, ufw, fail2ban
Logging in only with keys, no root and no passwords; a firewall that blocks everything except SSH from the admin network and the VPN and the web ports; and fail2ban as a second line of defence – plus a quick check that the automatic updates and zram from chapter 1 really work.
Back to the network map and all chapters
| What You Need |
|---|
| The server from chapter 1, reachable over SSH with your user |
| A computer with an SSH client – Windows 10/11, Linux and macOS all have one built in |
The address ranges of your admin network and of the VPN (here 192.168.20.0/24 and 192.168.70.0/24; the networks themselves come in chapter 6 and chapter 8) |
| Just in case: a monitor and keyboard nearby the first time you change SSH or the firewall |
After chapters 1 and 2 the server runs, is encrypted and can be unlocked from another computer. Now it gets its locks. Three layers, each covering what the others miss:
- SSH accepts only keys – no passwords, no root.
- The firewall (ufw) lets nothing in except SSH from the admin network and the VPN, and the two web ports.
- fail2ban watches the logs and blocks addresses that keep failing to log in.
Automatic security updates and zram were already set up in chapter 1 (1.12 and 1.13); at the end of this chapter there is only a quick check that they really work. As before, every step is Command – What it does – Check, and the names are examples.
Example names used in this chapter:
| What | Example |
|---|---|
| Server | homeserver, 192.168.40.20 |
| User | username |
| Admin network | 192.168.20.0/24 |
| VPN | 192.168.70.0/24 |
3.1 Log In with a Key
This has to work before passwords are switched off in step 3.2 – otherwise you lock yourself out. If you followed chapter 2, you already have a key; this step shows how to make one and checks it.
Command (on your computer, Windows or Linux):
ssh-keygen -t ed25519
What it does: creates a key pair: a private key (id_ed25519) that never leaves your computer, and a public key (id_ed25519.pub) that goes to the server. A passphrase on the key is a good idea – if the laptop is stolen, the key alone is useless.
Command (copy the public key to the server – Linux and macOS):
ssh-copy-id username@192.168.40.20
Windows has no ssh-copy-id; in PowerShell the same is:
type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh username@192.168.40.20 "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
What it does: appends your public key to ~/.ssh/authorized_keys on the server. You type the user's password one last time.
A short name saves typing. In ~/.ssh/config on your computer (on Windows C:\Users\<name>\.ssh\config):
Host homeserver
HostName 192.168.40.20
User username
IdentityFile ~/.ssh/id_ed25519
Check:
ssh homeserver
logs you in without asking for the server password (it may ask for the key's passphrase). Do this from every computer you'll use before going on.
3.2 SSH: No Passwords, No Root
Command:
sudo nano /etc/ssh/sshd_config.d/10-hardening.conf
with this content:
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
PubkeyAuthentication yes
MaxAuthTries 3
AllowUsers username
X11Forwarding no
What it does:
| Line | Meaning |
|---|---|
PasswordAuthentication no, KbdInteractiveAuthentication no | no password logins of any kind – a bot can try millions of passwords, it can't guess a key |
PermitRootLogin no | root can't log in over SSH at all; you log in as yourself and use sudo |
MaxAuthTries 3 | at most three attempts per connection |
AllowUsers username | only the users listed may log in, even if other accounts exist (later chapters add service users here when they need SSH) |
X11Forwarding no | no graphical programs over SSH – a server without a desktop doesn't need it, and Debian's default is yes |
Why a separate file and not /etc/ssh/sshd_config itself? Debian's sshd_config reads the files in sshd_config.d/ right at its start, and for each setting the first value read wins. So your file overrides Debian's defaults further down, and an update of the package never touches it.
Command:
sudo sshd -t
sudo systemctl reload ssh
What it does: sshd -t checks the configuration (no output means no errors), then the SSH service reloads it. Keep your current session open and test in a new window – an open session survives a reload, so if something is wrong you can still fix it.
Check:
sudo sshd -T | grep -Ei '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin|pubkeyauthentication|maxauthtries|allowusers|x11forwarding)'
shows the values that are really in effect:
maxauthtries 3
permitrootlogin no
pubkeyauthentication yes
passwordauthentication no
kbdinteractiveauthentication no
x11forwarding no
allowusers username
And from your computer, try to log in with a password on purpose:
ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password homeserver
must end with Permission denied (publickey). – the server doesn't even offer a password prompt.
3.3 The Firewall: ufw
ufw ("uncomplicated firewall") is a simple front end for the Linux firewall (nftables). The rule is simple too: block everything coming in, then allow only what is needed. For now that is SSH from the admin network and the VPN, and the web ports 80 and 443 for the websites later in the series.
Command:
sudo apt install -y ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow from 192.168.20.0/24 to any port 22 proto tcp comment 'SSH admin network'
sudo ufw allow from 192.168.70.0/24 to any port 22 proto tcp comment 'SSH VPN'
sudo ufw allow 80,443/tcp comment 'web'
What it does: sets the defaults (nothing in, everything out) and adds three rules. The firewall is not active yet.
Important: add the SSH rules before the next command. Turning on a firewall without them over SSH cuts off your own connection, and then only the monitor and keyboard help.
Command:
sudo ufw enable
What it does: switches the firewall on, now and at every boot. It warns that existing SSH connections may be disrupted – answer y.
Check:
sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), deny (routed)
To Action From
-- ------ ----
22/tcp ALLOW IN 192.168.20.0/24 # SSH admin network
22/tcp ALLOW IN 192.168.70.0/24 # SSH VPN
80,443/tcp ALLOW IN Anywhere # web
80,443/tcp (v6) ALLOW IN Anywhere (v6) # web
The (v6) line appears by itself – ufw adds IPv6 rules for rules without a source address. And the real test: from a device in another network (a phone on the guest Wi-Fi, for example), ssh to the server must fail with a timeout, while it still works from the admin network.
To remove a rule later: sudo ufw status numbered shows the numbers, sudo ufw delete <number> removes one. As the server gets more services, more rules join these – each in the chapter of its service (network drives, backups of laptops and so on).
A trap to remember: Docker writes its own firewall rules and goes around ufw. A container port published as 8080:80 is open to everyone, whatever ufw says. That's why every container in this series is bound to 127.0.0.1 – more in chapter 13.
3.4 fail2ban
fail2ban reads the logs, and when one address fails to log in too often, it adds a firewall rule that blocks it for a while. With SSH keys only and SSH not reachable from the Internet it has little to do for SSH – it's the second line of defence. It becomes really important for the public services later (mail, web applications), which get their own fail2ban rules ("jails").
Command:
sudo apt install -y fail2ban
What it does: installs fail2ban with Debian's defaults, which already protect SSH. Look at them:
cat /etc/fail2ban/jail.d/defaults-debian.conf
[DEFAULT]
banaction = nftables
banaction_allports = nftables[type=allports]
[sshd]
backend = systemd
journalmatch = _SYSTEMD_UNIT=ssh.service + _COMM=sshd
enabled = true
Two things are Debian 13 specific: fail2ban reads the systemd journal (Debian no longer writes /var/log/auth.log by default), and it blocks with nftables. The limits come from fail2ban's main file jail.conf: an address that fails 5 times in 10 minutes is blocked for 10 minutes.
Never edit jail.conf – an update overwrites it. Your own settings and jails go into separate files in /etc/fail2ban/jail.d/, one per service; later chapters add them there.
Check:
sudo fail2ban-client status sshd
Status for the jail: sshd
|- Filter
| |- Currently failed: 0
| |- Total failed: 0
| `- Journal matches: _SYSTEMD_UNIT=ssh.service + _COMM=sshd
`- Actions
|- Currently banned: 0
|- Total banned: 0
`- Banned IP list:
Journal matches shows that it reads the right log. Zeros are normal here – nobody from the Internet can even reach port 22.
To see that blocking really works, ban an address from the documentation range, which no real device uses, then remove it:
sudo fail2ban-client set sshd banip 192.0.2.10
sudo nft list ruleset | grep 192.0.2.10
sudo fail2ban-client set sshd unbanip 192.0.2.10
The middle command must show the address in an nftables rule. Never test with your own address – you'd be the one locked out.
3.5 Check the Updates and zram from Chapter 1
Command:
systemctl list-timers 'apt-daily*'
Check: two timers, apt-daily.timer (downloads) and apt-daily-upgrade.timer (installs), each with a next run time.
Command:
sudo tail /var/log/unattended-upgrades/unattended-upgrades.log
Check: recent dates and lines like No packages found that can be upgraded unattended or a list of installed packages.
Command:
swapon --show
Check: /dev/zram0 with a higher priority than the swap in LVM.
One thing the automatic updates don't do is restart the server. After a kernel update the file /var/run/reboot-required appears; then reboot when it suits you and unlock the disk as in chapter 2.
Summary
| What | State |
|---|---|
| SSH | keys only, no root, no passwords, only listed users, 3 attempts, no X11 |
| Firewall | ufw: nothing in by default; 22 only from the admin network and the VPN; 80/443 open |
| fail2ban | SSH jail from Debian (journal, nftables): 5 failures in 10 min → 10 min block |
| Updates, zram | from chapter 1, checked |
Next: Chapter 4 – Data disk: RAID1 (mdadm) and LUKS with a key file. The bigger picture of who should reach what is in chapter 7 (the firewall on the router), and the general ideas behind all of this in Security Considerations.