How to Modify Kernel Variables Using sysctl Command on Linux

How to Modify Kernel Variables Using sysctl Command on Linux

Looking to understand how to modify kernel variables using sysctl command on Linux OS? Here’s everything you need to know about it. 

In Linux, you can alter kernel behavior using several parameters called tunable. This is applicable both at boot time or in real-time while the operating system is running. For this purpose, the sysctl command line utility can be used. This article will discuss how we can modify kernel variables using the sysctl command. 

The sysctl is, by default, installed on the Linux operating system. This utility is the command to all the distributions of Linux. However, slight differences may be observed. We will start with a brief discussion on sysctl and its internal working details. Then, we will discuss various commands of sysctl to manipulate configuration variables.

Before moving towards the discussion, we must mention that besides the sysctl command, you can also use ipcs to list down the kernel’s settings. The following command illustrates it:

#ipcs -l

Introduction to sysctl

Sysctl is a command line utility that can modify the kernel behavior by specifying various settings. The kernel tunables can be found by investigating the /proc/sys/. Other files are also involved, such as /etc/sysctl.d/. We will discuss the details of the sysctl via first briefly talking about what kernel parameters are. Then we will also discuss procfs.

Kernel parameters

Kernel parameters are the system’s settings that can be tuned via different commands or using sysctl. The changes are immediately applied while the kernel is running. One doesn’t need to recompile or even reboot the system. The kernel parameters can be changed via the following:

  • Sysctl command
  • The virtual file system at /proc/sys/
  • The configuration files in the directory /etc/sysctl.d/

The procfs

The sysctl is powered by procfs (proc file system). It is a pseudo file system available for Linux and other Unix-like operating systems. It provides an interface to the kernel data structure. It can be used to inquire about processes and additional system information. 

Sysctl classes

There are several classes in which the tunable parameters of sysctl are categorized. This includes parameters for user domains and personalities, cryptography, debugging, devices, file system, kernel, network, remote procedure calls, user namespaces, memory buffers, and cache. More details about these classes can be seen here.

Sysctl commands

Now, we will briefly provide some examples of sysctl commands. To run the commands below, you should have access to the root user or a user with sudo privileges.

List all kernel parameters

To list all kernel parameters, you can run the sysctl command with –a or --all flags. The following command illustrates it:

list all kernel parameters

You will see the variables in the following format:

<tunable class>.<tunable> = <value>


For instance, you will see the OS type as:

kernel.ostype = Linux


Similarly, you will see the value of kernel.shmmax as follows:

kernel.shmmax = 68719476736

You can also see the value of these variables by using the cat command as follows:

# cat /proc/sys/kernel/shmmax

Include deprecated variable names in the list

You should include the deprecated variables as well. For this case, you can use the – deprecated flag as follows:

# sudo sysctl -a --deprecated


List only names of kernel variables

The names of kernel variables can be obtained via the following command:

# sudo sysctl -a –N

Finding a specific kernel variable

To find a specific kernel variable, you can use the grep to filter out a specific variable. The following command finds the variable associated with memory as follows:

# sudo sysctl -a | grep memory


Update kernel variable with new value

You can write to a kernel variable temporarily as follows:

# sudo sysctl net.core.netdev_max_backlog=1200


Here, we have updated the maximum size of the receive queue for the ring protocol’s network interface card (NIC). 

Note: The change is applied immediately without restarting the kernel. However, the settings will return to their default after the kernel restart.

You can use the following echo command to change the kernel value as well:

# echo <TARGET_VALUE> > /proc/sys/<TUNABLE_CLASS>/<PARAMETER>


And, then check if the value has been updated with the following command:

# cat /proc/sys/<TUNABLE_CLASS>/<PARAMETER>


Modify Kernel Variables Using sysctl Command

To update kernel variables permanently, you can use the following command:

# sudo sysctl -w net.core.netdev_max_backlog=1200 >> /etc/sysctl.conf

Here, we have used the –w flag and redirected the output to systctl.conf, the default configuration file. Alternatively, you can create your configuration file using vim to modify kernel variables using sysctl command as follows:

# sudo vim /etc/sysctl.d/10-test-settings.conf


Then, you can write the kernel variable value as follows:

net.core.netdev_max_backlog = 1200
user.max_net_namespaces = 63067
vm.overcommit_memory = 0


Now, close the file and save it. Now, you should load the changes with the help of the following command:

# sudo sysctl -p /etc/sysctl.d/10-test-settings.conf

Alternatively, you can try the following command:

# sudo sysctl --load= /etc/sysctl.d/10-test-settings.conf


Reload the sysctl variables from configuration files

To load the sysctl variables from configuration files without rebooting, you can use the following command:

# sudo sysctl  --system


This will load the kernel parameters from the following files (the files are listed according to the order of loading):

/run/sysctl.d/*.conf
/etc/sysctl.d/*.conf
/usr/local/lib/sysctl.d/*.conf
/usr/lib/sysctl.d/*.conf
/lib/sysctl.d/*.conf
/etc/sysctl.conf


Loading the variables from custom configuration files

You can load the variables from your custom configuration files with the help of the following commands:

#sudo sysctl -p/etc/sysctl.d/10-test-settings.conf


Alternatively, you can also try the following command:

# sudo sysctl --load= /etc/sysctl.d/10-test-settings.conf


Reload variables based on a specific pattern

You can load the variables based on a specific pattern using the following command:

#sudo sysctl --system --pattern '^net.ipv6'
# sudo sysctl --system -r memory

In this article, we have discussed what the kernel parameters are. These are various parameters through which the kernel’s behavior can be modified. We have discussed how we can modify kernel variables using sysctl command. The sysctl command is discussed in detail. Then, various commands of sysctl have been discussed.

If this guide helped you, please share it.

Related Posts