sysctl Command in Linux | Explained

The system administrator is the one who takes care of the computers, which includes the network, user & rights management, and a few other essential jobs. To do that, the sysctl command is helpful. It allows the user to modify the kernel parameters on the Linux system at runtime.

These kernel parameters can be set at three instances: kernel building, system boot, and run time. The “/proc/sys” contains all the kernel parameters. This article will explain the following about the sysctl command in Linux.

  • How to Use the sysctl Command in Linux?
    • Example 1: Display All Kernel Parameters
    • Example 2: Check the Value of a Single Parameter
    • Example 3: Modify Kernel Parameters Temporarily
    • Example 4: Modify Kernel Parameters Permanently

How to Use the sysctl Command in Linux?

The primary purpose of the sysctl command is to modify the kernel parameters. The syntax followed by this command is provided below.

$ sysctl <Options> <variable>

The “sysctl” usage depends on the variety of options offered by it. The details about the options and the basic syntax can be obtained using the below command.

$ sysctl --help

The “/proc/sys” directory is used by the sysctl command to modify the kernel parameters, and the directory’s contents can be listed using the following command.

$ ls -R /proc/sys

The above image is cropped, and you will see a long list when executing the command.

Example 1: Display All Kernel Parameters

To get a complete list of all configured kernel parameters, the “-a” or “-all” flag is used in the following syntax.

$ sysctl -a

And the above list goes entirely down below.

Example 2: Check the Value of a Single Parameter

you’ll find it a bit difficult to find what you’re looking for, so check the value of a single parameter. We will test it by getting the kernel hostname’s name and knowing how often the system uses the swap space (a portion of virtual memory on the hard disk used by the system when RAM is full.)

The following syntax is used to check the value of a single parameter.

$ sysctl <parameter>

If you’re having problems with the above command, then you need to use the following command to retrieve the file’s contents.

$ cat /proc/sys/kernel/hostname 
$ cat /proc/sys/vm/swappiness

Example 3: Modify Kernel Parameters Temporarily

An administrator can use the sysctl command to modify a kernel parameter (temporarily) in the following syntax.

$ sudo sysctl -w parameter=value

The above command requires “sudo,” which gives the current user elevated privileges, and we enabled IPv4 packet forwarding by changing its value to “1,” and if you wish to disable it, use “0.” Although these changes are immediately in effect, you may need to reboot the system.

Modifying the kernel parameters without knowledge about what you are doing may lead to an unstable kernel which means random system crashes and some unbearable bugs, which could be troublesome.

Example 4: Modify Kernel Parameters Permanently

To permanently modify a kernel parameter using the sysctl command, the administrator must write changes to the file opened after executing this command.

$ sudo vim /etc/sysctl.conf

As you can see, we have made permanent changes to sysctl.conf file for ipv4.ip_forward=1, as pointed out in the above image.

Note: To execute all the commands we’ve discussed above, you must have root privileges, and a user password may be needed.

Conclusion

The Linux system is powered by its kernel. To modify its parameters, the system administrators often prefer the sysctl command. While making any changes to Kernel parameters, make sure you are well aware of the change; otherwise, misusing can harm the system’s overall functionality. This post has briefly explained the working and usage of the sysctl command in Linux.