sysctlRead and tune kernel parameters at runtime
sysctl reads and changes kernel parameters while the system runs: network buffers and backlogs, swappiness, file-handle limits, IP forwarding and hundreds more. Changes apply instantly but are lost at reboot unless you put them in /etc/sysctl.d/.
sysctl [-a] [-n] [-w] NAME[=VALUE]... | sysctl -p [FILE] | sysctl --systemYou've inherited a server and want to check a few kernel settings that commonly affect production.
sudoReading is open to everyone; writing a kernel parameter needs root.
Every kernel tunable is exposed as a file under /proc/sys, a virtual filesystem whose reads and writes go straight to variables inside the running kernel. sysctl just translates names: net.ipv4.ip_forward is /proc/sys/net/ipv4/ip_forward. Reading opens the file and prints its contents; writing opens it for write and the kernel's handler validates the value (an invalid one fails with EINVAL) and applies it immediately. Nothing is saved: at boot, systemd-sysctl.service reads /usr/lib/sysctl.d/, /run/sysctl.d/ and /etc/sysctl.d/ (*.conf, ordered by filename, later files win) plus /etc/sysctl.conf, and applies them; sysctl --system does the same by hand. Many net.* settings are per network namespace, so a container can have its own values, while vm.* and kernel.* are global for the host. Some values only affect new objects: changing a socket buffer default doesn't resize existing sockets, and somaxconn caps the backlog only for listen() calls made after the change.
sysctl -w writes into the running kernel through /proc/sys; only files in /etc/sysctl.d survive a reboot.
sysctl turns net.core.somaxconn into the file /proc/sys/net/core/somaxconn. /proc/sys isn't on disk: it's a window into kernel variables.
NAMERead one parameter, e.g. sysctl vm.swappiness-w NAME=VALUEWrite a value now (not persistent)-aList every parameter (about 1,000)-nPrint only the value, handy in scripts-p [FILE]Load settings from FILE (default /etc/sysctl.conf)--systemReload all of /etc/sysctl.d, /run/sysctl.d, /usr/lib/sysctl.d like at boot-NPrint only names (combine with -a to grep for a parameter)--pattern REGEXOnly parameters whose names match, e.g. 'tcp_.*mem'-eIgnore errors about unknown keys (useful for shared config files)sysctl -w net.ipv4.ip_forward=1 on a new router or NAT box, and nothing elseIt works until the next reboot, then forwarding silently stops.
Also write it to /etc/sysctl.d/99-forward.conf and test with sudo sysctl --systemCopying a big 'kernel tuning' list from an old blog postSome keys no longer exist (tcp_tw_recycle), some defaults are already better now, and wrong values can hurt (huge buffers, swappiness 0 on small boxes).
Change one setting at a time, for a measured problem, and record why in the .conf fileRaising net.core.somaxconn and expecting the accept queue to growThe effective backlog is the smaller of somaxconn and what the app passes to listen(), applied when it calls listen().
Raise both, then restart or reload the applicationPutting settings in /etc/sysctl.conf and a 99- file sets the same keyFiles are applied in a fixed order and the last write wins, so your value may be overridden.
sysctl --system and read the output order; grep -r KEY /etc/sysctl.d /usr/lib/sysctl.dTry these on a Linux machine of your own. Each task has a hint and a solution.
Print only the value of vm.swappiness, with no name in front.
Find every parameter whose name contains 'somaxconn' or 'syn_backlog'.
Set vm.swappiness to 10 now and make it permanent, then prove the file is applied.
sysctlRead and tune kernel parameters at runtime
sysctl reads and changes kernel parameters while the system runs: network buffers and backlogs, swappiness, file-handle limits, IP forwarding and hundreds more. Changes apply instantly but are lost at reboot unless you put them in /etc/sysctl.d/.
sysctl [-a] [-n] [-w] NAME[=VALUE]... | sysctl -p [FILE] | sysctl --systemYou've inherited a server and want to check a few kernel settings that commonly affect production.
sudoReading is open to everyone; writing a kernel parameter needs root.
Every kernel tunable is exposed as a file under /proc/sys, a virtual filesystem whose reads and writes go straight to variables inside the running kernel. sysctl just translates names: net.ipv4.ip_forward is /proc/sys/net/ipv4/ip_forward. Reading opens the file and prints its contents; writing opens it for write and the kernel's handler validates the value (an invalid one fails with EINVAL) and applies it immediately. Nothing is saved: at boot, systemd-sysctl.service reads /usr/lib/sysctl.d/, /run/sysctl.d/ and /etc/sysctl.d/ (*.conf, ordered by filename, later files win) plus /etc/sysctl.conf, and applies them; sysctl --system does the same by hand. Many net.* settings are per network namespace, so a container can have its own values, while vm.* and kernel.* are global for the host. Some values only affect new objects: changing a socket buffer default doesn't resize existing sockets, and somaxconn caps the backlog only for listen() calls made after the change.
sysctl -w writes into the running kernel through /proc/sys; only files in /etc/sysctl.d survive a reboot.
sysctl turns net.core.somaxconn into the file /proc/sys/net/core/somaxconn. /proc/sys isn't on disk: it's a window into kernel variables.
NAMERead one parameter, e.g. sysctl vm.swappiness-w NAME=VALUEWrite a value now (not persistent)-aList every parameter (about 1,000)-nPrint only the value, handy in scripts-p [FILE]Load settings from FILE (default /etc/sysctl.conf)--systemReload all of /etc/sysctl.d, /run/sysctl.d, /usr/lib/sysctl.d like at boot-NPrint only names (combine with -a to grep for a parameter)--pattern REGEXOnly parameters whose names match, e.g. 'tcp_.*mem'-eIgnore errors about unknown keys (useful for shared config files)sysctl -w net.ipv4.ip_forward=1 on a new router or NAT box, and nothing elseIt works until the next reboot, then forwarding silently stops.
Also write it to /etc/sysctl.d/99-forward.conf and test with sudo sysctl --systemCopying a big 'kernel tuning' list from an old blog postSome keys no longer exist (tcp_tw_recycle), some defaults are already better now, and wrong values can hurt (huge buffers, swappiness 0 on small boxes).
Change one setting at a time, for a measured problem, and record why in the .conf fileRaising net.core.somaxconn and expecting the accept queue to growThe effective backlog is the smaller of somaxconn and what the app passes to listen(), applied when it calls listen().
Raise both, then restart or reload the applicationPutting settings in /etc/sysctl.conf and a 99- file sets the same keyFiles are applied in a fixed order and the last write wins, so your value may be overridden.
sysctl --system and read the output order; grep -r KEY /etc/sysctl.d /usr/lib/sysctl.dTry these on a Linux machine of your own. Each task has a hint and a solution.
Print only the value of vm.swappiness, with no name in front.
Find every parameter whose name contains 'somaxconn' or 'syn_backlog'.
Set vm.swappiness to 10 now and make it permanent, then prove the file is applied.