Lesson 16 of 16 · Everyday hygiene
Capstone: harden a new server
Use everything in the course to take a fresh Ubuntu server from first login to a hardened HTTPS web server: a personal admin user with sudo and an SSH key, key-only SSH with no root login, a default-deny firewall, automatic updates, fail2ban, nginx with a Let's Encrypt certificate, HSTS, expiry monitoring and backups. Every step is verified, and the checklist is reusable for any server.
The brief
A new Ubuntu server with a public IP,
rootlogin with a password from the provider's email, and a domainexample.compointing at it. Make it a hardened HTTPS web server, without ever locking yourself out.
Keep one SSH session open at all times until the end, and have the provider's web console available as a fallback.
Step 1: update and create your admin user
# apt update && apt full-upgrade -y
# adduser asha
# usermod -aG sudo asha
# groupadd sshusers && usermod -aG sshusers asha
From your laptop, install your key (lesson 5) and test in a new terminal:
$ ssh-copy-id -i ~/.ssh/id_ed25519.pub asha@203.0.113.10
$ ssh asha@203.0.113.10 'sudo -v && echo sudo ok'
Step 2: harden SSH (lesson 6)
$ sudo tee /etc/ssh/sshd_config.d/00-hardening.conf <<'EOF'
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
AuthenticationMethods publickey
AllowGroups sshusers
MaxAuthTries 3
LoginGraceTime 30
X11Forwarding no
AllowAgentForwarding no
EOF
$ sudo sshd -t && sudo systemctl reload ssh
$ sudo sshd -T | grep -Ei '^(permitrootlogin|passwordauthentication|allowgroups)'
In a new terminal: ssh asha@203.0.113.10 must work, and ssh -o PubkeyAuthentication=no asha@203.0.113.10 must be refused.
Step 3: firewall (lesson 15)
$ sudo ufw default deny incoming
$ sudo ufw default allow outgoing
$ sudo ufw allow from 198.51.100.0/24 to any port 22 proto tcp # your office/VPN range
$ sudo ufw allow 80,443/tcp
$ sudo ufw enable
If you have no fixed range, use sudo ufw limit OpenSSH instead and rely on key-only login and fail2ban. Mirror the same rules in the cloud security group.
Step 4: updates and fail2ban
$ sudo apt install -y unattended-upgrades fail2ban
$ sudo dpkg-reconfigure -plow unattended-upgrades
$ sudo systemctl enable --now fail2ban
$ sudo fail2ban-client status sshd
Step 5: nginx with HTTPS (lessons 7, 11)
$ sudo apt install -y nginx certbot python3-certbot-nginx
$ sudo certbot --nginx -d example.com -d www.example.com --redirect
$ sudo certbot renew --dry-run
Add HSTS to the HTTPS server block once you're sure every subdomain works over HTTPS:
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
ssl_protocols TLSv1.2 TLSv1.3;
server_tokens off;
$ sudo nginx -t && sudo systemctl reload nginx
Step 6: monitoring and backups
- Certificate expiry: an external check that alerts at 21 days left (lesson 12), from a monitoring service or a blackbox exporter.
- Uptime: an external HTTP check of the home page.
- Logins: review
journalctl -u sshfor successful logins; alert on logins from unexpected places if you can. - Backups: automated, off the server, and test a restore. Backups protect availability and are your answer to ransomware.
You've just moved into a new house. You changed the locks (SSH keys, no root), told the gate guard who may enter (firewall), signed up for free repairs (automatic updates), hired someone to turn away people rattling the door (fail2ban), put curtains on the windows (HTTPS), set a reminder before the insurance runs out (expiry alerts), and kept copies of the important papers at a friend's house (backups).
Step 7: verify from outside
From another machine you own:
$ nmap -Pn 203.0.113.10 # expect 22 (maybe filtered), 80, 443 only
$ curl -sI http://example.com | head -1 # HTTP/1.1 301 Moved Permanently
$ curl -sI https://example.com | grep -i strict-transport
$ ssh root@203.0.113.10 # Permission denied (publickey)
The reusable checklist
| Area | Check | Lesson |
|---|---|---|
| Accounts | Personal admin user with sudo; no shared logins | 1, 2 |
| SSH | Key-only, no root, AllowGroups, verified with sshd -T |
4–6 |
| Exposure | ss -tulpn clean; internal services on 127.0.0.1 |
15 |
| Firewall | Default deny; SSH restricted; security group mirrors it | 15 |
| Updates | unattended-upgrades on; reboots planned | 15 |
| Brute force | fail2ban active | 6 |
| HTTPS | Valid certificate, full chain, redirect, HSTS, TLS 1.2+ | 7–11 |
| Certificates | Auto-renewal tested; external expiry alert | 11, 12 |
| Secrets | None in code or images; stored in a secret manager | 14 |
| Recovery | Off-server backups, restore tested; console access works | 1 |
Recap
- Order matters: user and key → SSH hardening → firewall (SSH first) → updates → HTTPS → monitoring.
- Verify every step, from a new session and from outside the server.
- Keep the checklist; run it on every new server, and again after big changes.
This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.