Linux kernel hardening is less about adding new security features and more about tuning existing ones to reduce the attack surface and mitigate common exploit techniques.

Let’s see some of these parameters in action. Imagine we have a web server experiencing a high volume of incoming connections. Without proper tuning, this could lead to resource exhaustion or make it easier for an attacker to perform a SYN flood.

Here’s a snippet of a sysctl.conf file demonstrating some hardening measures:

# Disable ICMP redirects (router advertisements)
net.ipv4.conf.all.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0

# Disable IP forwarding (if this machine is not a router)
net.ipv4.ip_forward = 0
net.ipv6.ip_forward = 0

# Enable SYN cookies
net.ipv4.tcp_syncookies = 1

# Increase TCP backlog queue size
net.core.somaxconn = 4096
net.ipv4.tcp_max_syn_backlog = 2048

# Ignore broadcast ICMP requests
net.ipv4.icmp_echo_ignore_broadcasts = 1

# Log spoofed ICMP packets
net.ipv4.icmp_ignore_bogus_error_responses = 1

# Protect against TCP SYN flood attacks
net.ipv4.tcp_syncookies = 1

# Enable TCP TIME_WAIT assassination
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30

The sysctl command allows us to modify kernel parameters at runtime. These parameters control various aspects of the kernel’s behavior, from network stack options to memory management. By strategically adjusting these values, we can make the system more resilient to attacks.

The core problem sysctl hardening addresses is the default configuration of Linux, which is optimized for ease of use and maximum performance, not necessarily maximum security. Many network-related parameters, for instance, are set to values that are permissive and can be exploited by attackers.

Let’s break down some key areas:

Network Stack Hardening

