Linux Server Hardening Beyond the Baseline: AppArmor and Kernel Parameters (sysctl)

The two kernel-side hardening layers most guides stop short of: AppArmor to limit what a compromised process can do, and sysctl to stop the kernel behaving like a workstation.

Share

A firewall decides which ports are reachable. These two layers decide what software can do once it is running, and how the kernel treats the traffic that reaches the box. The first 24 hours on a new VPS baseline is where access and patching get put in place; this guide is the containment layer that sits on top.

AppArmor restricts what a confined process can touch — a file, a socket, an executable — profile by profile, enforced by the kernel. sysctl flips kernel defaults that are tuned for a workstation's compatibility rather than a public server's caution. Put both together and a compromise of your most exposed service is contained, while the kernel handles network traffic the way a hardened host should.

AppArmor: mandatory access control

AppArmor is already on your Ubuntu server. That is not the same as it being useful.

Most Ubuntu installs ship with AppArmor running but with profiles in complain mode — which logs violations without actually preventing anything. Complain mode is fine for testing. On a production server you want enforce mode: the kernel actively blocks processes from doing things their profile does not permit. The difference is the difference between an alarm system that logs when someone opens a door, and one that locks the door.

This half covers checking current status, installing the profile tools, switching the profiles that matter to enforce mode, and handling the case where an enforced profile breaks a service.

Check current AppArmor status

sudo aa-status

This shows how many profiles are loaded, how many are in enforce mode versus complain mode, and which processes are confined. A default Ubuntu 22.04 server typically has a handful of profiles loaded — dhclient, lsb_release, nvidia_modprobe — mostly in enforce mode, but not much that matters for server security specifically.

If aa-status is missing:

apt install apparmor apparmor-utils -y
systemctl enable --now apparmor

Install the profile packages

The base AppArmor install is minimal. The useful profiles live in separate packages:

apt install apparmor-profiles apparmor-profiles-extra -y

That installs profiles for nginx, MySQL, Apache, BIND and other server software. Most land in /etc/apparmor.d/ in disabled or complain state; you bring them to enforce manually.

Check which profiles cover your services

ls /etc/apparmor.d/

Profiles of interest on a typical web server:

  • usr.sbin.nginx — nginx
  • usr.sbin.mysqld — MySQL
  • usr.bin.man — man pages (low value, but commonly present)

For each service you want to confine:

# Load a profile in enforce mode
aa-enforce /etc/apparmor.d/usr.sbin.nginx

# Or complain mode first, to test
aa-complain /etc/apparmor.d/usr.sbin.nginx

Enforce nginx

Nginx is worth confining because it is the most exposed service on most web servers — it handles all inbound traffic and, if compromised, could read arbitrary files. Its hardening guide covers the web layer; AppArmor is the containment behind it.

Load the nginx profile in enforce mode:

aa-enforce /etc/apparmor.d/usr.sbin.nginx

Restart nginx and confirm it serves:

systemctl restart nginx
systemctl status nginx

If it fails, the denials are in the journal:

journalctl -xe | grep apparmor

A denial looks like this:

apparmor="DENIED" operation="open" profile="nginx" name="/etc/ssl/private/yourkey.pem"

That means the nginx profile does not permit reading that path. Common causes: custom SSL certificate locations, non-standard config include paths, or third-party modules reading from unusual directories.

Handling profile violations

Two ways to give a profile the permission it is missing.

Option A — let aa-logprof do it:

aa-logprof

aa-logprof reads violations from the logs and walks you through adding the missing permissions interactively. It is the right tool when a profile has a few gaps.

Option B — edit the profile manually. Profiles are readable text files in /etc/apparmor.d/. To allow a path:

