In part 1 I put my home server in its own isolated network, behind a Raspberry Pi running OpenWrt as a firewall. The server can reach the Internet, but it can't reach my home network or even manage the Pi itself.
That protects my home from the server. But what protects the server?
If the Pi is ever misconfigured, replaced, or someone plugs a device into the pro network, the server shouldn't be wide open just because it trusted the wall around it. In security this is called defence in depth: several independent layers, so that when one fails the others still hold.
The analogy I've been using: the Pi is the castle wall. Now the house needs its own lock.
The series
- Network segmentation with a Raspberry Pi and OpenWrt
- Hardening the server (this post)
- Publishing websites without Cloudflare
- Pentesting: attacking my own setup and publishing the results
What's on the server
An Ubuntu 24.04 LTS machine running:
- Two Odoo instances in Docker (client demos), each with its own PostgreSQL container.
- A native Odoo 19 instance as a systemd service, with a native PostgreSQL. It holds personal data, which makes it the most sensitive thing on the machine.
- Tailscale, to show demos from my laptop.
- It doubles as a development machine (VS Code Remote).
So it's a real-world, slightly messy server, not a clean lab VM. That's exactly why it's worth hardening.
Further down the road I plan to manage the whole infrastructure with Kubernetes. For now it's Docker Compose and systemd, but that doesn't change this post: whatever runs the containers, the machine underneath still has to be hardened.
Step 1: Inventory, know what's listening
You can't protect what you don't know about. The first step is a list of every service waiting for connections, and on which address:
sudo ss -tulpn
(-t TCP, -u UDP, -l only listening sockets, -p show the process, -n numeric ports.)
How to read the "Local Address" column:
| Address | Meaning | Risk |
|---|---|---|
127.0.0.1:X or [::1]:X |
Only reachable from the server itself | ✅ Safe |
127.0.0.53:53 |
Ubuntu's local DNS resolver | ✅ Normal |
100.x.x.x:X |
Only reachable through Tailscale | ⚠️ Reachable from your tailnet |
0.0.0.0:X, [::]:X, *:X |
Reachable from any network | ⚠️ Exposed, review it |
And for Docker specifically:
sudo docker ps --format 'table {{.Names}}\t{{.Ports}}'
What I found:
- ✅ Both Odoo containers published on
127.0.0.1only (127.0.0.1:8169->8069/tcp). Their databases weren't published at all. - ✅ The native Odoo and its PostgreSQL also bound to
127.0.0.1. - ⚠️
sshdon0.0.0.0:22: needed, but should be restricted. - ⚠️
avahi-daemonon0.0.0.0:5353: not needed (more below). cupsd(printing) on localhost: harmless, but useless on a server.- A leftover container named
...-odoo-run-..., created by adocker compose runand never removed. - ❌ UFW inactive. No host firewall at all.
- Nothing on ports 80/443 yet: the reverse proxy comes in part 3.
Step 2: Why Docker must listen on 127.0.0.1 (and why UFW won't save you)
This is the most important concept in this post, and the one that surprises most people.
When Docker publishes a port (-p 8069:8069 or ports: in Compose), it creates its own iptables rules to route traffic into the container. Those rules are evaluated before UFW's rules. The result: if you publish a container as 8069:8069 (which means 0.0.0.0:8069), it is reachable from the network even if UFW says "deny". UFW never sees that traffic.
The fix is simple and costs nothing: always bind published ports to localhost, and let a reverse proxy on the host be the only thing facing the network.
# docker-compose.yml
services:
odoo:
ports:
- "127.0.0.1:8169:8069" # ✅ only reachable from the host
# - "8169:8069" # ❌ reachable from the network, bypassing UFW
Databases shouldn't be published at all; the app container reaches them over Docker's internal network.
Step 3: Updates and a reboot
sudo apt update
sudo apt upgrade
sudo reboot
After the reboot I confirmed that everything starts on its own: all containers Up and healthy, and the native Odoo and PostgreSQL services active (systemctl is-active <service>). This check matters more than it seems: later I'll enable automatic reboots, and that's only safe if every service comes back without help. The leftover docker compose run container was no longer running, because those containers have no restart policy (it still shows up in docker ps -a until you remove it with docker rm).
Step 4: Remove what you don't use
Every running service is code that can have vulnerabilities and has to be kept updated. What isn't running can't be attacked.
| Service | What it does | Why it goes |
|---|---|---|
avahi-daemon |
Announces the machine on the local network ("I'm here, I offer these services") | A server doesn't need to introduce itself; it only gives information to whoever is listening. |
cups |
Printing | The server will never print anything. |
sudo systemctl disable --now avahi-daemon.service avahi-daemon.socket
sudo systemctl disable --now cups.service cups.socket cups-browsed.service
disable stops it from starting at boot; --now also stops it immediately. Disabling the .socket units matters: with socket activation, systemd can wake a stopped service as soon as someone connects to its port. CUPS also has a .path unit, which wakes it when a file appears in its spool directory.
Check that they're really gone:
systemctl is-active avahi-daemon.socket cups.socket cups.path # inactive x3
sudo ss -tulpn | grep -E 'avahi|cups' # no output
Step 5: SSH with keys only
Passwords can be guessed; bots on the Internet do it all day, millions of attempts. An ed25519 key, in practice, can't be.
One key per machine, with a passphrase
ssh-keygen -t ed25519 -C "admin-server" -f ~/.ssh/id_ed25519_server
ssh-copy-id -i ~/.ssh/id_ed25519_server.pub server
Two decisions here:
- One key per machine. The Pi has its own key and the server has another. If one is ever compromised, it doesn't open every door.
- A passphrase on every key. My first keys had none, which meant anyone who copied the key file (malware, a stolen laptop, a lost backup) had instant access. With a passphrase the file alone is useless, and Ubuntu's keyring asks for it only once per session. You can add one to an existing key without regenerating it:
ssh-keygen -p -f ~/.ssh/id_ed25519_server
Since the server lives behind the Pi, I reach it with ProxyJump (explained in part 1). For the server, every SSH connection comes from the Pi's address, which is useful for the firewall later.
Disabling password login, and the cloud-init trap
Ubuntu 24.04 reads /etc/ssh/sshd_config plus every file in /etc/ssh/sshd_config.d/. The catch: when an option appears more than once, the first value read wins, and files are read in alphabetical order. Many installations ship a 50-cloud-init.conf that sets PasswordAuthentication yes. If you create your own file with a name that sorts after it, your setting is silently ignored.
So the file starts with 00-:
sudo nano /etc/ssh/sshd_config.d/00-hardening.conf
# SSH hardening - key-only access
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
PubkeyAuthentication yes
KbdInteractiveAuthentication closes a second path that can also ask for a password. PermitRootLogin no means you log in as your user and use sudo.
Never restart SSH blindly. First check the syntax, then check the effective values:
sudo sshd -t # no output = OK
sudo sshd -T | grep -E 'passwordauthentication|kbdinteractive|permitrootlogin'
sudo systemctl restart ssh
Then test from a second terminal, keeping the current session open in case you need to undo:
ssh server # must work (key)
ssh -o PubkeyAuthentication=no server # must fail: Permission denied (publickey)
The second command simulates an attacker without your key. Permission denied (publickey) means the server doesn't even offer password authentication anymore.
Step 6: A host firewall with UFW
The Pi already filters everything, so why a second firewall? Because the server should be safe on its own. If the Pi is replaced, misconfigured, or something else gets plugged into the pro network, the server keeps protecting itself.
Rules: deny everything incoming, allow everything outgoing, and open only four things:
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow from 10.10.10.1 to any port 22 proto tcp comment 'SSH via Pi'
sudo ufw allow in on tailscale0 comment 'Tailscale'
sudo ufw allow 80/tcp comment 'HTTP'
sudo ufw allow 443/tcp comment 'HTTPS'
sudo ufw show added # review BEFORE enabling
| Rule | Why |
|---|---|
SSH only from 10.10.10.1 |
My ProxyJump goes through the Pi, so for the server every legitimate SSH connection comes from the Pi. Nobody else can even try. |
Everything on tailscale0 |
My private tailnet (server and laptop), for demos and as an emergency access path. |
| 80 and 443 | For the reverse proxy and the public websites (part 3). |
sudo ufw enable
sudo ufw status verbose
ufw enable warns that it may disrupt existing SSH connections. Mine survived, because it came from the allowed address, but keep a second session open until you've tested a new login. If it fails, sudo ufw disable from the open session undoes everything.
Note the line deny (routed) in the status output: the server doesn't forward traffic between networks. It's not a router, and it shouldn't act like one.
Step 7: Automatic security patches
The most common way into a server isn't a clever zero-day; it's a known vulnerability that was never patched. Ubuntu's unattended-upgrades is usually installed and enabled by default, and it's more conservative than people think:
| Installs automatically | Never touches |
|---|---|
Security patches from Ubuntu's official security channel (noble-security) |
Release upgrades (24.04 → 26.04) |
| Regular non-security updates | |
| Docker images, your app code | |
| Third-party repositories (unless you add them) |
I checked that it was enabled (/etc/apt/apt.conf.d/20auto-upgrades should contain two lines set to "1") and added my own config file instead of editing the default one, so package updates never overwrite it:
sudo nano /etc/apt/apt.conf.d/52unattended-upgrades-local
// Reboot automatically only if a patch requires it
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "03:00";
Unattended-Upgrade::Automatic-Reboot-WithUsers "true";
Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";
About the reboot: some patches, especially kernel ones, only take full effect after a reboot. I chose an automatic reboot at 03:00, and only when a patch requires it, not every night. I could choose that safely because I had already verified that every service comes back on its own after a reboot. WithUsers "true" is already the default, but I set it explicitly: if it were "false", an editor left connected overnight would block the reboot forever.
A dry run confirms the configuration is valid without installing anything:
sudo unattended-upgrade --dry-run --debug 2>&1 | tail -15
In my case it listed pending updates from noble-updates and skipped them, because they weren't security updates. That's exactly the conservative behaviour you want.
Before and after
| Before | After | |
|---|---|---|
| SSH | Password, from any network | Key + passphrase, only from the Pi |
| Host firewall | None | UFW: SSH (Pi), Tailscale, 80/443 |
| Extra services | avahi, cups | Disabled |
| Docker exposure | Already bound to 127.0.0.1 ✅ | Verified and documented |
| Patches | Manual, pending | Automatic security patches, 03:00 reboot if needed |
What's still on my list
To be honest about what isn't done yet:
- Tailscale and segmentation. Tailscale creates an encrypted tunnel that doesn't pass through the Pi's firewall. If my home PC were on the same tailnet as the server, a compromised server could reach my PC through it and skip all the isolation from part 1. For now only the server and my laptop are on it, and I'll lock it down with Tailscale ACLs.
- Least privilege per service. Each exposed app should run as its own unprivileged user, inside a systemd sandbox. I did this when I rebuilt the sensitive Odoo instance, and I'll show it in part 3.
- Active defence (CrowdSec or Fail2Ban) once the websites are public.
- Off-site backups, especially for the most sensitive database.
- Removing the desktop packages this server still has, which are just more code to patch.
Lessons learned
- Inventory first.
ss -tulpnanddocker psbefore touching anything. - Docker bypasses UFW. Bind published ports to
127.0.0.1, always. - Disable what you don't use, including its
.socketand.pathunits. - Config precedence matters.
00-beats50-cloud-init.confinsshd_config.d. - Keep a session open while changing SSH or the firewall, and test from a second one.
- Defence in depth. The network firewall and the host firewall each have to stand on their own.
What's next
In part 3 the websites finally go public: Caddy as a reverse proxy with automatic HTTPS, a home-made dynamic DNS updater, CAA records, and how to publish a sensitive app with extra layers in front of it, all without Cloudflare's proxy, which in Spain can get your site blocked during football matches.