Linux Commands

Sysctl Linux Command

The sysctl is a helpful tool for any Linux administrator. It allows the reading and writing of sysctl variables. With it, you can modify the kernel parameters of your system at runtime. The Linux kernel is the operating system’s core, controlling different system services. The kernel parameters can be set at three instances, during the building of the kernel, at system boot, and at runtime.
The /proc/sys/ contains the kernel parameters. We will see how to use the sysctl command to modify the Linux kernel parameters

Using the sysctl Linux Command

The sysctl uses the files in the /proc/sys directory to modify kernel parameters. You can list the contents of the directory to see the different folders.

sysctl: Display Kernel Parameters

Use the “-a” or “-all” flag to view all the configured kernel parameters.

$ sysctl -a

 
All the configurations will display in a long list showing the parameters and their values in each line.

The previous list can be challenging to understand, but there is a way to narrow it down by checking the values of single parameters. You can pass the parameter’s name to the command and get its specific value. For instance, you can use the following commands to get the kernel hostname and swappiness, which defines how often the system uses the swap space.

$ sysctl kernel.hostname
$ sysctl vm.swappiness

 

The same output can be obtained by retrieving the contents of the file containing it. You only need to replace the “slash” with a “dot”.

For instance, use the following commands to get the same values previously shown:

Alternately, you can filter the output by grep-specific kernel parameters by providing matching words. For example, to filter all ipv4 output, you can use the following command:

sysctl: Modify Kernel Parameters

As an administrator, the sysctl allows you to permanently or temporarily modify the kernel parameters.

The syntax for temporarily modifying kernel parameters is:

$ sysctl -w [parameter=value]

 
Note that if the value contains special characters or spaces, you should enclose it in double quotes. Furthermore, the set parameters reset to the initial values after the next reboot.

Let’s take an example of the TCP Fast Open, which speeds up the loading of TCP connections between two devices. By default, it is enabled. To disable it, use the following command. You should have administrator privileges for it to work. Also, ensure no spaces are between the parameter and the value.

$ sudo sysctl -w net.ipv4.tcp_fastopen=0

 

We see that the values modify from “1” for enabled to “0” for disabled.

If you were to set the same parameters permanently, you need to modify the parameters either in the /etc/sysctl.conf or in the /etc/sysctl.d/99-custom.conf directory. You can open the files using an editor or directly add the configuration using echo.

$ echo 0 > /proc/sys/net/ipv4/tcp_fastopen=0

 
Executing the previous command will modify the parameters permanently.

You can also add the net.ipv4.tcp_fastopen in the configuration file.

$ sudo nano /etc/sysctl.d/99-systemctl.conf

 
Adding the parameter and its value will get loaded every time the system boots.

The system default loads the configurations in the /etc/sysctl.conf file. However, you can use the “-p” option to load another configuration file like the one we previously modified.

$ sysctl -p /etc/sysctl.d/99-systemctl.conf

 
Proceed with caution when making the permanent kernel changes to avoid rendering your kernel unstable. That said, any time you need to modify a parameter, use the syntax highlighted in the article or directly modify it from the configuration file.

Conclusion

The Linux kernel powers the Linux operating system. If you are a Linux system administrator, modifying the kernel parameters to suit various tasks is part of your job. Luckily, this post covers how you can achieve that using the sysctl Linux command.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.