Protego field desk
Advanced Hardening and Threat Hunting · Advanced

L22. Kernel Hardening with sysctl

Video generating

Check back soon for the video lesson on Kernel Hardening with sysctl

Course outlineLesson 22 of 23

The Linux kernel has hundreds of runtime settings that change how it handles network packets, memory and debugging information. A handful of them close common attack paths. Learn how sysctl works, which settings the CIS benchmarks expect, and how to apply them without breaking routers, containers or Kubernetes nodes.

What sysctl Controls

sysctl reads and changes kernel parameters at runtime. Every parameter is also a file under /proc/sys/: net.ipv4.ip_forward is /proc/sys/net/ipv4/ip_forward.

# Read one setting
sysctl net.ipv4.ip_forward

# Search all settings
sysctl -a 2>/dev/null | grep redirects

# Change a setting until the next reboot
sudo sysctl -w net.ipv4.conf.all.accept_redirects=0

Changes made with -w disappear at reboot. To make them permanent, put them in a file under /etc/sysctl.d/ and load it:

sudo nano /etc/sysctl.d/60-hardening.conf
sudo sysctl --system
sysctl --system loads every configuration file in filename order and prints each one as it goes. If the same setting appears in two files, the one loaded last wins, so check that output when a value does not stick.

Network Settings

These settings stop a server from being used to redirect or spoof traffic. They match the network parameter section of the CIS Linux benchmarks.

# /etc/sysctl.d/60-hardening.conf (network part)

# Do not route packets between interfaces (see the warning below)
net.ipv4.ip_forward = 0

# Ignore ICMP redirects, which can be used to reroute traffic through an attacker
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
net.ipv6.conf.default.accept_redirects = 0

# Do not send redirects either (this host is not a router)
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0

# Refuse source-routed packets, which let the sender choose the path
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0

# Drop packets whose source address could not have arrived on that interface
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1

# Log packets with impossible source addresses
net.ipv4.conf.all.log_martians = 1

# Resist SYN flood attacks
net.ipv4.tcp_syncookies = 1

# Ignore broadcast pings and bogus ICMP error messages
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1

Kernel and Filesystem Settings

These make memory attacks harder and limit what an unprivileged user can learn about the kernel.

SettingHardened valueWhat it does
kernel.randomize_va_space2Full address space layout randomization (ASLR), so exploit code cannot rely on fixed addresses
kernel.kptr_restrict1 or 2Hides kernel memory addresses from unprivileged users
kernel.dmesg_restrict1Only privileged users can read the kernel log
kernel.yama.ptrace_scope1A process can only debug its own children, which blocks reading secrets from other processes' memory
fs.suid_dumpable0SUID programs do not write core dumps that could contain secrets
fs.protected_hardlinks1Blocks hard-link tricks against files you do not own
fs.protected_symlinks1Blocks symlink tricks in world-writable sticky directories such as /tmp
kernel.unprivileged_bpf_disabled1Only privileged users can load eBPF programs, a frequent privilege escalation path
Many distributions already ship several of these values. Check before you change anything:

sysctl kernel.randomize_va_space kernel.kptr_restrict kernel.dmesg_restrict kernel.yama.ptrace_scope fs.suid_dumpable

For core dumps, CIS also expects a hard limit in /etc/security/limits.conf (* hard core 0), because fs.suid_dumpable only covers SUID programs.

Do Not Break Routers, Containers or Kubernetes

The network settings assume a plain server. Some hosts are supposed to route:

  • Docker and Podman hosts need net.ipv4.ip_forward = 1 for container networking; Docker sets it when it starts. Forcing it to 0 breaks container traffic.
  • Kubernetes nodes need IP forwarding for pod networking, and most network plugins document their own sysctl requirements.
  • VPN gateways and routers need forwarding and may need send_redirects.
  • Hosts with asymmetric routing (traffic leaves on a different interface than it arrives) can drop legitimate packets with strict rp_filter = 1; use loose mode (2) there.

Document these exceptions the same way you document CIS exceptions: which host, which setting, and why.

Verify and Keep It That Way

# Confirm the live values after loading
sysctl net.ipv4.ip_forward net.ipv4.conf.all.accept_redirects net.ipv4.tcp_syncookies

# Lynis reports kernel settings that differ from its hardened profile (test group KRNL)
sudo lynis audit system --tests-from-group kernel

Keep the file under configuration management so a rebuilt server gets the same settings, and re-run your CIS scan after kernel upgrades.

References

Continue Learning

Exam Focus Points
  • ✓sysctl changes kernel parameters at runtime; persistent settings go in /etc/sysctl.d/ and load with sysctl --system
  • ✓Disable ICMP redirects and source routing, enable reverse path filtering and SYN cookies on hosts that do not route
  • ✓kernel.randomize_va_space = 2 enables full ASLR; kptr_restrict and dmesg_restrict hide kernel information
  • ✓Docker, Podman and Kubernetes hosts need net.ipv4.ip_forward = 1
  • ✓Document sysctl exceptions per host the same way as CIS exceptions
Knowledge Check

1. You set net.ipv4.conf.all.accept_redirects=0 with sysctl -w. What happens after a reboot?

2. Which host should NOT have net.ipv4.ip_forward set to 0?

3. What does kernel.randomize_va_space = 2 do?