sysctl Command in Linux with Examples

Estimated reading: 4 minutes 202 views

Linux administrators use the sysctl command to read or modify kernel parameters at runtime. The option to control and modify networking, I/O operations, and memory management settings in real-time, without performing a reboot, is essential for high-availability systems.

Find out how to use the sysctl command and its options to tweak system performance on the fly.

sysctl Command Syntax

Linux kernel parameters are in the proc/sys/ directory. The sysctl command allows admins to make structured changes to these parameters and retain the changes on reboots without interacting with /proc/sys/ files directly.

The sysctl command in Linux has the following syntax:

sysctl [options] [variable[=value] ...]
  • sysctl – The command name.
  • [options] – The command accepts optional flags to customize the behavior.
  • [variable] – The name of the kernel parameter you want sysctl to read or modify. Enter a variable without a value to read the current value of the variable only.
  • [=value] – If the command contains a variable, you can assign a value to the variable. Entering a value will set the new value for the variable.
  •  – The three full stops mean it’s possible to specify multiple variable=value pairs in a single sysctl command.

The sysctl syntax is flexible and allows users to enter the command with or without options, read existing values, or define new values for specific kernel parameters.

sysctl Command Options

The following table provides a breakdown of the standard sysctl options:

OPTION DESCRIPTION
-a Display all variables and values.
-n Display only values for specified variables.
-e Do not display errors for non-existent variables.
-w [parameter]=[value] Change the value for the specified variable.
-p [file] Load values from the specified configuration file. The system loads values from the /etc/sysctl.conf file by default if no file is provided.
-q Error messages for stdout kernel parameter values are suppressed.
-f Load settings from all system configuration files.
-r [pattern] Only list variables that match the defined pattern.
-V Display the sysctl version.

sysctl Command Examples

1. Show All Kernel Parameters

Review all kernel parameters and their current values using the -a option:

sysctl -a

2. Display Parameter Names without Values

Use the following command to display parameter names without their values:

sysctl -a -N

3. Search for Specific Parameters

Combine the -a option and the grep utility to search for a specific kernel parameter. The following command searches for all parameters related to IPv6 routing:

sysctl -a | grep net.ipv6.route

4. Display a Specific Parameter Value

Use the sysctl command and the kernel parameter name to display its value. The following command displays the swappiness value of the system:

sysctl vm.swappiness

5. Display Value Without Parameter Name

If you need to retrieve the parameter value without its name, for example, for scripting purposes, utilize the -n option:

sysctl -n vm.swappiness

6. Modify a Parameter Value

The -w option enables users to modify an existing kernel parameter value. The following command sets the swappiness value to 20:

sysctl -w vm.swappiness=20

The system’s swappiness level influences the kernel’s behavior toward swapping.

7. Load Configuration from Default File

Use the -p option to reload default values from the /etc/sysctl.conf and /etc/sysctl.d/ files:

sysctl -p

There is no output, and the values are reloaded without a reboot.

8. Load Configuration from Custom File

Enter the following command to load settings from a custom config file:

sysctl -p [/path/to/custom-sysctl.conf]

Replace [/path/to/custom-sysctl.conf/] with the actual path to your configuration file.

9. Load All System Configuration Files

The -f option is used to load values from all system configuration locations:

sysctl -f

The system loads settings from the following configuration files: /run/sysctl.d/*.conf, /etc/sysctl.d/*.conf, /usr/local/lib/sysctl.d/*.conf, /usr/lib/sysctl.d/*.conf, /lib/sysctl.d/*.conf, and /etc/sysctl.conf.

10. Suppress Error Messages for Non-Existent Parameters

Omitting errors for non-existent parameters results in a cleaner output and reduces unnecessary noise in logs. Use the -q option to suppress error messages for a specified parameter:

sysctl -q [non_existent_parameter]

Replace [non_existent_parameter] with the actual name of the variable.

Conclusion

Hopefully, this article will be useful for you. Good luck !