Lesson 6 of 12 · 15 min · Intermediate

Ubuntu: central logging, fail2ban and a Lynis audit

Make logs persistent and ship them off the host, add fail2ban for noisy sources, check file integrity with AIDE, and score your hardening with Lynis and Ubuntu Security Guide. Ends with the events worth alerting on.

In this lesson you will

  • Make journald persistent and forward logs off the server
  • Configure fail2ban for SSH and read its status
  • Use Lynis (and USG on Ubuntu Pro) to score and improve hardening
  • List the Linux events worth alerting on

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.

From event to alert: a logging pipeline

Persistent journald

By default a minimal server may keep the journal only in memory, losing it at reboot.

bash
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-usage

Useful queries to know:

bash
journalctl -u ssh --since "1 hour ago"          # SSH activity
journalctl _COMM=sudo --since today             # sudo usage
journalctl -p err -b                            # errors since boot

Ship logs off the host

If an attacker gains root, they can wipe local logs. A copy on another system survives. Forward with rsyslog:

bash
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 rsyslog

The 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.

bash
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 sshd

Put 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.

bash
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 baseline

Run 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.

bash
sudo apt install -y lynis
sudo lynis audit system
sudo grep -E 'hardening_index|warning\[\]|suggestion\[\]' /var/log/lynis-report.dat | head -30

Read 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_server reports, and sudo usg fix cis_level1_server remediates. Run audit first and review what fix would 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

  1. Make the journal persistent and run the three example queries.
  2. Install and enable fail2ban; check fail2ban-client status sshd.
  3. Run lynis audit system; record the hardening index and fix three suggestions.
  4. Build an AIDE baseline, change a file in /etc, and watch aide --check report it.

Checkpoint

  1. Why forward logs off the host rather than keep them only locally?
  2. What is fail2ban for when SSH is already key-only?
  3. Why add your admin range to fail2ban's ignoreip?
  4. What does the Lynis hardening index tell you, and why not chase 100?
  5. Name three Linux events you would alert on.