CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Performance Tuning / sysctl

sysctl

Read and tune kernel parameters at runtime

Intermediatekerneltuningnetworking

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/.

Syntaxsysctl [-a] [-n] [-w] NAME[=VALUE]... | sysctl -p [FILE] | sysctl --system

Watch it run

You've inherited a server and want to check a few kernel settings that commonly affect production.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
sysctl vm.swappiness net.core.somaxconn fs.file-max net.ipv4.ip_local_port_range net.ipv4.ip_forward

Several names in one call. swappiness 60 is the default; somaxconn defaults to 4096 since kernel 5.4; file-max is effectively unlimited on modern kernels; the local port range gives about 28,000 ephemeral ports for outgoing connections.

Anatomy of a command

$
commandsudo

Reading is open to everyone; writing a kernel parameter needs root.

How it works

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.

run8192set variableread *.confapplycaps backlogtee fileYousysctl -wsysctlprocps/proc/sysnet/core/...Kernelsomaxconn=4096/etc/sysctl.d60-web.confsystemd-sysctlat every bootnginxlisten() backlog
1/7
Name to path

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.

← → to step

Options you'll actually use

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)

Common mistakes

sysctl -w net.ipv4.ip_forward=1 on a new router or NAT box, and nothing else

It works until the next reboot, then forwarding silently stops.

Also write it to /etc/sysctl.d/99-forward.conf and test with sudo sysctl --system
Copying a big 'kernel tuning' list from an old blog post

Some 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 file
Raising net.core.somaxconn and expecting the accept queue to grow

The 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 application
Putting settings in /etc/sysctl.conf and a 99- file sets the same key

Files 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.d

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Print only the value of vm.swappiness, with no name in front.

  2. 2

    Find every parameter whose name contains 'somaxconn' or 'syn_backlog'.

  3. 3

    Set vm.swappiness to 10 now and make it permanent, then prove the file is applied.

Related commands

ulimitShow and set per-process resource limitsdmesgRead the kernel's message logfreeShow used, free and available memoryssSee listening ports and open connections
PreviouspidstatNextulimit
Linux / Performance Tuning / sysctl

sysctl

Read and tune kernel parameters at runtime

Intermediatekerneltuningnetworking

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/.

Syntaxsysctl [-a] [-n] [-w] NAME[=VALUE]... | sysctl -p [FILE] | sysctl --system

Watch it run

You've inherited a server and want to check a few kernel settings that commonly affect production.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
sysctl vm.swappiness net.core.somaxconn fs.file-max net.ipv4.ip_local_port_range net.ipv4.ip_forward

Several names in one call. swappiness 60 is the default; somaxconn defaults to 4096 since kernel 5.4; file-max is effectively unlimited on modern kernels; the local port range gives about 28,000 ephemeral ports for outgoing connections.

Anatomy of a command

$
commandsudo

Reading is open to everyone; writing a kernel parameter needs root.

How it works

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.

run8192set variableread *.confapplycaps backlogtee fileYousysctl -wsysctlprocps/proc/sysnet/core/...Kernelsomaxconn=4096/etc/sysctl.d60-web.confsystemd-sysctlat every bootnginxlisten() backlog
1/7
Name to path

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.

← → to step

Options you'll actually use

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)

Common mistakes

sysctl -w net.ipv4.ip_forward=1 on a new router or NAT box, and nothing else

It works until the next reboot, then forwarding silently stops.

Also write it to /etc/sysctl.d/99-forward.conf and test with sudo sysctl --system
Copying a big 'kernel tuning' list from an old blog post

Some 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 file
Raising net.core.somaxconn and expecting the accept queue to grow

The 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 application
Putting settings in /etc/sysctl.conf and a 99- file sets the same key

Files 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.d

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Print only the value of vm.swappiness, with no name in front.

  2. 2

    Find every parameter whose name contains 'somaxconn' or 'syn_backlog'.

  3. 3

    Set vm.swappiness to 10 now and make it permanent, then prove the file is applied.

Related commands

ulimitShow and set per-process resource limitsdmesgRead the kernel's message logfreeShow used, free and available memoryssSee listening ports and open connections
PreviouspidstatNextulimit