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.
- SSH settings never took effect. The script edited
sshd_configbut never restarted sshd. Many cloud images also shipsshd_config.d/50-cloud-init.confwithPasswordAuthentication yes, and sshd uses the first value it reads. The settings now go in/etc/ssh/sshd_config.d/00-hardening.conf, thensshd -tandsystemctl restart ssh(on 24.04 and later the service isssh). The script refuses to turn off passwords if there's no key inauthorized_keys. - Cron lines in the wrong place. The post said
crontab -e, but the lines include a user field, which only works in/etc/crontab. - ModSecurity wasn't actually on. The post never set up
modsecurity.confor switchedSecRuleEnginetoOn. The script did, but never addedmodsecurity on;to nginx. It now goes inconf.d, which turns it on for every site. - Log rotation lost logs. Writing a new
/etc/logrotate.d/nginxdropped Ubuntu'spostrotatestep, which tells nginx to reopen its log files. Now the script only changes the retention to 30 days. - ClamAV scanned
/proc. The nightly scan now skips/proc,/sys,/devand/run. - "Resume after reboot" didn't resume. The script rebooted after updates and you had to start it again by hand. It also only remembered the last phase, so a re-run repeated the reboot. It now adds an
@rebootcrontab entry and removes it at the end. - Nothing was pinned. It cloned whatever was on ModSecurity's and the Core Rule Set's main branches. Now it uses ModSecurity v3.0.17, the nginx connector v1.0.4 and CRS v4.30.0, set at the top of the script.
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.
