Lesson 5 of 12 · 18 min · Intermediate

Ubuntu: file system, kernel settings, AppArmor and auditd

Find risky permissions and SUID files, apply safe mount options and kernel hardening, keep AppArmor enforcing, and install auditd rules that record changes to identity, sudo and SSH configuration.

In this lesson you will

  • Find world-writable files, SUID binaries and unowned files
  • Apply mount options and kernel sysctl hardening with known trade-offs
  • Check and use AppArmor in enforce mode
  • Install auditd rules and query them with ausearch

This lesson hardens the host itself: the file system, the kernel's own settings, mandatory access control with AppArmor, and an audit trail of the changes an attacker must make.

Ubuntu host hardening: the layers

Permissions and risky files

Start by finding what should not exist.

bash
sudo find / -xdev -type f -perm -0002 -ls 2>/dev/null          # world-writable files
sudo find / -xdev -type d -perm -0002 ! -perm -1000 -ls 2>/dev/null   # world-writable dirs without sticky bit
sudo find / -xdev \( -nouser -o -nogroup \) -ls 2>/dev/null     # files with no owner
sudo find / -xdev -perm -4000 -type f 2>/dev/null | sort        # SUID binaries
  • World-writable files and directories (without the sticky bit) let any user alter or replace content. Fix with chmod o-w FILE or add the sticky bit: chmod +t DIR.
  • SUID binaries run as their owner (often root). Keep a list of the expected ones (passwd, sudo, mount, su and similar) and investigate anything new. Compare the list to your baseline at each audit.
  • Home directories should not be readable by others: ls -ld /home/* and chmod 750 /home/USER.
  • Set a stricter default umask (027) for new files by editing UMASK 027 in /etc/login.defs. Test your applications afterwards: some expect world-readable files.

Mount options

Mount options stop whole classes of abuse. Edit /etc/fstab carefully and test with sudo mount -a before you reboot.

text
tmpfs   /dev/shm   tmpfs   defaults,noexec,nosuid,nodev   0 0
tmpfs   /tmp       tmpfs   defaults,noexec,nosuid,nodev,size=1G   0 0
Option Effect
nosuid SUID/SGID bits are ignored on that mount
nodev Device files are ignored
noexec Programs cannot be executed from that mount

Caution on noexec for /tmp. Some installers and package scripts run helpers from /tmp. nosuid,nodev are safe nearly everywhere; add noexec after you have tested package installs and your applications. Keep the old line commented so you can revert.

Kernel hardening with sysctl

bash
sudo tee /etc/sysctl.d/98-kernel-hardening.conf >/dev/null <<'EOF'
kernel.randomize_va_space = 2
kernel.kptr_restrict = 2
kernel.dmesg_restrict = 1
kernel.yama.ptrace_scope = 1
kernel.unprivileged_bpf_disabled = 1
net.core.bpf_jit_harden = 2
kernel.kexec_load_disabled = 1
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
fs.suid_dumpable = 0
EOF
sudo sysctl --system

These make memory layouts less predictable, hide kernel addresses from unprivileged users, restrict debugging and BPF, stop kexec, and block classic link-based file attacks. Ubuntu already sets several; setting them explicitly documents your intent.

Unused kernel modules. On a server with no USB storage, blacklisting it removes a physical-access attack path: echo 'install usb-storage /bin/false' | sudo tee /etc/modprobe.d/hardening.conf. Skip it where you need the device.

AppArmor: keep it on

AppArmor confines programs to the files and capabilities their profile allows. It is enabled by default on Ubuntu. Do not turn it off to "fix" a problem; fix the profile.

bash
sudo aa-status                          # profiles loaded, in enforce vs complain mode
sudo apt install -y apparmor-utils
sudo aa-complain /etc/apparmor.d/PROFILE   # log denials without blocking (for tuning)
sudo aa-logprof                         # turn logged denials into profile updates
sudo aa-enforce /etc/apparmor.d/PROFILE    # block as well as log
journalctl -k | grep -i 'apparmor="DENIED"'   # recent denials

Workflow for a new or noisy service: put its profile in complain mode, exercise the application, run aa-logprof to accept legitimate access, then switch to enforce.

auditd: record the changes that matter

The Linux audit subsystem logs security-relevant events. A small, focused rule set gives you a high-value trail without drowning in noise.

bash
sudo apt install -y auditd audispd-plugins
sudo tee /etc/audit/rules.d/50-hardening.rules >/dev/null <<'EOF'
-w /etc/passwd -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/group -p wa -k identity
-w /etc/sudoers -p wa -k scope
-w /etc/sudoers.d/ -p wa -k scope
-w /etc/ssh/sshd_config -p wa -k sshd
-w /etc/ssh/sshd_config.d/ -p wa -k sshd
-w /etc/crontab -p wa -k cron
-w /etc/cron.d/ -p wa -k cron
-w /var/log/lastlog -p wa -k logins
-a always,exit -F arch=b64 -S execve -F euid=0 -F auid>=1000 -F auid!=unset -k root_exec
-a always,exit -F arch=b64 -S adjtimex,settimeofday -k time_change
EOF
sudo augenrules --load
sudo auditctl -l                       # list active rules

Read the trail:

bash
sudo ausearch -k identity -i --start today      # changes to user and group files
sudo ausearch -k root_exec -i --start today | head   # commands run as root by a real user
sudo aureport --summary                          # overview

Two cautions. The root_exec rule can be chatty on busy hosts; watch disk use and trim it if needed. Adding -e 2 as the last line makes the rules immutable until reboot, which is excellent against tampering but means you cannot change rules without a restart. Add it only when your rule set is final.

🧪 Lab

  1. Run the four find commands and save the output as a baseline. Fix anything world-writable you cannot justify.
  2. Apply the kernel sysctl file and verify two values with sysctl.
  3. Run aa-status; identify one profile in complain mode and decide how to move it to enforce.
  4. Install the audit rules, then create a test user and confirm ausearch -k identity -i shows it.

Checkpoint

  1. Why is a world-writable directory without the sticky bit a problem?
  2. What do nosuid, nodev and noexec each prevent?
  3. What is the correct workflow for tuning an AppArmor profile?
  4. Which audit key would show someone editing /etc/sudoers?
  5. What does -e 2 do in an audit rules file, and when should you use it?