Log inSign up

Harden kernel and network settings with sysctl

Put your settings in one file such as /etc/sysctl.d/90-hardening.conf, load it with sudo sysctl --system and read values back with sysctl name. Disable ICMP redirects and source routing, enable reverse path filtering and SYN cookies, and restrict kernel pointers, dmesg and ptrace. Then mount /dev/shm with nodev,nosuid,noexec in /etc/fstab. Leave IP forwarding and IPv6 router advertisements alone if your server relies on them.

Topic: Security · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

What is sysctl and why change it?

sysctl exposes kernel parameters under /proc/sys that control networking, memory and security behaviour. Distribution defaults aim to work everywhere, from laptops to routers, so a few of them are looser than a single purpose internet server needs. Tightening them closes small gaps: accepting route changes from the network, leaking kernel addresses to unprivileged users, or letting one process inspect another.

These settings are a layer on top of the basics, not a replacement. Updates, key based SSH and a firewall (see UFW or firewalld) matter more. Our servers are KVM virtual machines with their own kernel, so every setting in this guide applies to your instance.

How do I read the current values?

Query single keys or search the full list. Nothing changes until you write a value:

sysctl net.ipv4.conf.all.accept_redirects
sysctl kernel.kptr_restrict kernel.dmesg_restrict
sudo sysctl -a 2>/dev/null | grep -E "rp_filter|syncookies"
uname -r

Write down what you see before editing so you can compare later. Values that already match the recommendations below can still go in your file: it documents your intent and survives future default changes.

Which network settings should I set?

Create a drop-in file. Files in /etc/sysctl.d/ load in name order, so a higher number wins over package defaults:

sudo nano /etc/sysctl.d/90-hardening.conf

# ignore ICMP redirects and do not send them
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
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0

# refuse source routed packets
net.ipv4.conf.all.accept_source_route = 0
net.ipv6.conf.all.accept_source_route = 0

# drop packets whose source cannot be routed back
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1

# SYN flood resistance and ICMP hygiene
net.ipv4.tcp_syncookies = 1
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1
net.ipv4.conf.all.log_martians = 1

A server with one network interface is not a router, so it has no reason to accept redirects or source routes. Strict reverse path filtering (1) suits one interface; use loose mode (2) if you run multiple interfaces or tunnels with asymmetric routing.

Which kernel settings should I add?

Append these lines to the same file. They hide kernel internals from unprivileged users and limit debugging features attackers like:

# hide kernel pointers and the kernel log from normal users
kernel.kptr_restrict = 2
kernel.dmesg_restrict = 1

# only a parent process may trace its children
kernel.yama.ptrace_scope = 1

# no core dumps from setuid programs
fs.suid_dumpable = 0

# safer handling of links and files in sticky directories
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
fs.protected_fifos = 2
fs.protected_regular = 2

# restrict BPF to privileged users and harden its JIT
kernel.unprivileged_bpf_disabled = 1
net.core.bpf_jit_harden = 2

# randomise memory layout fully
kernel.randomize_va_space = 2

With dmesg_restrict set, read the kernel log with sudo dmesg or sudo journalctl -k. A ptrace_scope of 1 still lets you debug a program you start with gdb; raise it to 2 only if no one on the server needs debuggers. Once unprivileged_bpf_disabled is set to 1 it cannot be lowered again without a reboot, which is intended.

How do I apply and verify the settings?

Load every file in the configured order and check a few keys:

sudo sysctl --system
sysctl net.ipv4.conf.all.accept_redirects net.ipv4.conf.all.rp_filter kernel.kptr_restrict

The output of --system lists each file and value as it applies them; an unknown key prints an error and is skipped, which usually means a typo or a module that is not loaded. Reboot once at a quiet moment with sudo systemctl reboot, then repeat the check to prove the values persist. Confirm SSH, your websites and IPv6 still work, for example with ip -6 addr and ip -6 route, then a ping -6 -c 3 to any host you know answers over IPv6.

Which settings should I not touch?

Some popular hardening lists break normal VPS features. Leave these as they are unless you know why you need to change them:

  • net.ipv4.ip_forward and net.ipv6.conf.all.forwarding: Docker, WireGuard, OpenVPN and other tunnels need forwarding on. Setting it to 0 silently breaks container networking and VPN routing.
  • net.ipv6.conf.*.accept_ra and autoconf: if your IPv6 address or default route comes from router advertisements, disabling them removes IPv6 connectivity after the next reboot. Check ip -6 route first.
  • net.ipv6.conf.all.disable_ipv6: your plan includes an IPv6 address, and turning it off loses reachability for IPv6 visitors. Firewall it properly instead (see IPv6 on your server).

Swap and memory tuning such as vm.swappiness is a performance topic, covered in the swap file guide.

How do I secure shared memory?

Mount /dev/shm without device files, setuid bits or program execution. It is a world writable memory file system, so blocking execution there removes a convenient place to drop and run tools. Add a line to /etc/fstab:

tmpfs /dev/shm tmpfs defaults,nodev,nosuid,noexec 0 0

Apply it without a reboot and check the options:

sudo mount -o remount /dev/shm
findmnt /dev/shm

The OPTIONS column should list nodev, nosuid and noexec. Older guides mention /run/shm; on current systems /dev/shm is the real mount point. Test your applications afterwards: a few programs, such as some database or browser automation setups, expect to execute from shared memory. If one breaks, drop noexec but keep the other two options.

How can I audit the result?

Run a host audit tool that compares your settings with common benchmarks. Lynis is packaged for Ubuntu and Debian and available from EPEL on Rocky and Alma:

# Ubuntu, Debian
sudo apt install lynis
# Rocky, Alma
sudo dnf install epel-release && sudo dnf install lynis

sudo lynis audit system

Read the kernel hardening section and the suggestions at the end. Treat it as advice: some suggestions do not fit a server that runs containers or a VPN. Keep your drop-in file under version control or in your notes, so you can apply the same settings to the next server you deploy. Pair it with file integrity checks to notice if someone changes it.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-1kernel settings cost no extra resources1 vCPU, 1 GB RAM, 25 GB NVMe$3.60/mo$43.20 for 12 months

Questions about Kernel and sysctl hardening

Do sysctl settings survive a reboot?

Values written with sysctl -w do not. Values in a file under /etc/sysctl.d/ are applied at every boot by systemd-sysctl.

Will these settings break Docker?

Not the ones in this guide. Docker needs IP forwarding, which we deliberately leave alone. Avoid lists that set ip_forward = 0 on container hosts.

Why is my setting overwritten after boot?

Another file with a higher name in /etc/sysctl.d/ or /usr/lib/sysctl.d/ sets the same key later. Run sudo sysctl --system and read which file applies last.

Is noexec on /dev/shm worth it?

It is a small, cheap layer that blocks one common trick. It does not stop a determined attacker, so keep it alongside updates and a firewall.

Do these settings work on FreeBSD?

No. FreeBSD has its own sysctl names and hardening options in /etc/sysctl.conf; this guide covers Linux only.