Every "we got hacked" post-mortem I've read in the last five years has the same root cause: an unpatched package, an SSH password that was hunter2, an exposed admin port, or a service running as root that didn't need to. None of them were exotic zero-days. They were the boring failures that a basic hardening pass would have prevented. This article is that pass.
I'm not going to give you a 200-item CIS checklist and call it a strategy. I'm going to give you the 20% of hardening that prevents 80% of real attacks, with the actual commands and configs you can copy-paste onto a fresh Ubuntu/Debian box. The goal isn't to pass an audit. The goal is to make your server boring to attackers so they move on to an easier target.
$32
Prerequisites
- A freshly installed Debian 12 or Ubuntu 22.04+ server. The patterns transfer to RHEL-family with package-name substitutions (
dnfinstead ofapt,firewalldinstead ofufw). - Root or sudo access. We'll create a non-root user partway through.
- A second terminal session open while you make SSH changes. The first time you lock yourself out of SSH is the last time you skip this step.
- An SSH key pair generated locally:
ssh-keygen -t ed25519 -a 100 -C "you@laptop". Ed25519 is the right choice in 2025; RSA still works but the keys are larger and slower.
You also need an out-of-band access method-a cloud provider's serial console, an IPMI session, or a VPS provider's web VNC. When SSH dies, this is how you get back in.
$33
$34
$35
$36
$37
$38
$39
$3a
$3b
$3c
$3d
$3e
$3f
Testing and Verification
After hardening, run a verification pass:
# CIS scan sudo oscap xccdf eval --profile cis_level1_server \ --report /tmp/cis-report.html \ /usr/share/xml/scap/ssg/content/ssg-ubuntu2204-ds.xml # Ly Lynis (third-party hardening scanner) sudo apt-get install -y lynis sudo lynis audit system --quick # Review /var/log/lynis-report.dat and /var/log/lynis.log # Verify firewall sudo ufw status verbose # Verify SSH config sudo sshd -T | grep -E 'permitrootlogin|passwordauthentication|allowusers' # Verify no listening on 0.0.0.0 except expected sudo ss -tulpn | grep '0.0.0.0:*' # Verify sysctl sudo sysctl net.ipv4.tcp_syncookies kernel.kptr_restrict kernel.dmesg_restrict # Verify audit rules loaded sudo auditctl -l | wc -l These commands give you a snapshot of the current hardening state. Run them monthly as a drift check-configurations silently change when engineers debug issues.
$40
Cost Considerations
Most hardening is free-just config files. The real costs:
- Engineer time - Initial hardening of a server takes 2-4 hours. Templating it in Ansible takes a day but pays back forever.
- Compliance tooling - CIS-CAT Pro is paid; OpenSCAP + SSG is free. Lynis is free; Lynis Enterprise is paid. For most teams, the free tools are enough.
- Livepatching - Ubuntu Pro Livepatch is free up to 5 machines, paid beyond. Canonical Livepatch for more machines is $75/year/machine. RHEL kpatch requires a RHEL subscription.
- SIEM - Splunk gets expensive fast. Wazuh is open-source. Elastic Security has a free tier. For a small fleet, Loki + Grafana + some custom alerting is enough.
- Audit log storage - Audit logs are verbose. Budget 100MB-1GB per busy server per day. Compress and rotate aggressively.
The most expensive "savings" is skipping hardening. A single incident response engagement runs $50K-$200K. The hardening checklist in this article would have prevented most of them.
$41
Real-World Use Case: Hardening a Public-Facing Web Server
A client came to me with a 4-year-old Ubuntu 18.04 box running their marketing site, an outdated WordPress, and an exposed phpMyAdmin. They'd been defaced twice in six months. Here's what we did in one afternoon:
- Snapshot the VM (cloud provider backup) as a rollback.
- Create a fresh Ubuntu 22.04 server, apply the hardening checklist in this article.
- Migrate the WordPress site to the new server (no phpMyAdmin-command-line
wp-clionly). - Put Cloudflare in front (free tier) for WAF + DDoS.
- Set up daily AIDE checks and weekly Lynis scans, emailed to the dev team.
- Enable unattended-upgrades.
- Decommission the old server.
Two years later, the new server has had zero successful attacks. The defacements stopped. The client spent $0 on security tooling. The lesson: most real-world compromises are preventable with basics, applied consistently.
Conclusion
Server hardening isn't glamorous. It's a checklist of boring config changes that, applied consistently, prevents 80% of real-world attacks. The 20% of hardening that matters: SSH key-only auth, firewall defaults, automatic security updates, fail2ban, sysctl hardening, auditd, and removing unused services.
Do it once manually so you understand each step. Then codify it in Ansible and enforce it on every server you provision. The goal isn't to be unbreakable-nothing is. The goal is to be uninteresting enough that attackers move on.
Originally published on Mango Developer.