Operating System CentOS

Harden the Linux Kernel with sysctl on AlmaLinux 9 and Rocky Linux 9

Put a documented set of kernel and network hardening settings in an /etc/sysctl.d drop-in on AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9, apply it and verify it.

This guide gives you a single drop-in file in /etc/sysctl.d/ that tightens kernel and network behaviour on AlmaLinux 9, Rocky Linux 9 and CentOS Stream 9. Every setting is explained, so you can drop the ones that do not fit your server instead of pasting a list you cannot audit.

Older tutorials on this topic were written for CentOS 7, which reached end of life in June 2024 and no longer receives security fixes. The RHEL 9 family replaces it. The sysctl command works the same way, but settings now belong in drop-in files rather than in /etc/sysctl.conf.

Requirements

  • A server running AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9.
  • A user with sudo rights (the examples use deploy).
  • Access to your provider's web console, in case a network setting cuts off SSH.

How sysctl settings are loaded

Kernel parameters live under /proc/sys. A change made with sysctl -w takes effect immediately and is lost at reboot. Persistent settings are read at boot from *.conf files in these directories:

  • /usr/lib/sysctl.d/ for defaults shipped by packages. Do not edit these.
  • /run/sysctl.d/ for runtime settings.
  • /etc/sysctl.d/ for your own settings.

All files are sorted by file name regardless of directory, and when two files set the same key the one that sorts last wins. The convention is a two-digit prefix, with 60 to 90 for files in /etc, so 90-hardening.conf overrides the vendor defaults. When you run sysctl --system, /etc/sysctl.conf is read after everything else, so make sure it does not contain older values for the same keys.

Step 1: Look at the current values

List everything, or read one key:

sudo sysctl -a | less
sysctl kernel.kptr_restrict
sysctl net.ipv4.conf.all.rp_filter

See which files already set values on your system:

ls /usr/lib/sysctl.d/ /etc/sysctl.d/
grep -v '^#' /etc/sysctl.conf

Several of the settings below are already the kernel or distribution default. Writing them down anyway documents your intent and protects you if a default changes.

Step 2: Create the hardening drop-in

Create the file:

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

Add the following content:

# --- Kernel information leaks and debugging ---
kernel.dmesg_restrict = 1
kernel.kptr_restrict = 2
kernel.yama.ptrace_scope = 1
kernel.sysrq = 0
kernel.randomize_va_space = 2
kernel.unprivileged_bpf_disabled = 1

# --- Core dumps and link/FIFO protections ---
fs.suid_dumpable = 0
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
fs.protected_fifos = 2
fs.protected_regular = 2

# --- IPv4 network ---
net.ipv4.tcp_syncookies = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.default.secure_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.log_martians = 1
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1

# --- IPv6 network ---
net.ipv6.conf.all.accept_redirects = 0
net.ipv6.conf.default.accept_redirects = 0
net.ipv6.conf.all.accept_source_route = 0
net.ipv6.conf.default.accept_source_route = 0

What the kernel settings do

SettingEffect
kernel.dmesg_restrict = 1Only users with CAP_SYSLOG can read the kernel log, which often contains addresses and hardware details.
kernel.kptr_restrict = 2Kernel pointers printed with %pK are replaced with zeros for everyone. With 1 they are hidden only from users without CAP_SYSLOG.
kernel.yama.ptrace_scope = 1A process may only attach with ptrace to its own descendants, not to every process of the same user. 2 limits ptrace to administrators; 3 disables it and cannot be changed back without a reboot.
kernel.sysrq = 0Disables the magic SysRq key combinations on the console. Root can still write to /proc/sysrq-trigger.
kernel.randomize_va_space = 2Full address space layout randomisation: stack, mmap base, VDSO and heap.
kernel.unprivileged_bpf_disabled = 1Unprivileged users cannot call bpf(). Once set to 1 it cannot be cleared until reboot; use 2 if you want an administrator to be able to change it later.
fs.suid_dumpable = 0Processes that changed privilege level (setuid programs, for example) do not write core dumps, which could expose secrets held in memory.
fs.protected_hardlinks = 1Users cannot create hard links to files they do not own or cannot read and write.
fs.protected_symlinks = 1Symlinks in sticky world-writable directories such as /tmp are only followed when the owners match, which blocks a classic class of /tmp race attacks.
fs.protected_fifos and fs.protected_regular = 2Stops a process from opening, with O_CREAT, a FIFO or regular file it does not own in world-writable or group-writable sticky directories.

