As a Linux system administrator, sometimes you may need to modify the default kernel’s behavior. For example, you may need to enable the magic SysRq key or to increase the number of connections that Kernel will accept. The kernel parameters can be set when building the kernel, on system boot, or at runtime.

This article explains how to use the sysctl command to view and modify kernel parameters at runtime.

Using sysctl to View the Kernel Parameters

To view all current kernel parameters invoke the sysctl command with the -a option:

sysctl -a

This will output a large list that looks something like the following where each line includes the name of the parameter and its value:

abi.vsyscall32 = 1
debug.exception-trace = 1
debug.kprobes-optimization = 1
...

All users can view the current kernel parameters; only the root user can modify their values.

You can check the value of a single parameter by passing its name as an argument to sysctl. For example, to check the current swappiness value you would type:

sysctl vm.swappiness
vm.swappiness = 60

Swappiness is a Linux kernel property that defines how often the system will use the swap space.

The sysctl command reads the information from the /proc/sys directory. /proc/sys is a virtual directory that contains file objects that can be used to view and set the current kernel parameters.

You can also view a parameter value by displaying the content of the appropriate file. The only difference is how the file is represented. For example, both sysctl vm.swappiness and cat /proc/sys/vm/swappiness will give the same output. When using sysctl the directory slashes are replaced with dots and the proc.sys part is assumed.

Using sysctl to Modify the Kernel Parameters

To set a kernel parameter at runtime run the sysctl command followed by the parameter name and value in the following format:

sysctl -w parameter=value

If the value contains empty space or special characters, enclose the value in double-quotes. You can also pass multiple parameter=value pairs in the same command.

Be extra careful when changing kernel settings on a production system as it may make the kernel unstable, and you’ll need to reboot the system.

For example, to enable IPv4 packet forwarding you would run:

sysctl -w net.ipv4.ip_forward=1

The change takes effect immediately, but it is not persistent. After a system reboot, the default value is loaded.

To set a parameter permanently, you’ll need to write the settings to /etc/sysctl.conf or another configuration file in the /etc/sysctl.d directory:

sysctl -w net.ipv4.ip_forward=1 >> /etc/sysctl.conf

Another way to change parameters is to use the echo command to write the settings to the files in the /proc/sys directory. For example, instead of running the command above, you can use:

echo 1 > /proc/sys/net/ipv4/ip_forward

The -p option allows you to load the settings from a configuration file:

sysctl -p /etc/sysctl.d/file_name.conf

When no file is given, sysctl reads the /etc/sysctl.conf file.

Conclusion

The sysctl command allows you to view and change Linux kernel parameters.

Feel free to leave a comment if you have any questions.