Linux Kernel Hardening With sysctl: 10 Settings That Make Attacks Harder


Most Linux servers ship with kernel defaults chosen for compatibility, not security. sysctl lets you change those defaults at runtime — no reboot, no new software. Ten well-chosen settings close off whole classes of attack, and they take five minutes to apply.

How sysctl works

Settings live in /etc/sysctl.conf (or files under /etc/sysctl.d/). Apply them with:

sudo sysctl -p

Check a single value any time with sysctl net.ipv4.tcp_syncookies. Changes apply immediately and survive reboot when written to the config file. Test on a lab machine first — a wrong network setting can lock you out of a remote server.

The 10 settings

1. Block IP spoofing

net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1

Reverse-path filtering drops packets whose source address could not plausibly arrive on that interface. It kills a whole family of spoofing attacks.

2. Stop SYN flood attacks

net.ipv4.tcp_syncookies = 1

SYN cookies let the kernel survive a flood of half-open connections without keeping state for each one. This should be on everywhere.

3. Ignore broadcast pings

net.ipv4.icmp_echo_ignore_broadcasts = 1

Stops your server answering broadcast ping requests — the trick behind Smurf-style amplification attacks.

4. Ignore bogus ICMP error messages

net.ipv4.icmp_ignore_bogus_error_responses = 1

Prevents attackers using crafted ICMP errors to probe or disrupt your network stack.

5. Disable IP forwarding (unless you are a router)

net.ipv4.ip_forward = 0

A server that is not a router should never forward packets between networks. Leaving this on turns your box into a stepping stone.

6. Hide kernel pointers

kernel.kptr_restrict = 2

Stops unprivileged users reading kernel memory addresses from /proc. Kernel addresses are the starting point for most privilege-escalation exploits — do not hand them out for free.

7. Restrict dmesg

kernel.dmesg_restrict = 1

Same idea: kernel log messages often leak addresses and hardware details. Only root should read them.

8. Stop core dumps from setuid programs

fs.suid_dumpable = 0

Core dumps of privileged programs can contain secrets. Disabling them for setuid binaries closes that leak.

9. Disable source routing

net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0

Source-routed packets let an attacker choose the path their traffic takes. No modern network needs this.

10. Log Martian packets

net.ipv4.conf.all.log_martians = 1

Packets arriving from impossible addresses get logged. They are usually a sign someone is probing or spoofing — and now you will see it in your logs.

Verify and keep it

sudo sysctl -p
sudo sysctl -a | grep -E "rp_filter|syncookies|kptr_restrict"

Put the file in version control or your server setup script so every new machine gets the same baseline. Hardening is not a one-time job — it is a default you repeat on every box you build.

Comments

Popular Posts