This is arguably the most critical area for hardening.

  • Disabling ICMP Redirects (net.ipv4.conf.all.accept_redirects, net.ipv6.conf.all.accept_redirects): Routers send ICMP redirect messages to inform hosts about a better route to a destination. An attacker can send forged ICMP redirect messages to trick a host into sending traffic through the attacker’s machine, enabling man-in-the-middle attacks. Setting these to 0 prevents the kernel from accepting such redirects.

    • Diagnosis: sysctl net.ipv4.conf.all.accept_redirects
    • Fix: sysctl -w net.ipv4.conf.all.accept_redirects=0
    • Why it works: The system simply ignores any incoming ICMP redirect packets, preventing the routing table from being maliciously altered.
  • Disabling IP Forwarding (net.ipv4.ip_forward, net.ipv6.ip_forward): If your Linux machine is not intended to be a router, it should not forward IP packets between interfaces. Enabling forwarding would allow an attacker to use your machine as a pivot point to attack other machines on the network. Setting these to 0 disables this functionality.

    • Diagnosis: sysctl net.ipv4.ip_forward
    • Fix: sysctl -w net.ipv4.ip_forward=0
    • Why it works: The kernel will not process or forward packets destined for networks other than the local one.
  • Enabling SYN Cookies (net.ipv4.tcp_syncookies): This is a crucial defense against SYN flood attacks. In a SYN flood, an attacker sends a large number of TCP SYN packets, overwhelming the server with half-open connections. SYN cookies allow the server to respond to SYN requests without allocating resources until the client responds with an ACK. If the ACK is received, the connection is established; otherwise, no resources were wasted. Setting this to 1 enables this mechanism.

    • Diagnosis: sysctl net.ipv4.tcp_syncookies
    • Fix: sysctl -w net.ipv4.tcp_syncookies=1
    • Why it works: Instead of allocating memory for each pending connection, the server sends back a SYN/ACK with a cryptographically generated sequence number (the "cookie"). This cookie encodes connection details, and only when the client’s ACK arrives is the connection state fully recreated.
  • Increasing TCP Backlog (net.core.somaxconn, net.ipv4.tcp_max_syn_backlog): These parameters control the maximum number of pending connections that can be queued by the kernel. During high traffic or SYN floods, the default values might be too low, leading to dropped connections. Increasing them can help absorb bursts of legitimate traffic, but it’s a double-edged sword as it can also provide more "room" for an attacker to fill with half-open connections. The key is to tune them appropriately for your expected load, often in conjunction with SYN cookies.

    • Diagnosis: sysctl net.core.somaxconn and sysctl net.ipv4.tcp_max_syn_backlog
    • Fix: sysctl -w net.core.somaxconn=4096 and sysctl -w net.ipv4.tcp_max_syn_backlog=2048 (example values)
    • Why it works: A larger backlog allows the kernel to queue more incoming connection requests before they are accepted or dropped, providing a buffer for legitimate traffic spikes.
  • Ignoring Broadcast ICMP (net.ipv4.icmp_echo_ignore_broadcasts): Hosts should ignore ICMP echo requests sent to broadcast addresses. Allowing them can lead to a "smurf attack" where an attacker sends a ping to a network’s broadcast address, causing every host on that network to respond to the attacker’s spoofed IP address, amplifying traffic. Setting this to 1 makes the system ignore these.

    • Diagnosis: sysctl net.ipv4.icmp_echo_ignore_broadcasts
    • Fix: sysctl -w net.ipv4.icmp_echo_ignore_broadcasts=1
    • Why it works: The system simply discards ICMP echo requests sent to broadcast addresses, preventing it from participating in amplification attacks.
  • Logging Spoofed ICMP (net.ipv4.icmp_ignore_bogus_error_responses): This parameter controls whether the kernel logs ICMP error messages that appear to be malformed or originate from unexpected sources. While primarily for debugging, it can help detect certain types of network scanning or spoofing attempts. Setting it to 1 logs these "bogus" responses.

    • Diagnosis: sysctl net.ipv4.icmp_ignore_bogus_error_responses
    • Fix: sysctl -w net.ipv4.icmp_ignore_bogus_error_responses=1
    • Why it works: The kernel will log ICMP error packets that don’t conform to expected patterns, providing an alert for potentially malicious network activity.
  • TCP TIME_WAIT Reuse (net.ipv4.tcp_tw_reuse, net.ipv4.tcp_fin_timeout): When a TCP connection closes, it enters the TIME_WAIT state for a period to ensure all packets have been delivered. If a server experiences a very high rate of short-lived connections, the large number of sockets stuck in TIME_WAIT can exhaust resources. tcp_tw_reuse=1 allows new TCP connections to reuse sockets in TIME_WAIT state if the new connection’s source IP and port match, and the new connection’s sequence number is higher than the old one (indicating it’s a fresh connection). tcp_fin_timeout reduces the time a socket stays in the FIN-WAIT-2 state, which can also contribute to resource exhaustion.

    • Diagnosis: sysctl net.ipv4.tcp_tw_reuse and sysctl net.ipv4.tcp_fin_timeout
    • Fix: sysctl -w net.ipv4.tcp_tw_reuse=1 and sysctl -w net.ipv4.tcp_fin_timeout=30
    • Why it works: tcp_tw_reuse allows the system to quickly re-establish connections that might otherwise be blocked by lingering TIME_WAIT sockets, while tcp_fin_timeout shortens the FIN-WAIT-2 state, freeing up resources faster after a connection closure.

Applying Changes

To make these changes persistent across reboots, you’ll edit the /etc/sysctl.conf file (or files in /etc/sysctl.d/). After editing, apply the changes immediately with:

sysctl -p

This command reloads the sysctl configuration from the specified file(s).

While these parameters significantly improve security, they are not a silver bullet. They reduce the attack surface and mitigate common exploits, but robust firewall rules (iptables/nftables), intrusion detection systems, and keeping software updated remain essential layers of defense.

The next thing you’ll likely encounter after hardening the network stack is the need to secure inter-process communication (IPC) mechanisms and manage memory more tightly.

Want structured learning?

Take the full Cdk course →