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.
Permissions and risky files
Start by finding what should not exist.
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 FILEor 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,suand 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/*andchmod 750 /home/USER. - Set a stricter default
umask(027) for new files by editingUMASK 027in/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.
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
noexecfor/tmp. Some installers and package scripts run helpers from/tmp.nosuid,nodevare safe nearly everywhere; addnoexecafter you have tested package installs and your applications. Keep the old line commented so you can revert.
Kernel hardening with sysctl
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 --systemThese 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.
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 denialsWorkflow 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.
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 rulesRead the trail:
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 # overviewTwo cautions. The
root_execrule can be chatty on busy hosts; watch disk use and trim it if needed. Adding-e 2as 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
- Run the four
findcommands and save the output as a baseline. Fix anything world-writable you cannot justify. - Apply the kernel sysctl file and verify two values with
sysctl. - Run
aa-status; identify one profile in complain mode and decide how to move it to enforce. - Install the audit rules, then create a test user and confirm
ausearch -k identity -ishows it.
Checkpoint
- Why is a world-writable directory without the sticky bit a problem?
- What do
nosuid,nodevandnoexeceach prevent? - What is the correct workflow for tuning an AppArmor profile?
- Which audit key would show someone editing
/etc/sudoers? - What does
-e 2do in an audit rules file, and when should you use it?