Oct 11, 2023 3 min read

How to Use Sysctl Command in Linux

Learn to use sysctl command in Linux with our step-by-step tutorial. A command that allows admins, users to interact, modify kernel parameters

Use Sysctl Command in Linux
Table of Contents

Before we begin talking about sysctl command in Linux, let's briefly understand - What is sysctl?

The sysctl command in Linux is a powerful utility that allows administrators and users to interact with and modify kernel parameters at runtime. These parameters, also known as sysctl variables, control various aspects of the Linux kernel's behavior, networking, performance, and security settings. With the sysctl command, you can view, modify, and manage these variables, providing flexibility and fine-grained control over your system's configuration.

In this tutorial, you will understand how to use the sysctl command. We will also address a few FAQs on how to use Sysctl Command in Linux.

Use sysctl to view the Kernel Parameters

Use the sysctl command with the -a option to see all current kernel parameters:

sysctl -a

This will produce a long list that looks like this, with each line containing the name of the parameter and its value:

Output

abi.vsyscall32 = 1
debug.exception-trace = 1
debug.kprobes-optimization = 1
...

The current kernel parameters can be viewed by all users, but only the root user can change their settings.

By giving the name of a single parameter as an argument to sysctl, you can check its value. To check the current swappiness value, for example, type:

sysctl vm.swappiness
Output

vm.swappiness = 20

Swappiness is a Linux kernel feature that controls how frequently the system uses swap space.

The information in the /proc/sys directory is accessed by the sysctl command. The virtual directory /proc/sys includes file objects that can be used to see and change the current kernel parameters.

You can also see the value of a parameter by displaying the contents of the relevant file. The only variation is in the way the file is displayed. Both sysctl vm.swappiness and cat /proc/sys/vm/swappiness, for example, will return a similar output. The directory slashes are substituted with dots when using sysctl, and the proc.sys part is presumed.

Using sysctl to Modify the Kernel Parameters

Run the sysctl command followed by the parameter name and value in the following format to set a kernel parameter at runtime:

sysctl -w parameter=value

Enclose the value in double-quotes if it contains any empty space or special characters. You can also use the same command to pass multiple parameter=value pairs.

💡
When modifying kernel settings on a production system, be especially cautious because it may cause the kernel to become unstable, necessitating a reboot.

To enable IPv4 packet forwarding, use the following command:

sysctl -w net.ipv4.ip_forward=1

The modification takes effect right away, but it isn't long-lasting. The default value is loaded after a system reboot.

You must put the following values to /etc/sysctl.conf or another configuration file in the /etc/sysctl.d directory to set a parameter permanently:

sudo sysctl -w net.ipv4.ip_forward=1 | sudo tee /etc/sysctl.conf

Using the echo command to write the settings to the files in the /proc/sys directory is another technique to update parameters. Instead of using the command above, you could instead use:

echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward

You can load settings from a configuration file with the -p option:

sysctl -p /etc/sysctl.d/file_name.conf

Sysctl reads the /etc/sysctl.conf file if no file is specified.

FAQ's on how to use the sysctl command in Linux

How can I view the current value of a sysctl variable? 

You can view the current value of a sysctl variable by running the command: sysctl variable_name.

Can I use wildcards with the sysctl command?

No, the sysctl command does not support wildcards. You need to specify the exact variable name to view or modify it.

How can I change the value of a sysctl variable? 

To change the value of a sysctl variable, use the command: sysctl -w variable_name=new_value. Be cautious when modifying system parameters.

Which configuration file should I modify to make sysctl changes persistent? 

The configuration file you should modify depends on your Linux distribution. Common ones include /etc/sysctl.conf, /etc/sysctl.d/*.conf, or files under /etc/sysctl.d directory.

Can I revert a sysctl variable to its default value? 

Yes, you can revert a sysctl variable to its default value by resetting it to -1. For example, sysctl -w variable_name=-1.

Is it possible to update sysctl settings on remote machines? 

Yes, you can update sysctl settings on remote machines by using Secure Shell (SSH) to access the remote system and executing the sysctl commands remotely.

Can I reset all sysctl variables to their default values?

Yes, you can reset all sysctl variables to their default values by rebooting the system or by reloading the default configuration settings from the appropriate files.

Conclusion

By using this command, administrators and users can fine-tune their system's behavior, optimize performance, and enhance security. The command allows you to view the current value of variables, change their values temporarily, and persist changes by updating the appropriate configuration files.

It is essential to exercise caution when modifying sysctl variables, as they can have a significant impact on system behavior and security.

We hope this detailed tutorial helped you understand how to use the sysctl command.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.