Linux kernel hardening with sysctl: the settings that matter on a server
The Linux kernel has runtime settings that limit what an unprivileged user or process can learn about the
kernel or do with it. They matter once someone has a foothold on the server: a web application bug that runs
commands as www-data, a compromised container, a leaked user account. Most distributions set some of them.
Five are worth checking, and one file in /etc/sysctl.d sets them for good.
Read the current values
sysctl kernel.kptr_restrict kernel.dmesg_restrict kernel.unprivileged_bpf_disabled \
fs.protected_symlinks fs.protected_hardlinks
Containers share the kernel of the host, so these values apply to every container on a Docker host too.
The five settings
| Setting | Value to aim for | What it does |
|---|---|---|
kernel.kptr_restrict |
1 |
Kernel addresses printed through %pK (for example in /proc/kallsyms and some /proc files) show as zeros to users without CAP_SYSLOG. 2 hides them from everyone. |
kernel.dmesg_restrict |
1 |
Only users with CAP_SYSLOG may read the kernel log with dmesg. The log can contain addresses and hardware details useful for an exploit. |
kernel.unprivileged_bpf_disabled |
1 or 2 |
Unprivileged users cannot call bpf(). Kernel privilege escalations such as CVE-2021-3490 went through unprivileged BPF. 1 cannot be undone until a reboot; 2 can be changed back by root. |
fs.protected_symlinks |
1 |
In world-writable sticky directories such as /tmp, a symlink is only followed when the follower or the directory owner owns it. This blocks the classic /tmp symlink attacks on root processes. |
fs.protected_hardlinks |
1 |
Users can only create hard links to files they own or can read and write. This blocks tricks with links to setuid programs or files of other users. |
Defaults per distribution
The values come from two places: build options of the distribution kernel, and files in /usr/lib/sysctl.d
or /etc/sysctl.d that packages install. This table lists what we found in November 2026, from the kernel
configuration of each distribution kernel and the sysctl files of its packages:
| Release | kptr_restrict |
dmesg_restrict |
unprivileged_bpf_disabled |
protected_symlinks, protected_hardlinks |
|---|---|---|---|---|
| Debian 12 | 0 (not set) | 1 (kernel option) | 2 (kernel option) | 1 (99-protect-links.conf, package procps) |
| Debian 13 | 0 (not set) | 1 (kernel option) | 2 (kernel option) | 1 (50-default.conf, package linux-sysctl-defaults) |
| Ubuntu 24.04 | 1 (/etc/sysctl.d/10-kernel-hardening.conf) |
1 (kernel option) | 2 (kernel option) | 1 (99-protect-links.conf, package procps) |
| Rocky Linux 9 | 1 (50-redhat.conf) |
0 (kernel option off) | 2 (kernel option) | not set by any file |
| Rocky Linux 10 | 1 (50-redhat.conf) |
1 (kernel option) | 2 (kernel option) | not set by any file |
Alpine 3.24 (linux-virt) |
0 (not set) | 1 (kernel option) | 1 (00-alpine.conf) |
1 (00-alpine.conf) |
"Kernel option" means CONFIG_SECURITY_DMESG_RESTRICT=y or CONFIG_BPF_UNPRIV_DEFAULT_OFF=y in the kernel
build; a custom kernel or a kernel of the hosting provider can differ. On a Debian 13 host with the stock
kernel, sysctl showed the values of the table. Where the table says "not set", the kernel default applies;
read the running value with sysctl before you rely on it.
Set them for good
Write the values to a file in /etc/sysctl.d/:
sudo tee /etc/sysctl.d/90-hardening.conf >/dev/null <<'EOF'
kernel.kptr_restrict = 1
kernel.dmesg_restrict = 1
kernel.unprivileged_bpf_disabled = 1
fs.protected_symlinks = 1
fs.protected_hardlinks = 1
EOF
sudo sysctl --system
sysctl --system loads all configuration files and prints each file and value as it goes. On systemd
systems, the files of /etc/sysctl.d, /run/sysctl.d and /usr/lib/sysctl.d are sorted by file name across
all directories, and for a setting that appears in several files the file with the latest name wins. A file
named 99-something.conf that sets the same key overrides 90-hardening.conf. To see every file in the
order it applies:
systemd-analyze cat-config sysctl.d
On Alpine, the OpenRC service sysctl applies the files of /usr/lib/sysctl.d and /etc/sysctl.d at boot
(rc-update show boot should list it). Apply the new file at once with
sysctl -p /etc/sysctl.d/90-hardening.conf.
Check that they work
As a normal user:
dmesg | head -1
With dmesg_restrict = 1, this prints dmesg: read kernel buffer failed: Operation not permitted.
head -2 /proc/kallsyms
With kptr_restrict = 1, the addresses are all zeros for a normal user.
Settings that are optional or need care
fs.protected_fifos = 1andfs.protected_regular = 2extend the/tmpprotection to FIFOs and regular files. Debian and Ubuntu set them in the same files as the symlink setting; the Rocky Linux files do not.kernel.yama.ptrace_scope = 1lets a process attach a debugger only to its own children (Ubuntu sets it in/etc/sysctl.d/10-ptrace.conf; Rocky Linux sets 0). It stops one compromised process of a user from reading the memory of another process of the same user. Attaching to a running process (gdb -p,strace -p) then needs root.fs.suid_dumpable = 0keeps setuid programs from writing core dumps. It is the kernel default.- Network settings such as
net.ipv4.conf.all.accept_redirects = 0andnet.ipv4.conf.all.send_redirects = 0fit a server that is not a router. Leavenet.ipv4.ip_forwardas it is on Docker hosts: Docker needs it for container networking, and turning it off cuts the containers off. kernel.unprivileged_userns_cloneis a Debian and Ubuntu kernel setting. Turning unprivileged user namespaces off closes a path that kernel exploits have used, and it breaks rootless containers and browser sandboxes. Leave it on unless you know nothing on the server needs it.
AppArmor and SELinux
The sysctl settings limit what can be learned about the kernel. Mandatory access control limits what each service can do. Debian and Ubuntu use AppArmor, RHEL, Rocky and AlmaLinux use SELinux in enforcing mode, both on by default. If either is off on your server, the explainer for that check shows how to turn it back on.
Sources
- Linux kernel documentation: /proc/sys/kernel —
kptr_restrict,dmesg_restrict,unprivileged_bpf_disabled,perf_event_paranoid - Linux kernel documentation: /proc/sys/fs —
protected_symlinks,protected_hardlinks,protected_fifos,protected_regular,suid_dumpable - Linux kernel documentation: Yama —
ptrace_scope - sysctl.d(5) — file order and precedence
- Debian wiki: AppArmor
Check your servers every day
The agent of Patchbrief reports the packages and settings of a server once a day; the digest names what to fix first.