LLI Wiki — My Digital Knowledge Base

Configuring kernel parameters with sysctl (swappiness example)

Read, change, persist and reload Linux kernel parameters with sysctl, using swappiness and pid_max as examples.

sysctl reads and changes kernel parameters at runtime. This article uses vm.swappiness, which controls how eagerly the kernel swaps memory pages out to swap space, as an example. It shows how to read a value, change it immediately, make it persistent and reload the configuration files.

Read current values

sysctl kernel.ostype
sysctl vm.swappiness
sysctl -a | grep kernel

sysctl -a lists all parameters. Pipe it to more if the output is long.

The same value is also available as a file:

cat /proc/sys/vm/swappiness

Change a value immediately (until reboot)

sudo sysctl vm.swappiness=0

or, equivalently:

sudo sh -c 'echo 0 > /proc/sys/vm/swappiness'

The change is lost at reboot. Use one of the persistent methods below.

Make the value persistent

Option 1: /etc/sysctl.conf

Back up the file first, then add the setting:

sudo cp -p /etc/sysctl.conf /etc/sysctl.conf.$(date +%Y%m%d-%H%M)
sudo sh -c 'echo "vm.swappiness = 0" >> /etc/sysctl.conf'

Drop-in files keep your changes separate from the distribution's main file:

sudo sh -c 'echo "vm.swappiness = 10" > /etc/sysctl.d/99-swappiness.conf'

Reload the configuration without rebooting

Reload /etc/sysctl.conf only:

sudo sysctl -p

Reload all configuration files (the list below is read in this order):

sudo sysctl --system

If the same key is set in several files, the value applied last wins. Avoid defining the same key in more than one place.

Example: maximum number of process IDs

On systems that can run many processes, bash may fail with fork: cannot allocate memory when the PID limit is reached. The limit is kernel.pid_max. The maximum value is 32768 on 32-bit systems and 4194304 (2^22) on 64-bit systems.

Temporary change:

cat /proc/sys/kernel/pid_max
echo 32768 > /proc/sys/kernel/pid_max

Permanent change, in /etc/sysctl.d/98-pid_max.conf:

kernel.pid_max = 4194304

Notes

Mastodon LLIMastodon SocialFosstodon