What the network settings do

SettingEffect
tcp_syncookies = 1Sends SYN cookies when a socket's SYN backlog overflows, so a SYN flood does not block new connections.
rp_filter = 1Strict reverse-path check: a packet is dropped if the reply would not leave through the interface it arrived on. The kernel uses the higher of the all and per-interface values.
accept_source_route = 0Rejects packets that carry a source-routing option, which lets the sender dictate the path.
accept_redirects = 0, secure_redirects = 0Ignores ICMP redirects, including those from listed gateways. A forged redirect can reroute your traffic.
send_redirects = 0The server does not send ICMP redirects. Only routers need to.
log_martians = 1Logs packets with impossible source addresses to the kernel log.
icmp_echo_ignore_broadcasts = 1Ignores pings sent to broadcast or multicast addresses (Smurf amplification).
icmp_ignore_bogus_error_responses = 1Suppresses log noise from routers that answer broadcast frames with invalid ICMP errors.

Settings to adjust for your server

  • Multi-homed servers, VPN gateways, asymmetric routing: strict reverse-path filtering drops legitimate traffic. Use rp_filter = 2 (loose mode) instead.
  • Routers, VPN servers and container hosts: these need net.ipv4.ip_forward = 1. On a plain web or database server the default of 0 is correct, and you can add net.ipv4.ip_forward = 0 to the file to pin it. Do not do that on a host running containers.
  • IPv6 router advertisements: net.ipv6.conf.all.accept_ra = 0 is often recommended, but if your server gets its IPv6 address or default route from router advertisements it will lose IPv6 connectivity. Only set it when IPv6 is configured statically.
  • Debuggers and profilers: ptrace_scope = 1 stops gdb -p and strace -p on processes you did not start from the same shell unless you run them with sudo.

Step 3: Apply the settings

Load every configuration file in the same order the system uses at boot:

sudo sysctl --system

The command prints each file as it applies it and reports keys it could not set. To load only your file, use:

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

Step 4: Verify

Read a few values back:

sysctl kernel.dmesg_restrict kernel.kptr_restrict kernel.yama.ptrace_scope
sysctl net.ipv4.conf.all.accept_redirects net.ipv4.conf.all.rp_filter

The default keys only apply to interfaces created later, and for redirects the kernel combines the all value with the per-interface value. Check your real interface as well (replace eth0 with the name shown by ip -br link):

sysctl net.ipv4.conf.eth0.accept_redirects net.ipv4.conf.eth0.send_redirects

If an interface still shows 1, add an explicit line for it to the drop-in, for example net.ipv4.conf.eth0.accept_redirects = 0, and run sudo sysctl --system again. Then confirm as an unprivileged user that the kernel log is closed:

dmesg | tail

You should get a permission error. Finally, open a second SSH session to confirm the network settings did not lock you out, and reboot once at a convenient time to check that the values persist.

What about performance tuning?

Older guides, including the previous version of this one, mixed hardening with large fixed values for fs.file-max, socket buffers (net.core.rmem_max, net.core.wmem_max), net.core.somaxconn and vm.swappiness. Those are tuning knobs, not security settings, and the right numbers depend on your RAM and workload. Current kernels size most of them automatically. Leave them at their defaults unless you have measured a specific bottleneck, and if you do change them, keep them in a separate file such as /etc/sysctl.d/80-tuning.conf so the two concerns stay apart.

Frequently asked questions

Do I need to reboot after changing sysctl settings?

No. sysctl --system applies them to the running kernel. The exceptions are one-way switches such as kernel.unprivileged_bpf_disabled = 1 and kernel.yama.ptrace_scope = 3, which can only be undone by removing the line and rebooting.

Can I still use /etc/sysctl.conf?

On the RHEL 9 family sysctl --system still reads it, after all the drop-in files. Drop-ins are easier to manage because each file has one purpose and can be removed without editing anything else. The full rules are in the sysctl.d manual.

A value I set keeps changing back. Why?

Something else sets the same key later. Search /etc/sysctl.conf and every sysctl.d directory for the key name with grep -r, and check whether a tuning service such as tuned is active with tuned-adm active, because tuning profiles can set kernel parameters too.

Where is the reference for each parameter?

The kernel documentation covers them by namespace: kernel.* and net.ipv4.* and net.ipv6.*. Check a setting there before copying it from a hardening list.

Share story:

Continue reading other guides on How VPS.