Mini Project: Harden a Server & Front an App with TLS
Time to put the course together. You get a fresh Ubuntu 24.04 server and a small HTTP app. The goal: an app that runs as a sandboxed systemd service, reachable only through an HTTPS reverse proxy, on a server where SSH accepts keys only, root can't log in, the firewall opens only what is needed, password guessers get banned, logs rotate and certificates are watched.
The plan
| # | Step | Lessons |
|---|---|---|
| 1 | Admin account, SSH keys, hardened sshd, narrow sudo |
SSH Keys, Hardening the SSH Server, sudo & sudoers |
| 2 | Firewall: SSH from the admin network, 80 and 443 from anywhere | nftables, ufw & firewalld |
| 3 | Automatic security updates | Patching & Unattended Upgrades |
| 4 | The app as a sandboxed, resource-limited service on localhost | Writing a Service Unit, Sandboxing, cgroups v2 |
| 5 | Nginx: HTTP→HTTPS redirect, TLS, proxy headers, HSTS | Nginx as a Reverse Proxy, Terminating TLS |
| 6 | Certificates: ACME in production, a private CA here; expiry check | ACME, Renewal & Expiry Monitoring |
| 7 | fail2ban for SSH and the app's login, log rotation | fail2ban, logrotate |
| 8 | Verify from outside | Listening Sockets, Debugging HTTP |
Part 1: the root-only steps (on your practice VM)
These need root and a booted systemd, so they are a sample session. Run them on a practice VM, with a second SSH session open the whole time:
# as the initial cloud user (ubuntu), from your workstation's key
$ sudo adduser --disabled-password --gecos "" ada
$ sudo groupadd ssh-users && sudo usermod -aG sudo,ssh-users ada
$ sudo install -d -m 700 -o ada -g ada /home/ada/.ssh
$ echo "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIK0x... ada@laptop" | sudo tee /home/ada/.ssh/authorized_keys
$ sudo chown ada:ada /home/ada/.ssh/authorized_keys && sudo chmod 600 /home/ada/.ssh/authorized_keys
$ sudo usermod -aG ssh-users ubuntu # keep the current login working until ada is tested
# step 1: install the validated files from Part 2
$ sudo install -m 600 server/sshd_config.d/10-hardening.conf /etc/ssh/sshd_config.d/
$ sudo sshd -t && sudo systemctl reload ssh
$ ssh ada@web1 sudo -v # from the workstation, in a NEW terminal
# step 2: firewall
$ sudo ufw default deny incoming && sudo ufw default allow outgoing
$ sudo ufw limit from 198.51.100.0/24 to any port 22 proto tcp comment 'admin ssh'
$ sudo ufw allow 80,443/tcp
$ sudo ufw enable
# step 3: updates
$ sudo apt install -y unattended-upgrades needrestart nginx fail2ban
$ sudo dpkg-reconfigure -f noninteractive unattended-upgrades
# steps 4-7: install the files from Part 2, then
$ sudo systemctl daemon-reload && sudo systemctl enable --now notes.service
$ sudo nginx -t && sudo systemctl reload nginx
$ sudo fail2ban-client -t && sudo systemctl enable --now fail2banIn production, step 6 is certbot certonly --webroot -w /var/www/acme -d notes.example.com with a deploy hook that reloads Nginx, instead of the private CA used in Part 2.
Part 2: build and validate every file
Everything that can be checked without root is checked here, in one session, exactly as a CI job for your server configuration repository would do it. It writes the files into server/, validates each with its own tool, then runs the app and Nginx locally (on ports 8080/8443 instead of 80/443) and tests the result from the outside like a client:
set -u
mkdir -p server/{sshd_config.d,sudoers.d,systemd,nginx,fail2ban,logrotate.d,tls} app && cd server
ok() { echo "PASS $1"; }; bad() { echo "FAIL $1"; }
# --- 1. SSH and sudo -------------------------------------------------------------
ssh-keygen -q -t ed25519 -N "" -f tls/hostkey
cat > sshd_config.d/10-hardening.conf <<'EOF'
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
AuthenticationMethods publickey
AllowGroups ssh-users
MaxAuthTries 3
LoginGraceTime 20
X11Forwarding no
AllowAgentForwarding no
AllowTcpForwarding no
EOF
echo "PasswordAuthentication yes" > sshd_config.d/50-cloud-init.conf # what the cloud image ships
printf 'Include %s/sshd_config.d/*.conf\nHostKey %s/tls/hostkey\nPidFile %s/sshd.pid\n' "$PWD" "$PWD" "$PWD" > sshd_config
sshd -t -f sshd_config && ok "sshd syntax"
[ "$(sshd -T -f sshd_config | grep -cE '^(permitrootlogin no|passwordauthentication no|kbdinteractiveauthentication no|allowgroups ssh-users)