Hardening without visibility is half the job. This lesson makes sure Ubuntu keeps its logs, sends them somewhere an attacker cannot erase, blocks noisy sources, and scores your work.
Persistent journald
By default a minimal server may keep the journal only in memory, losing it at reboot.
sudo mkdir -p /var/log/journal
sudo sed -i 's/^#\?Storage=.*/Storage=persistent/' /etc/systemd/journald.conf
sudo sed -i 's/^#\?SystemMaxUse=.*/SystemMaxUse=1G/' /etc/systemd/journald.conf
sudo systemctl restart systemd-journald
journalctl --disk-usageUseful queries to know:
journalctl -u ssh --since "1 hour ago" # SSH activity
journalctl _COMM=sudo --since today # sudo usage
journalctl -p err -b # errors since bootShip logs off the host
If an attacker gains root, they can wipe local logs. A copy on another system survives. Forward with rsyslog:
sudo apt install -y rsyslog
sudo tee /etc/rsyslog.d/90-forward.conf >/dev/null <<'EOF'
*.* action(type="omfwd" target="logs.example.internal" port="514" protocol="tcp"
queue.type="LinkedList" queue.size="10000"
action.resumeRetryCount="-1")
EOF
sudo systemctl restart rsyslogThe queue lines buffer events if the collector is briefly down. For anything crossing a network you do not fully trust, use TLS (rsyslog's gtls stream driver) or a log agent such as Wazuh, Elastic Agent or the Splunk forwarder. Plain TCP is only acceptable inside a protected management network.
fail2ban: quiet the noise
With key-only SSH, brute force cannot succeed, but it still floods logs and wastes resources. fail2ban reads logs and bans repeat offenders.
sudo apt install -y fail2ban
sudo tee /etc/fail2ban/jail.local >/dev/null <<'EOF'
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
ignoreip = 127.0.0.1/8 203.0.113.0/24
[sshd]
enabled = true
backend = systemd
EOF
sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshdPut your admin range in ignoreip so you cannot ban yourself. Treat fail2ban as noise reduction, not a substitute for key-only SSH and a firewall.
File integrity with AIDE
AIDE records hashes of important files so you can see what changed.
sudo apt install -y aide
sudo aideinit # builds the initial database (can take a while)
sudo cp /var/lib/aide/aide.db.new /var/lib/aide/aide.db
sudo aide --check # compare now against the baselineRun the check on a schedule and send the report off-host. After legitimate changes (patches), update the baseline.
Score it: Lynis
Lynis is an open-source auditing tool that tests your configuration and gives a hardening index plus concrete suggestions.
sudo apt install -y lynis
sudo lynis audit system
sudo grep -E 'hardening_index|warning\[\]|suggestion\[\]' /var/log/lynis-report.dat | head -30Read the Warnings first, then the Suggestions. Do not chase a perfect score: some suggestions do not fit your server. Record the starting index now and the final one at the end of the track.
On Ubuntu Pro? The Ubuntu Security Guide (USG) automates CIS and DISA-STIG profiles:
sudo usg audit cis_level1_serverreports, andsudo usg fix cis_level1_serverremediates. Runauditfirst and review whatfixwould change on production.
Events worth alerting on
| Event | Where it shows | Why it matters |
|---|---|---|
| Burst of failed SSH logins | journal / auth log | Brute force or credential stuffing |
| Successful SSH from a new source | journal / auth log | Possible stolen key |
| New user or group; sudoers change | auditd keys identity, scope |
Persistence or privilege change |
Change to sshd_config or cron |
auditd keys sshd, cron |
Backdoor or persistence |
| New listening port | scheduled ss -tulpn diff |
Unexpected service or implant |
| Root command by a real user | auditd key root_exec |
Privileged activity |
| Log service stopped or logs cleared | rsyslog/auditd status | Defence evasion |
🧪 Lab
- Make the journal persistent and run the three example queries.
- Install and enable fail2ban; check
fail2ban-client status sshd. - Run
lynis audit system; record the hardening index and fix three suggestions. - Build an AIDE baseline, change a file in
/etc, and watchaide --checkreport it.
Checkpoint
- Why forward logs off the host rather than keep them only locally?
- What is fail2ban for when SSH is already key-only?
- Why add your admin range to fail2ban's
ignoreip? - What does the Lynis hardening index tell you, and why not chase 100?
- Name three Linux events you would alert on.