/etc/ssl/private/*.pem r,

Reload after editing:

apparmor_parser -r /etc/apparmor.d/usr.sbin.nginx

Check the final status

sudo aa-status

You want your key services listed under processes defined in policy in enforce mode, not complain. The exact number of profiles does not matter; what matters is that the services with a network listener are actually confined. AppArmor does not replace a firewall or key-based auth, but a confined nginx cannot read /etc/shadow even with code execution.

Kernel parameters: sysctl

UFW controls which ports accept connections. sysctl controls what the kernel itself does with network traffic, memory and processes — below the application layer, before any firewall rule applies.

Most servers ship with kernel defaults optimised for broad compatibility. That means behaviours that make sense in a controlled environment stay enabled on a public server where they are pure attack surface. This half covers the parameters worth changing, what each one does, and how to persist them across reboots. None of them require a reboot; they apply immediately.

How sysctl works

# View a single value
sysctl net.ipv4.tcp_syncookies

# Apply a value immediately (not persistent)
sysctl -w net.ipv4.tcp_syncookies=1

# Apply all values from a file
sysctl -p /etc/sysctl.d/99-hardening.conf

Persistent changes live in /etc/sysctl.d/, applied on boot in alphabetical order. 99-hardening.conf loads last, so your values override defaults set elsewhere. UFW, part of the first 24 hours on a new VPS baseline, handles port-level access; sysctl changes how the kernel treats the packets that reach those ports.

Create the hardening config

cat > /etc/sysctl.d/99-hardening.conf << 'EOF'
# ── Network: SYN flood protection ──────────────────────────────
net.ipv4.tcp_syncookies = 1

# ── Network: IP spoofing / source routing ──────────────────────
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
net.ipv6.conf.all.accept_source_route = 0

# ── Network: ICMP redirects ────────────────────────────────────
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.default.secure_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0

# ── Network: Disable IP forwarding (unless this server routes) ──
net.ipv4.ip_forward = 0
net.ipv6.conf.all.forwarding = 0

# ── Network: Log martian packets ───────────────────────────────
net.ipv4.conf.all.log_martians = 1
net.ipv4.conf.default.log_martians = 1

# ── Network: Ignore ICMP broadcast / bogus requests ────────────
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1

# ── Memory: disable magic SysRq, restrict core dumps, ASLR ─────
kernel.sysrq = 0
fs.suid_dumpable = 0
kernel.randomize_va_space = 2

# ── Process: restrict kernel pointer, dmesg, ptrace ────────────
kernel.kptr_restrict = 2
kernel.dmesg_restrict = 1
kernel.yama.ptrace_scope = 1
EOF

Apply immediately:

sysctl -p /etc/sysctl.d/99-hardening.conf

If you see errors, they identify parameters your kernel or distribution does not support. Most are universal; a few (like kernel.yama.ptrace_scope) depend on kernel modules.

What each parameter does

  • tcp_syncookies = 1 — SYN flood mitigation. With the backlog full, the kernel sends a cryptographic cookie instead of allocating state; legitimate clients complete the handshake while flood packets are discarded. It is the Linux default these days — verify it has not been reset.
  • rp_filter = 1 — reverse path filtering. The kernel checks that an incoming packet arrived on the interface it would use to reply to that source, and drops the rest. Blocks packets with a forged source address.
  • accept_source_route = 0 — source routing lets a packet pick its own path; no legitimate traffic needs it, and disabling it removes a traffic-redirection vector.
  • accept_redirects = 0 / send_redirects = 0 — ICMP redirects tell a host to use a different gateway, a mechanism abused for man-in-the-middle. A server has no reason to accept or send them.
  • ip_forward = 0 — packet forwarding between interfaces; leave 0 unless the box routes. If you run WireGuard you set it to 1 in the WireGuard setup guide and skip this parameter.
  • log_martians = 1 — logs packets with impossible source addresses (private ranges on public interfaces, broadcast sources), a sign of spoofing or misconfigured upstream gear.
  • icmp_echo_ignore_broadcasts = 1 — stops broadcast ping replies used in Smurf amplification.
  • kernel.sysrq = 0 — disables the Magic SysRq key combination you never use on a remote box with no keyboard.
  • fs.suid_dumpable = 0 — stops setuid programs producing core dumps that can leak keys or memory.
  • kernel.randomize_va_space = 2 — ASLR at maximum, making memory-corruption exploits substantially harder. Usually already set on modern Ubuntu; worth verifying.
  • kernel.kptr_restrict = 2 — hides kernel pointers from unprivileged users, denying attackers a map of kernel memory.
  • kernel.dmesg_restrict = 1 — restricts dmesg to privileged users; kernel messages leak hardware addresses and module paths useful for fingerprinting.
  • kernel.yama.ptrace_scope = 1 — limits ptrace() to a process's own children, stopping an unprivileged process from attaching to another and reading its memory.

Exceptions: WireGuard and Docker

If the server runs WireGuard, set net.ipv4.ip_forward = 1 (and net.ipv6.conf.all.forwarding = 1 for IPv6) or the VPN cannot forward packets.

If it runs Docker, Docker manages its own iptables, requires ip_forward = 1, and sets bridge parameters itself — if you see routing issues with containers, do not force net.ipv4.conf.all.rp_filter = 1; it can interfere with bridge interfaces.

Verify

sysctl net.ipv4.tcp_syncookies net.ipv4.conf.all.rp_filter kernel.randomize_va_space

Each should return what you set. Reboot, check again, and persistence is confirmed.


These two layers sit underneath whatever runs on top, and they complement the rest of the stack rather than replacing it. CrowdSec handles active threat intelligence at the network layer; the first 24 hours on a new VPS map is where the broader hardening lives. AppArmor contains a compromised process, sysctl stops the kernel working against you, and both are cheap insurance on a box that faces the internet.

## Convertkit Newsletter