sysctl Command in Linux with Examples

August 24, 2023

Introduction

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.

Linux admin using the sysctl command to manage kernel parameters.

Prerequisites

  • A user account with root or sudo privileges.
  • Access to a command line/terminal window.

Note: Modifying kernel parameters affects system behavior, performance, and security. Always test parameter changes in a controlled environment, like a virtual machine, before applying them to production systems.

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] ...]

Note: In command-line syntax, square brackets [] typically indicate that a parameter is optional.

  • 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:

OPTIONDESCRIPTION
-aDisplay all variables and values.
-nDisplay only values for specified variables.
-eDo 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.
-qError messages for stdout kernel parameter values are suppressed.
-fLoad settings from all system configuration files.
-r [pattern]Only list variables that match the defined pattern.
-VDisplay the sysctl version.

sysctl Command Examples

Note: Only a sudo or root user can modify parameter values. Read actions usually do not require sudo or root privileges.

The following commands showcase some of the basic sysctl command functionalities:

1. Show All Kernel Parameters

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

sysctl -a
List all kernel parameters using the sysctl command.

2. Display Parameter Names without Values

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

sysctl -a -N
Only display kernel parameter names using sysctl.

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:

sudo sysctl -a | grep net.ipv6.route
Search kernel parameters by name using sysctl.

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
Display swappiness in Linux using sysctl command.

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
Retrieve only the kernel parameter value using sysctl.

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 30:

sudo sysctl -w vm.swappiness=30
Modify Linux kernel parameter value using the sysctl command.

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

You know how the sysctl command works, its syntax, and options, and you have tested several rudimentary commands.

Sysctl is not the only command you can use to manage kernel parameters in Linux. Read our article on using the modprobe command to add or remove kernel modules.

Was this article helpful?
YesNo
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.
Next you should read
Kernel Panic: Definition and Causes
June 20, 2023

The kernel panic error prevents the system from operating normally and requires it to be restarted. This guide explains kernel panic and offers troubleshooting advice.
Read more
How to Build Linux Kernel From Scratch
November 12, 2020

All Linux distributions come with a predefined kernel which is usually outdated. Follow this step-by-step guide to learn how to build a Linux kernel from scratch.
Read more
How to Remove Old Kernels on Ubuntu
November 7, 2024

It is considered good system hygiene practice to prune out old files and dependencies. This guide shows you how to delete old Linux kernels in a couple of simple steps.
Read more
How to Check Kernel Version in Linux
April 18, 2024

The Linux kernel is built with multiple protocols to ensure stability and security. This guide will walk you through how to check the Linux kernel version of your operating system.
Read more