Find a parameter
2>/dev/null hides the permission denied on key lines; with sudo all
parameters are visible.
sysctl reads and changes kernel parameters; -w lasts until reboot, a file in /etc/sysctl.d/ makes it permanent.
sysctl vm.swappiness
read a parameter
sysctl -n vm.swappiness
the value only
sudo sysctl -w vm.swappiness=10
until reboot
echo 'vm.swappiness = 10' \
| sudo tee /etc/sysctl.d/90-swappiness.conf
permanently
sudo sysctl --system
apply all files
sudo sysctl -p /etc/sysctl.d/90-swappiness.conf
one file
sysctl -a 2>/dev/null | grep forward
find a parameter
Kernel parameters are numbers and strings that change kernel behaviour on
the fly: the eagerness to swap, packet forwarding, file limits. sysctl
reads and changes them without a reboot. A change made with sysctl -w
lasts until reboot; a permanent value goes into a file in /etc/sysctl.d/.
Every parameter is a file in /proc/sys (see
proc-filesystem). The parameter name is the file path with
slashes replaced by dots: vm.swappiness is /proc/sys/vm/swappiness.
The first part of the name is the subsystem: vm — memory, net —
network, fs — files, kernel — the kernel itself. For the student,
sysctl -a shows about 900 parameters; some of them cannot be read, and
permission denied on key goes to standard error.
The -w key (sudo sysctl -w net.ipv4.ip_forward=1) does the same and can
be omitted. There must be no spaces around = on the command line (see
troubleshooting). The value lasts until reboot.
At boot the values come from *.conf files in three directories:
| Directory | Whose files |
|---|---|
/etc/sysctl.d/ |
the administrator's — put yours here |
/run/sysctl.d/ |
temporary, until reboot (absent on the training machine) |
/usr/lib/sysctl.d/ |
packages': 10-apparmor.conf, 50-pid-max.conf |
Files from all directories are sorted by name and applied in order: a
parameter from a later file overrides an earlier one. That is why names
start with a number — 90-swappiness.conf is applied after 10-… and
50-…. A file in /etc with the same name as one in /usr/lib replaces
it entirely. /etc/sysctl.conf is applied last; the link 99-sysctl.conf
in /etc/sysctl.d/ points to it.
A file line is name = value; spaces around = are fine here:
sysctl --system applies all files the same way as at boot and prints each
file and each value. Only *.conf files are read: a 99-z.txt or
99-sysctl.conf.bak file in the same directory is skipped silently.
Common parameters:
| Parameter | What for |
|---|---|
vm.swappiness |
eagerness to swap (60 by default) |
net.ipv4.ip_forward |
packet forwarding: router, NAT, containers |
fs.nr_open |
ceiling of the per-process open files limit |
fs.file-max |
open files for the whole system |
fs.inotify.max_user_watches |
inotify watches (editors, tail -f) |
kernel.pid_max |
the largest PID (4194304 on Ubuntu) |
kernel.dmesg_restrict |
1 — dmesg for root only |
kernel.hung_task_timeout_secs |
after how many seconds in D the kernel reports a hung task |
net.core.somaxconn |
connection queue of a listening socket |
The command's own options are in the table below.
| Option | What it does | Example |
|---|---|---|
-w param=value |
Change a parameter now; the change lasts until reboot. | sudo sysctl -w net.ipv4.ip_forward=1 |
-a |
List every kernel parameter — about 900 on the training machine, usually piped through grep. | sysctl -a 2>/dev/null | grep forward |
--system |
Apply all sysctl.d files and sysctl.conf in the correct order. | sudo sysctl --system |
-p FILE |
Apply parameters from a specific file (defaults to /etc/sysctl.conf). | sudo sysctl -p /etc/sysctl.d/99-forward.conf |
-n |
Print only the value, without the parameter name — convenient in scripts. | sysctl -n vm.swappiness |
Nothing found. Try a shorter query.
2>/dev/null hides the permission denied on key lines; with sudo all
parameters are visible.
If sysctl --system gives the right value, boot will give the same: the
systemd-sysctl service reads the same files in the same order.
sysctl --system first prints all the * Applying … lines in order, then
all the values. systemd-analyze shows more clearly which file set a
parameter last: the files in the order they apply, each after a
# /path line:
A value from a file lower in this list overrides the ones above it.
Remove your file from /etc/sysctl.d/ and set the value by hand
(sudo sysctl -w vm.swappiness=60) or reboot — sysctl --system does not
restore old values, it only applies files.
sysctl -w and writes to /proc/sys last until reboot. You need a
*.conf file in /etc/sysctl.d/ (section "Permanently" above).
Without sudo, sysctl prints the line with the new value and exits with 0,
but does not change the value. The error is on standard error. Repeat with
sudo and check by reading.
.conf — the file is skipped silently.grep -r <parameter> /etc/sysctl.d/ /usr/lib/sysctl.d/ /etc/sysctl.conf.sysctl --system skips it without a message
and with exit code 0. sudo sysctl -p <file> shows
cannot stat /proc/sys/…: No such file or directory and returns 1.The answer is sysctl: cannot stat /proc/sys/net/ipv4/no_such: No such file
or directory.
There is no such parameter: a typo, or the parameter appears only with a
kernel module (for example, net.netfilter.* after nf_conntrack is
loaded, see kernel-modules).
On the command line name=value has no spaces. In sysctl.d files spaces
around = are fine.
--dry-run should only show what would be applied. In procps 4.0.4 the
order of keys matters: sudo sysctl --dry-run --system changes nothing,
while sudo sysctl --system --dry-run applies the files for real. Checked
on the training machine: after the second command vm.swappiness went from
60 to 33. To try a file, use sudo sysctl --dry-run -p <file>.
/proc/sys and the rest of /proc.vm.swappiness and swap.fs.nr_open and process limits.Found an inaccuracy? Report an error in the article