Skip to content
Revisiting my Ubuntu hardening script

Revisiting my Ubuntu hardening script

· 2026-11-17

In May 2025 I wrote up how we hardened an Ubuntu server after a breach, with the scripts on GitHub. I hadn't run them since. So I started a throwaway EC2 instance on Ubuntu 26.04, which ships nginx 1.28.3, and ran the script end to end. I did the audit and the test run with Claude Code.

It didn't get through. Some of the problems came from Ubuntu and ModSecurity moving on. Others were in the script from day one.

What broke when I ran it

The postfix prompt. rkhunter and chkrootkit pull in postfix, which stops to ask how mail should be set up. Inside a detached screen session nobody sees the question, so the script just waits. In the last post I asked if anyone knew a clean way around this. This is it:

echo "postfix postfix/main_mailer_type select No configuration" | sudo debconf-set-selections
sudo DEBIAN_FRONTEND=noninteractive apt install -y ...

The variable goes after sudo, because sudo drops your environment.

set -e doesn't cover && chains. The build step was ./configure && make && make install. Configure failed, and the script carried on to the next step as if nothing happened. Bash only stops on the last command of an && list. Now every command is on its own line.

Mbed TLS has submodules of its own. ModSecurity's configure failed with "Mbed TLS was not found". Mbed TLS 4.x has nested submodules, so a plain git submodule update isn't enough:

sudo git submodule update --init --recursive

The nginx modules folder doesn't exist yet. On 26.04, /usr/lib/nginx/modules is only created when a dynamic module is installed. The script now reads the path from nginx and creates it:

MODULES_PATH=$(nginx -V 2>&1 | grep -oP -- '--modules-path=\K\S+')
sudo mkdir -p "$MODULES_PATH"

libpcre3 is gone. Ubuntu 26.04 no longer ships libpcre3-dev, and one missing package fails the whole apt install. ModSecurity uses PCRE2 by default now, so I dropped it.

What was wrong all along

These worked badly or not at all, even on the original server.

Result

With the fixes, the script ran from a fresh Ubuntu 26.04 server to the end on its own, reboot included. The ModSecurity build is the slow part, about 11 minutes on a t3.medium.

verify-setup.sh passed all 31 checks. It now asks sshd for the settings it's actually using (sshd -T), not just what's in the config file. I also sent a few requests at nginx:

normal request:  200
SQL injection:   403
XSS:             403

Both attacks were blocked by the Core Rule Set and logged as "ModSecurity: Access denied". The new ClamAV exclusions worked on a test scan, and a logrotate dry run showed 30 rotations with Ubuntu's postrotate step still in place.

What I'd tell myself in 2025

A setup script you haven't run in a year doesn't work. Run it on a throwaway server before you trust it. Reading the script found half of the problems above. Running it found the rest.

The updated scripts are on GitHub.

Header photo by Brett Sayles from Pexels