Skip to content

Kernel sysctl hardening for Linux servers

A practical set of sysctl tunables for network, kernel, and filesystem hardening — the values worth applying, with the trade-offs each one carries.

The Linux kernel exposes hundreds of runtime knobs through /proc/sys/. Most defaults are fine; a handful are not, and a handful more are conservative defaults that you can tighten on a server that does not need them open. This article is the curated list of sysctl tunables worth applying as part of a hardening baseline, with the trade-offs each one carries — because every “secure by default” recommendation breaks somebody’s workload.

How to verify

Check the current effective values and where they came from:

sudo sysctl -a 2>/dev/null | grep -E 'rp_filter|tcp_syncookies|kptr_restrict|kernel.dmesg_restrict' | sort
sudo sysctl --system | tail   # show which file last set each value
ls /etc/sysctl.d/
sudo cat /etc/sysctl.d/99-hardening.conf 2>/dev/null

sysctl --system reads every file under /etc/sysctl.d/, /usr/lib/sysctl.d/, and /etc/sysctl.conf in order; the last write wins. The order matters — distro defaults often live in /usr/lib/sysctl.d/ and your overrides should be in /etc/sysctl.d/99-*.conf to come last.

What’s happening

sysctl values live in the kernel’s procfs. Writing to /proc/sys/... takes effect immediately; writing to a file under /etc/sysctl.d/ makes the value persist across reboots when systemd applies the contents on next boot. The naming maps directly: net.ipv4.tcp_syncookies is the file /proc/sys/net/ipv4/tcp_syncookies.

Hardening tunables fall into three families. Network: protect against SYN floods, IP spoofing, source routing, ICMP redirects. Kernel: hide pointer addresses from non-root users, restrict dmesg, restrict ptrace. Filesystem: protect against hard-link and symlink races, restrict access to BPF and user namespaces.

The pitfall with every recommendation list is that some workloads need the “insecure” default. A container host that uses user namespaces relies on kernel.unprivileged_userns_clone=1; setting it to 0 breaks rootless Podman. The right approach is to read what each knob does and write a host-class baseline that turns off only what the class never needs.

The procedure

  1. Create one operator-owned sysctl file. /etc/sysctl.d/99-stackharbor-hardening.conf. Keep distro files alone; your file ends in 99 so it loads last and wins.
  2. Network hardening — apply on every internet-exposed host. net.ipv4.tcp_syncookies = 1, net.ipv4.tcp_rfc1337 = 1, 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.ipv4.conf.all.send_redirects = 0, net.ipv4.conf.all.accept_redirects = 0, net.ipv4.icmp_echo_ignore_broadcasts = 1, net.ipv4.icmp_ignore_bogus_error_responses = 1. Mirror each ipv4 value with the ipv6 equivalent where it exists.
  3. IP forwarding off by default. net.ipv4.ip_forward = 0 and net.ipv6.conf.all.forwarding = 0. Container/router hosts override this with their own file (Docker writes /etc/sysctl.d/...).
  4. Kernel info disclosure. kernel.kptr_restrict = 2 (hide kernel pointers from all users), kernel.dmesg_restrict = 1 (restrict dmesg to CAP_SYSLOG), kernel.perf_event_paranoid = 3 (disable perf events for non-root), kernel.yama.ptrace_scope = 1 (ptrace only your descendants).
  5. Filesystem races. fs.protected_symlinks = 1, fs.protected_hardlinks = 1, fs.protected_fifos = 2, fs.protected_regular = 2. These prevent classic /tmp symlink attacks on world-writable directories.
  6. Core dumps. fs.suid_dumpable = 0 (no core dumps from setuid binaries) plus kernel.core_pattern = |/bin/false if you want to disable core dumps entirely. A core dump from a vulnerable daemon may contain secrets in memory.
  7. BPF and user namespaces — opt-in. kernel.unprivileged_bpf_disabled = 1 if no workload needs eBPF from non-root. kernel.unprivileged_userns_clone = 0 if no rootless container runtime is in use. Both break common dev workflows — verify the host’s role first.
  8. Apply and verify. sudo sysctl --system reads everything. sudo sysctl -a | grep tcp_syncookies confirms the value is 1. Reboot and re-check; a value that disappears at boot was set in /proc directly, not in a config file.

Common pitfalls

  • The rp_filter = 1 (strict reverse path filtering) drops packets that arrive on an interface that is not the route back to the source. On a host with asymmetric routing (multi-WAN, dual-NIC with BGP), this drops legitimate traffic. Use rp_filter = 2 (loose) or write per-interface overrides.
  • net.ipv4.ip_forward = 0 on a Docker host breaks container networking — Docker writes its own sysctl file to override. If you also write ip_forward = 0 in your 99-*.conf file, the order of the files determines who wins (Docker is typically 100-docker.conf so it wins anyway, but verify with sysctl --system | tail).
  • kernel.ptrace_scope = 1 breaks gdb for non-root debugging across processes — annoying for developers, fine for servers. Setting it to 3 is one-way; you must reboot to change it back.
  • A net.core.somaxconn = 1024 (raising the listen backlog) is necessary for high-concurrency services but the application also needs to call listen() with a matching backlog argument — without that, the sysctl change has no effect.
  • Some VMs and cloud providers re-write /etc/sysctl.conf from cloud-init on every boot, overwriting your changes. Put your overrides in /etc/sysctl.d/ (which cloud-init typically leaves alone) and confirm by rebooting and re-checking.

In the engagements we run, sysctl baselines are part of the OS image and applied via configuration management — never hand-edited per host. The drift detection that catches “someone changed tcp_syncookies on the database server” is the kind of operating hygiene that lives in Managed Operations.