Read and Safely Change Linux Kernel Settings in /proc/sys
You want to tune a kernel setting without a reboot, so you turn to /proc/sys, where every tunable is a plain file you can read or write. Allow about fifteen minutes. You need a shell and a mounted proc filesystem; reading needs no special privilege, but writing usually needs root and can affect networking, memory management, logging or other running services.
The route
Jump straight to the step you need, or tick off Done means at the end.
This follows proc_sys(5) from Linux man-pages 6.7, checked against procps-ng 4.0.4 on Linux 6.8. The examples use vm.swappiness, but the right value always depends on the workload and the host's own policy, never on a number copied from somewhere else.
1. Confirm the filesystem and tools
Start with read-only checks. None of these change kernel state:
$ findmnt -no FSTYPE,TARGET /proc
proc /proc
$ command -v sysctl
/usr/sbin/sysctl
$ sysctl --version
sysctl from procps-ng 4.0.4
If findmnt does not show /proc, stop here: a missing proc mount is a system setup problem, not a reason to create a directory and write pretend settings into it. If sysctl is absent, the files still work directly, but install the normal procps tooling through your package manager before you standardise scripts around it.
Checkpoint
You should now know whether you are reading the live /proc filesystem and which sysctl implementation will run.
2. Read a value directly
Each file below /proc/sys represents a kernel variable. Read one with an ordinary command:
$ cat /proc/sys/vm/swappiness
10
$ cat /proc/sys/kernel/hostname
server.example.com
The output is the current value, live, not a copy from a config file. It can change while you are working, and some values are host-specific; do not build a script around the hostname shown above.
The proc_sys(5) interface accepts a few value shapes:
- Strings that may end with a newline or a NUL character.
- Integers and long integers written in decimal or hexadecimal, such as
0x3FFF. - Several values at once for settings that take more than one number, separated by a space, tab or newline. Any other separator produces
EINVAL.
3. Find settings without guessing paths
List the top-level names, then a particular directory, with ordinary read-only commands:
$ find /proc/sys -maxdepth 1 -type d -printf '%f\n' | sort
abi
debug
dev
fs
kernel
net
user
vm
$ find /proc/sys/vm -maxdepth 1 -type f -printf '%f\n' | sort | head
admin_reserve_kbytes
compact_memory
compact_unevictable_allowed
dirty_background_bytes
dirty_background_ratio
The exact list depends on the kernel and its configuration. Keep the search targeted when you already know the subsystem: a broad recursive dump is noisy and can expose settings you did not mean to collect.
For sysctl, replace each slash in the file path with a dot and drop the /proc/sys/ prefix. These two commands read the same variable:
$ cat /proc/sys/vm/swappiness
10
$ sysctl vm.swappiness
vm.swappiness = 10
Use the file form in a small shell script that needs an exact path. Use the dotted form when working with tools or configuration conventions that already speak sysctl names.
4. Record the old value before writing
A runtime write is not automatically persistent, and it is not automatically harmless. Before changing a setting, record the current value and read the relevant subsystem's documentation. For this example, save the live swappiness value without changing it:
$ setting=/proc/sys/vm/swappiness
$ old_value=$(cat "$setting")
$ printf 'old swappiness: %s\n' "$old_value"
old swappiness: 10
This is not a recommendation to raise or lower swappiness. It only demonstrates the mechanics of a reversible write. If you are chasing a real performance problem, capture workload, memory pressure and monitoring data first.
5. Apply one temporary change
Warning
This is the first state-changing step. It needs elevated privileges and can alter how the running kernel behaves, so make sure the value is intentional and use a maintenance window on a service-sensitive host.
$ printf '%s\n' 10 | sudo tee /proc/sys/vm/swappiness >/dev/null
$ cat /proc/sys/vm/swappiness
10
10 is used here because it is the value already observed, so this particular run is a no-op on this host. Replace it only once you have decided on a value for your own system; never write a value copied from an unrelated host.
If the kernel rejects the value, the write fails. Check the shell's exit status immediately:
$ printf '%s\n' 10 | sudo tee /proc/sys/vm/swappiness >/dev/null
$ printf 'write status: %s\n' "$?"
write status: 0
A failed write does not prove the path is wrong. The value may be outside the setting's accepted range, the file may be read-only, or the kernel may reject its format: read the error and re-check the exact variable rather than retrying blindly.
6. Undo the runtime change
The direct write changes the running kernel only. Restore the value you recorded once testing is over:
$ printf '%s\n' "$old_value" | sudo tee /proc/sys/vm/swappiness >/dev/null
$ cat /proc/sys/vm/swappiness
10
Recovery
If the shell holding old_value has gone away, do not guess. Ask the service owner or check the change record for the intended value, then verify it with the same read command. A reboot may restore a distribution default, but that is a disruptive recovery method, not an undo button.
7. Understand persistence and failure boundaries
Writing a proc file normally affects the current boot only. A later boot may load a different value from system configuration, a distribution service, a container runtime or a kernel default. proc_sys(5) describes the live pseudo-filesystem; it does not promise that a runtime write survives a reboot.
Do not make a change persistent until you have identified the owner of the setting and documented a rollback. Persistent sysctl configuration is a separate operational decision, checked afterwards with your distribution's configuration tools, kept apart from the runtime observation.
Treat these as changes that need review, not casual tweaks:
- Network forwarding settings.
- Namespace controls.
- ptrace restrictions.
- Kernel logging controls.
- Memory limits.
Root access only gives you permission to attempt a write; it does not make the value safe.
Done means
- Confirmed the mount:
/procis there and you checked the installedsysctlversion. - Mapped a path, such as
/proc/sys/vm/swappiness, to its dotted namevm.swappiness. - Recorded the old value before attempting a write.
- Verified the new value and checked the write status.
- Restored the original value, or recorded a clear owner and rollback plan.
- Know the limit: a runtime change is not automatically persistent across reboot.