A comprehensive Ubuntu server hardening script by Terra Consults.
- System updates — Upgrades all packages before hardening begins
- Kernel hardening — Applies secure
sysctlparameters (ASLR, BPF JIT hardening, restricteddmesg, etc.) - Firewall (UFW) — Configures deny-by-default ingress; allows SSH, HTTP, and HTTPS
- Fail2Ban — Protects SSH with incremental banning (3 retries, 24 h ban, doubles on repeat offenders)
- SSH hardening — Prompts for a custom SSH port and optionally disables root login with a new sudo user
- Docker — Installs Docker CE from the official Docker repository
- Audit logging — Deploys
auditdrules tracking privileged commands, auth events, and file changes - AppArmor — Ensures AppArmor is installed and active
- Password policy — Enforces strong passwords via
libpam-pwquality(min 12 chars, mixed character classes) - Log retention — Configures
journald,auditd, andlogrotatewith consistent weekly rotation - Unattended upgrades — Enables automatic security updates
- Uncommon kernel modules disabled — Blocks
dccp,sctp,rds,tipc, and USB mass storage - Legal login banner — Sets
/etc/issueand/etc/issue.net - Lynis audit — Runs an initial system audit report at the end
- Ubuntu (any supported LTS release)
- Root or
sudoaccess
sudo bash harden-vps.shThe script is interactive — it will prompt for:
- A new SSH port
- Whether to disable root login (and a new sudo username if so)
/tmp noexecis intentionally skipped to maintain compatibility with Docker builds.- Port 22 is kept open alongside your custom SSH port during setup. You can remove it manually once you have confirmed access on the new port.
- After the script completes, test SSH access in a new terminal before closing your current session.