How to use the command 'sysctl' (with examples)
- Linux
- December 25, 2023
Sysctl is a command-line tool that allows users to view and modify kernel runtime variables on a Unix-like operating system. These variables control various aspects of the system’s behavior and performance. The ‘sysctl’ command is commonly used for system tuning and troubleshooting purposes.
Use case 1: Show all available variables and their values
Code:
sysctl -a
Motivation: This use case is helpful when you want to see a comprehensive list of all available kernel variables and their current values. It provides a quick way to obtain detailed information about the system’s configuration.
Explanation: The ‘-a’ flag is used to display all kernel variables along with their current values. When running this command, the output will show a long list of variables and their corresponding values.
Example output:
...
kernel.hostname = myhostname
kernel.osrelease = 5.4.0-47-generic
kernel.version = #51~18.04.1-Ubuntu SMP Sat Sep 5 14:35:50 UTC 2020
...
Use case 2: Set a changeable kernel state variable
Code:
sysctl -w section.tunable=value
Motivation: This use case is useful when you need to modify a specific kernel variable to tailor the system’s behavior according to your requirements. It allows you to change the value of a tunable kernel parameter on the fly.
Explanation: The ‘-w’ flag is used to set the value of a changeable kernel state variable. “section.tunable” refers to the specific variable you want to modify, while “value” represents the desired new value.
Example:
Suppose you want to increase the maximum number of open file handlers to 10000, the command would look like this:
sysctl -w fs.file-max=10000
Use case 3: Get currently open file handlers
Code:
sysctl fs.file-nr
Motivation: This use case is useful when you want to obtain information about the current number of allocated file handles in the system. It provides insights into the system’s file handling capabilities and resource allocation.
Explanation: The ‘fs.file-nr’ variable represents the current number of allocated file handles by the system. By running this command, you can fetch the value of this particular kernel variable.
Example output:
fs.file-nr = 3912 0 65536
The output consists of three numbers. The first number represents the number of allocated file handles, the second number represents the number of unused file handles, and the third number represents the system’s maximum number of file handles.
Use case 4: Get the limit for simultaneous open files
Code:
sysctl fs.file-max
Motivation: This use case is helpful when you need to determine the maximum number of simultaneous open files supported by the system. It gives you insights into the system’s limits for file handling.
Explanation: The ‘fs.file-max’ variable represents the maximum number of simultaneous open files supported by the system. By running this command, you can fetch the value of this particular kernel variable.
Example output:
fs.file-max = 65536
The output displays the current maximum limit for simultaneous open files.
Use case 5: Apply changes from /etc/sysctl.conf
Code:
sysctl -p
Motivation: This use case is useful when you have made changes to the /etc/sysctl.conf configuration file and want to apply those changes without restarting the system. It allows you to activate the modified kernel variables immediately.
Explanation: The ‘-p’ flag is used to apply changes from the /etc/sysctl.conf file. This file contains a list of kernel variables and their desired values. By running this command, you can load the modified configuration into the kernel.
Example:
Suppose you have made changes to the /etc/sysctl.conf file to increase the maximum number of open file handlers, the command would look like this:
sysctl -p
This will apply the changes from /etc/sysctl.conf and activate the modified kernel variable without requiring a system restart.
Conclusion:
The ‘sysctl’ command is a powerful tool for managing and tuning the kernel runtime variables on Unix-like operating systems. It provides a way to view, modify, and apply changes to various aspects of the system’s behavior. This article highlighted five use cases with examples, demonstrating the versatility and functionality of the ‘sysctl’ command.