Open In App

Systl Command in Linux

Last Updated : 24 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

sysctl command is used to view and modify kernel parameters at runtime in Linux systems. A kernel is the core program of a computer's operating system that handles all the interactions between the hardware and software of a computer. Kernel parameters are text strings that are loaded into the kernel when a computer boots. Kernel parameters define how the system behaves or it can be used to enable or disable certain features.

In Linux, the sysctl interface kernel parameters are present as a proc filesystem (procfs) in the /proc/sys directory. A procfs is a special kind of filesystem that is utilized by a running process to store information in them, such as kernel parameters use a procfs to store their parameters in a hierarchical structure. The procfs of kernel parameters can be found in /proc/sys directory.

Syntax:

sysctl [options] [variable[=value]] [...]

options - options are used to modify the behavior of the sysctl command. Some common options are as follows:

option

long option

description

-n

--values

disable the printing of parameter names in terminal output. For example, you want to print only the value of a certain parameter without printing its name. Useful when you want the parameter value in shell scripts.

-e

--ignore

ignores errors related to unknown keys.

-N

--names

use this to print only the names. It may be useful with shells that have programmable completion.

-w

--write

use this argument to set a parameter value.

-p


use this to load sysctl parameters from a specified file or /etc/sysctl.conf if none was given.

-a

--all

display all values currently available.

-b

--binary

display only values without a new line.


-s

--system

load settings from all system configuration files.

-r

--pattern

only print parameters that match the pattern specified. The pattern specified uses extended regular expression.

-h

--help

display help text.

-V

--version

display version information.

variable - variable can be any parameter name on your machine.

value - value is the valid value of the variable name specified. It is optional and only required wherever necessary.

Examples

1. Display kernel version

Display the version of the kernel. Output may vary from machine to machine.

sysctl kernel.version


kernel-version

2. Viewing all kernel parameters

You can view all the currently existing kernel parameters on your system using the -a option with sysctl. This will print a huge list of kernel parameters.

sysctl -a
sysctl-a


The parameter names include the whole path in the procfs. All these parameters can be found in /proc/sys directory. For example, the value of the parameter dev.cdrom.autoclose can be found in /proc/sys/dev/cdrom/autoclose.

cat /proc/sys/dev/cdrom/autoclose

The above command prints the value of dev. cdrom.autoclose parameter in the terminal as output.

3. Viewing single parameter

To view a single parameter, add the parameter's full name after the sysctl command.

sysctl net.ipv4.ip_forward

modify

To only view the value of the parameter use the -n option

sysctl -n net.ipv4.ip_forward

To view parameters matching a certain pattern use the -r or --pattern option. The command given below prints all the parameters that end with "_time".

sysctl -a --pattern _time$

pattern


4. Modifying parameter values at runtime

To modify the parameter's value at runtime use the -w option and the parameter name with the new value. You need root privileges to modify the kernel parameters. The following command sets the value of net.ipv4.ip_forward to 1. This indicates that the packets that are transferred to the computer can be forwarded to another device i.e. the OS can now act as a router.

sudo sysctl -w net.ipv4.ip_forward=1

You will get an output showing the updated parameter name with the updated value. This parameter value is only modified for the current session and will be reset back to the default value after the next reboot.

modify

The parameters found in the/proc/sys directory are those that are currently being used by the kernel. They are destroyed when the OS shuts down. The permanent parameter values are stored inside the /etc/sysctl.conf.

cat /etc/sysctl.conf

If you want to permanently modify a parameter value, you can do so by modifying the parameter in the /etc/sysctl.conf file. (Warning: Modifying some parameters with improper values may cause problems in your usethe system. Proceed with caution). After modifying the values you need to update the kernel parameters manually by running the sysctl command with -p option or reboot your machine.

 sysctl -p

Note: Whenever you are trying to modify a parameter permanently in the /etc/sysctl.conf file make sure to test it out temporarily using the sysctl -w command.

So far we looked at some common use cases of sysctl command and some common kernel parameters. sysctl is an important command used by sysadmins to tweak and test their systems with changing kernel parameter values. You can explore more about this command in the Linux man command.

FAQs

1. How to view the current value of a kernel parameter?

You can view the current value of a kernel parameter using the command

sysctl parameter_name

2. How do you change the value of a kernel parameter?

To change the value of a system kernel parameter use the command with -w option.

sysctl -w parameter_name=new_value

3. How to make changes made by sysctl permanent?

The parameter values changed using the sysctl is not permanent and is reset to default on system reboot. You should modify the /etc/sysctl.conf file to permanently change the parameter values.

4. Is it possible to update sysctl settings on remote machines?

Yes you can update sysctl settings on remote machine by using SSH (secure shell) and executing the sysctl commands remotely.

5. Can I reset all kernel parameter values that are updated by sysctl command to their defaults?

Yes you can reset the kernel parameter values to their defaults by rebooting you machine. Note that this only resets the parameters that were modified using the sysctl command.


Similar Reads