Sysctl Command in Linux

FREE Online Courses: Click for Success, Learn for Free - Start Now!

In this article, you will learn all there is to the sysctl command in Linux. We will go through different topics like What sysctl is, the relation between the kernel and the sysctl command, and the syntax and options of the sysctl command.

In the end, we will also be looking at different practical commands in the terminal that help us in viewing and setting kernel parameters and their values.

What is linux sysctl?

Sysctl is a common-line-base tool or utility in Linux-based operating systems that helps in managing the parameters and kernel configuration and other stuff like its version number, maximum limits, and security settings.

The sysctl is extremely helpful for system administrators, because as an admin, you may need to modify the kernel’s default behavior, you may need to increase the number of connection the kernel will accept or enable the magic SysyRq key.

We can tinker or modify the kernel parameters when building the kernel, at run time or at boot time. The sysctl enables us to modify the parameter of the kernel during runtime. The parameters are present or listed under the /proc/sys directory.

Location of the kernel parameters

The kernel parameter are in the /proc/sys directory. Run the command ls -R /proc/sys to see all of the files and subdirectories in in the directory, the option “-R” performs a recursive searc, therefore allowing you to see all of the sub directouies and files within these subdirectories of the /proc/sys directory

location of the kernel parameters

In the above output, notice that we first get the list of the subdirectories of the /proc/sys directory and since we used the option “-R”, we are also getting the list of files withing the subdirectories.

The /proc/sys directory consists of all those files that have the setting of the kernel parameters. Therefore any setting of the kernel parameter that needs to be changed, can be found in this directory.

Options used with Linux sysctl command

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Sysctl comes with various options that help us in doing different tasks. Let us look at the options and what they are used for:

1. -a

This option displays all the values currently available. You can also write this option as “-A” or “-x”

2. -p

This option loads in sysctl settings from the file specified. You can also write this option as “–load”

3. -w

Use this option when you want to change a setting of sysctl.

4. -q

This option displays the values set to standard output.

5. -N

This option prints only the names of the variables. It comes in handy with shells that have a programmable completion.

6. -e

This option ignores errors about unknown keys. You can also write this option as “–ignore”

7. -b

This option prints the value without going to a new line. You can also write this option as “–binary”

8. -n

This option disables the printing of key names when printing values. You can also write this option as “–names”.

9. -q

This option does not echo a variable set. You can also write this option as “–quiet”

10. -V

This option displays the version information of the sysctl you are using. You can also write this option as “–version”

11. -h

You can also write this option as “–help”. It prints the help menu as shown:

help menu of sysctl

Now that we have laid down some fundamentals, let us look at some practical examples of the sysctl command in the terminal.

Viewing all kernel parameters

To list all the configured parameters of the kernel, pair the sysctl command with the option “-a”.

viewing all kernel parameters

Viewing a specific kernel parameter

If you want to list a specific parameter of a kernel, then you can simply write the name of the parameter next to the sysctl command as follows:

sysctl <parametre name>

viewing a specific kernel parameter

Viewing only the value of a kernel parameter

Notice that in the previous 2 outputs, we have got both the value and name of the parameter, we can instead print only the value and omit the name by using using the option “-n” followed by the parameter name as follows:

sysctl -n <parameter name>

viewing only the value of a kernel parameter

Viewing a specific set of kernel parameters

If you want to list a specific set of parameters of the kernel, say if they have something common in their names, you can pair the sysctl command and the grep command by piping both of them as follows:

sysctl -a | grep <n>

The pipe command will take the output of the “sysctl -a” and send it as the output to the grep command, which will filter out all the names for which kernel parameters you are searching.

viewing a specific set of kernel parameters fan

viewing a specific set of kernel parameters user

Setting the value of a kernel parameter

First things first, to change a value of a kernel parameter, you either have to be a root user or with sudo privileges. To write a parameter’s value, you need to use the option “-w” and also specify the value you want to change ot to it to in the end as follows:

sysctl -w <parameter name> = <new value>

Let us understand this better with an example. Let us take the example of the parameter “net.ipv6.conf.all.autoconf”. Its initial value is 1:

checking parameter value before changing its value

 

Let us change its value to 0 by using the syntax discussed above.

changing configuration from 1 to 0

 

If you really want to confirm if the value has been changed, you can check it by running a simple sysctl command as shown:

rechecking parameter value after changing its value

 

And voila! You just changed the value of a kernel parameter

Changing the value of a kernel parameter permanently

The method that we followed above to change the value of a parameter, is temporary, that means, if you kill the current session of your terminal and start a new one, the value will be set back to initially what if was (in our case, it will change back from 0 to 1).

So, how do you change it permanently? Well, we write the value directly in the configuration file. To open the configuration file of the kernel parameter, type the following command in the terminal:

sudo gedit /etc/sysctl.conf

opening configuration file to change the value of a parameter permanently

changing the value of a parameter in the configuration file

In line 33, we can see the parameter we want to change (net.ipv6.conf.all.forwarding). Let us change the value from one to zero.

After doing the necessary changes press “ctrl” + “c” to save and then exit.

checking the value of a parameter after changing its value permanently

And there you go! The value of the parameter is now changed permanently until you decid3 to go back into the file and then change it back to 1.

If at all you have not saved the changes, you can enforce it with the option “-p” as follows:

sudo sysctl -p /etc/sysctl.conf

Summary

As you have seen, the sysctl command is a really simple command that helps in managing system parameters. You have now learned what sysctl is, where the files related to the kernel are stored, and the various option sussed with the sysctl command.

You have also learned many commands that help in viewing kernel parameters and also setting the values of kernel parameters permanently.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *