Skip to content
Hardening an Ubuntu Server: Our Battle-Tested Playbook

Hardening an Ubuntu Server: Our Battle-Tested Playbook

· 2025-05-29

We recently had to rebuild an Ubuntu server from scratch after a nasty breach. While the code also had issues, the server itself wasn't hardened properly or audited. The old setup was a mess with files dumped in uploads/, .git folders exposed to the web, and basic security tools missing. This time, we weren’t screwing around. Here’s how we locked down the new server to make sure it’s clean, secure, and stays that way.

Phase 0: Pre-Kick Off

Start with a clean new server. We used Ubuntu 24.04 for this.

Phase 1: Kick Off with Screen and Updates

First, we fire up a detached screen session and get the system current:

screen -S secure-setup
sudo apt update && sudo apt upgrade -y
sudo timedatectl set-timezone Asia/Kolkata

No excuses—always start with a fresh base.


Phase 2: Install the Must-Haves

We pull in everything we need in one shot: UFW, Fail2Ban, ClamAV, rootkit scanners, and ModSecurity dependencies. No fluff, just the essentials:

sudo apt install -y nginx ufw fail2ban rkhunter chkrootkit clamav clamav-daemon logrotate unattended-upgrades git build-essential libpcre3 libpcre3-dev libssl-dev zlib1g-dev certbot python3-certbot-nginx gcc make autoconf automake libtool libcurl4-openssl-dev liblua5.3-dev libfuzzy-dev ssdeep gettext pkg-config libgeoip-dev libyajl-dev doxygen libpcre2-dev zlib1g wget libsystemd-dev libevent-dev curl

Note: Tools like rkhunter and fail2ban throw config prompts even with “no configuration” selected. If you know a clean way to skip those, hit me up.


Phase 3: Lock Down SSH and Firewall

Root logins? Password auth? Not on our watch. UFW gets strict rules:

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow http
sudo ufw allow https
sudo ufw --force enable

sudo tee /etc/ssh/sshd_config.d/00-hardening.conf >/dev/null <<EOF
PermitRootLogin no
PasswordAuthentication no
EOF
sudo sshd -t && sudo systemctl restart ssh
sudo sshd -T | grep -E 'permitrootlogin|passwordauthentication'

Before running this, make sure you can log in with an SSH key, and keep your current session open until a new one connects. The settings go in a drop-in file because sshd uses the first value it finds, and many cloud images ship /etc/ssh/sshd_config.d/50-cloud-init.conf with PasswordAuthentication yes, which would override an edit to the main file. On Ubuntu 24.04 the service is ssh, not sshd. The last line shows the settings sshd is actually using.


Phase 4: Set Up Fail2Ban

We configure Fail2Ban to catch SSH and Nginx brute-force attempts:

sudo tee /etc/fail2ban/jail.local >/dev/null <<EOF
[sshd]
enabled = true
port = ssh
logpath = /var/log/auth.log
maxretry = 5

[nginx-http-auth]
enabled = true
port = http,https
logpath = /var/log/nginx/error.log
maxretry = 5
EOF

sudo systemctl restart fail2ban

Phase 5: Schedule Nightly Scans

ClamAV, rkhunter, and chkrootkit run every night to keep things clean:

sudo tee -a /etc/crontab >/dev/null <<'EOF'
0 2 * * * root clamscan -r -i --exclude-dir='^/(proc|sys|dev|run)' / >> /var/log/clamav/scan.log 2>&1
0 3 * * * root rkhunter --check --skip-keypress
0 4 * * * root chkrootkit
EOF

These lines include a user (root), so they go in /etc/crontab rather than crontab -e, which doesn't take a user field. The ClamAV scan skips /proc, /sys, /dev and /run, which hold virtual files rather than anything worth scanning.


Phase 6: Build ModSecurity for Nginx

ModSecurity isn’t in Ubuntu’s repos, so we compile it and build the nginx connector as a dynamic module. The module has to be built against the same nginx version that's installed, so check it first with nginx -v. Ubuntu 24.04 ships 1.24.0:

cd /opt
sudo git clone https://github.com/owasp-modsecurity/ModSecurity.git
cd ModSecurity && sudo git submodule init && sudo git submodule update
sudo ./build.sh && sudo ./configure && sudo make && sudo make install

cd /opt
sudo git clone https://github.com/owasp-modsecurity/ModSecurity-nginx.git
sudo wget https://nginx.org/download/nginx-1.24.0.tar.gz
sudo tar -xzf nginx-1.24.0.tar.gz

cd nginx-1.24.0
sudo ./configure --with-compat --add-dynamic-module=/opt/ModSecurity-nginx
sudo make modules
sudo cp objs/ngx_http_modsecurity_module.so /usr/lib/nginx/modules/
echo 'load_module /usr/lib/nginx/modules/ngx_http_modsecurity_module.so;' | sudo tee /etc/nginx/modules-enabled/50-mod-http-modsecurity.conf

The .so file goes with nginx's other modules, and the small file in modules-enabled/ tells nginx to load it. Ubuntu's nginx.conf already includes everything in that folder.


Phase 7: Load OWASP CRS Rules

ModSecurity needs a base config and rules to shine. We start from ModSecurity's recommended config, switch it from detection-only to blocking, then grab the OWASP Core Rule Set:

sudo cp /opt/ModSecurity/modsecurity.conf-recommended /etc/nginx/modsecurity.conf
sudo cp /opt/ModSecurity/unicode.mapping /etc/nginx/
sudo sed -i 's/^SecRuleEngine .*/SecRuleEngine On/' /etc/nginx/modsecurity.conf

sudo git clone https://github.com/coreruleset/coreruleset.git /etc/nginx/owasp-crs
sudo cp /etc/nginx/owasp-crs/crs-setup.conf.example /etc/nginx/owasp-crs/crs-setup.conf

sudo tee -a /etc/nginx/modsecurity.conf <<EOF
Include owasp-crs/crs-setup.conf
Include owasp-crs/rules/*.conf
EOF

In your Nginx server block, add:

modsecurity on;
modsecurity_rules_file /etc/nginx/modsecurity.conf;

Then test and reload:

sudo nginx -t && sudo systemctl reload nginx

If you're putting this in front of a live app, consider leaving SecRuleEngine DetectionOnly for a few days and checking the audit log for false positives before switching to On.


Phase 8: Logs and Auto-Updates

Logs shouldn’t pile up, and packages shouldn’t go stale:

sudo sed -i 's/^\(\s*\)rotate [0-9]*/\1rotate 30/' /etc/logrotate.d/nginx
sudo dpkg-reconfigure -f noninteractive unattended-upgrades

Ubuntu's nginx package already ships a daily logrotate config, so we only raise how many days it keeps. Replacing the file would drop its postrotate step, which tells nginx to reopen its log files. Without it, nginx keeps writing to the rotated file and logs go missing.


✅ Check Your Work

We wrote a script to verify every tool, config, and cronjob is in place. Run it:

~/secure-server-setup/verify-setup.sh

If something’s off, it’ll tell you what to fix.


📦 Scripts to Reuse

We split the setup into two scripts for easy reuse:

Grab them from GitHub.


Use it, automate it, and keep your Ubuntu box tight. Send a pull request if you make it better.

Update: I ran these scripts again on a fresh Ubuntu 26.04 server. What broke, what changed, and a clean way past the config prompts mentioned above are in Revisiting my Ubuntu hardening script.


"Re-written using LLM, hence the emojis" - 17-Nov-2026

Updated on 2026-11-17

Header photo by Brett Sayles from Pexels