Home / Alt manpages / sysctl(8)

  • sysctl(8)
  • Admin command
  • linux

Tune Kernel Parameters Safely with sysctl

One bad sysctl -w and a server can stop routing traffic before you can blink, so treat every change as reversible until proven otherwise. This guide walks you through inspecting a kernel parameter, making a temporary runtime change, verifying it, and persisting a deliberately chosen setting for future boots. The commands use the installed procps-ng 4.0.4 and systemd 255.4 interfaces described by the local sysctl(8), sysctl.conf(5) and sysctl.d(5) pages.

Allow about 15 minutes for a read-only inspection and one reversible test. Persistent tuning needs more care, because a bad value can affect networking, memory pressure or process isolation. You need a shell, access to /proc/sys, and elevated privileges for writes to most keys and for editing /etc/sysctl.d/.

1. Inspect before changing anything

Start with a parameter whose meaning you actually understand. The name maps to a file below /proc/sys; dots are accepted as separators, and slashes work too. The hostname is a harmless example that is easy to read:

$ sysctl kernel.hostname
kernel.hostname = server.example.com
$ sysctl -n kernel.hostname
server.example.com

The plain form prints both the key and value. Add -n when a script needs only the value, and -N when you need only the name. Do not confuse -a with a focused query: it prints every parameter currently exposed by this kernel, which can be a long and very machine-specific list.

Checkpoint

Save the current value of any key you plan to alter. This gives you a practical undo value even when the setting is not documented in your distribution's defaults.

$ sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 0
$ OLD_FORWARDING=$(sysctl -n net.ipv4.ip_forward)

2. Query a narrow group of parameters

Use --pattern with -a when you need a family of keys. The pattern is an extended regular expression, so anchor it when you mean one exact name:

$ sysctl -a --pattern '^net\.ipv4\.ip_forward$'
net.ipv4.ip_forward = 0

For a broader look, sysctl -a --pattern 'forward' can return several keys. Treat that output as discovery, not a ready-made change list. Kernel versions, loaded modules and network interfaces all change which keys exist.

3. Make one temporary runtime change

Only do this when you actually know what the parameter controls. The command below enables IPv4 forwarding until the next reboot or until you restore it. It is a system-wide networking change and normally needs root:

$ sudo sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1
$ sysctl -n net.ipv4.ip_forward
1

Keep the assignment as one shell word. Quoting is needed when a value contains spaces or shell metacharacters, for example kernel.domainname="example.com". The -w option forces arguments to be treated as writes, which catches a command that was accidentally written as a read.

Warning

Changing a sysctl can alter security or service behaviour immediately. Do not paste a hardening or performance recommendation in without checking its kernel documentation and the workload it affects. A successful write only proves the kernel accepted the value, not that your application is now configured correctly.

Undo the example with the value you recorded earlier:

$ sudo sysctl -w net.ipv4.ip_forward="$OLD_FORWARDING"
net.ipv4.ip_forward = 0
$ sysctl -n net.ipv4.ip_forward
0

4. Persist a setting in sysctl.d

For a setting that should survive reboot, create a local administrator file in /etc/sysctl.d/. Use a numbered name so its ordering is visible. This example persists forwarding, but pick the value for your own host:

$ sudo install -m 0644 /dev/null /etc/sysctl.d/90-local-forwarding.conf
$ sudo sh -c 'printf "%s\n" "net.ipv4.ip_forward = 1" > /etc/sysctl.d/90-local-forwarding.conf'
$ sudo cat /etc/sysctl.d/90-local-forwarding.conf
net.ipv4.ip_forward = 1

Configuration files hold assignments, blank lines and comments starting with # or ;. Values can contain whitespace. A leading single hyphen makes a failed assignment non-fatal, but use that only when failure is genuinely expected: hiding an unknown or mistyped key just makes troubleshooting harder later.

The 90- prefix is not magic, just sortable. Files load lexicographically, so a later filename can override an earlier assignment. Files in /etc/sysctl.d/ take precedence over same-named files in /run/sysctl.d/, /usr/local/lib/sysctl.d/ and /usr/lib/sysctl.d/. A same-named file in /etc replaces the lower-priority file outright; a differently named later file can override individual keys instead.

5. Load and verify the file now

Apply the file without rebooting. The file argument is explicit, so this test will not unexpectedly load every system configuration file:

$ sudo sysctl --load=/etc/sysctl.d/90-local-forwarding.conf
net.ipv4.ip_forward = 1
$ sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 1

At boot, systemd-sysctl.service reads the sysctl.d directories. You can inspect the full ordered set that procps-ng would load with:

$ sudo sysctl --system
* Applying /etc/sysctl.d/90-local-forwarding.conf ...
net.ipv4.ip_forward = 1

Real output includes other files, and may include permission-denied messages for keys this environment cannot write to. Do not treat every one of those lines as evidence your file was ignored. Check the target value separately, then inspect the service status and journal if a boot-time application actually fails.

6. Remove or disable the persistent change

Persistent configuration is state. To undo this guide, remove the assignment from the file or move the file aside, then restore the runtime value explicitly:

$ sudo mv /etc/sysctl.d/90-local-forwarding.conf /etc/sysctl.d/90-local-forwarding.conf.disabled
$ sudo sysctl -w net.ipv4.ip_forward=0
$ sysctl -n net.ipv4.ip_forward
0

The .disabled suffix means systemd-sysctl will not treat the renamed file as configuration. If another file also sets the key, find it before assuming the setting will stay at zero:

$ sudo sysctl --system 2>&1 | grep 'ip_forward'
$ sysctl -a --pattern 'net\.ipv4\.ip_forward'

Some parameters only appear once a kernel module or network interface exists, so a boot-time run can happen before a key is available. The sysctl.d(5) guidance points to udev rules for settings that must apply the moment a module or interface appears, rather than hiding the failure inside a general configuration file.

Done means

  • Original value recorded. You saved it before changing a live key.
  • Runtime value verified. A focused sysctl query confirms the new value.
  • Persistent file named clearly. It lives under /etc/sysctl.d/ with a sortable prefix.
  • File tested explicitly. You loaded it and understand its ordering and precedence.
  • Rollback known. You know how to remove the file and restore the previous runtime value.