Qadry
unixcop Admin

How To Use sysctl Command In Linux

Introduction

sysctl is a software utility of some Unix-like operating systems that reads and modifies the attributes of the system kernel such as its:

  • Version number
  • Maximum limits
  • Security settings.

It is available both as a system call for compiled programs, and an administrator command for interactive use and scripting. Linux additionally exposes sysctl as a virtual file system.

This article shows how to use the sysctl command to view and modify kernel parameters at runtime.

View the Kernel Parameters with sysctl

To view all current kernel parameters use this command:

sysctl -a
[root@unixcop ~]# sysctl -a
abi.vsyscall32 = 1
crypto.fips_enabled = 0
debug.exception-trace = 1
debug.kprobes-optimization = 1
dev.cdrom.autoclose = 1
dev.cdrom.autoeject = 0
dev.cdrom.check_media = 0
dev.cdrom.debug = 0
dev.cdrom.info = CD-ROM information, Id: cdrom.c 3.20 2003/12/17
dev.cdrom.info = 
dev.cdrom.info = drive name:		sr0
dev.cdrom.info = drive speed:		1
........
....

This is a list where each line includes the name of the parameter and its value as shown above.

Check the singel Kernel Parameter value with sysctl

You can check the value of a single parameter by invoking its name as an argument to sysctl.

Note: Only the root user can modify their values.

Example:

Check the raid device speed limit maximum value.

[root@unixcop ~]# sysctl dev.raid.speed_limit_max 
dev.raid.speed_limit_max = 200000
[root@unixcop ~]#

Also you can use grep command to search on the parameter value as shown below:

 sysctl -a | grep dev.raid.speed_limit_max 

sysctl command reads the information from the /proc/sys directory.

/proc/sys is a virtual directory contains file objects that can be used to view and set the current kernel parameters.

You can also view a parameter value by displaying the content of the appropriate file. The only difference is how the file is represented.

Example:

sysctl dev.raid.speed_limit_max and cat /proc/sys/dev/raid/speed_limit_max will show the same output as shown below.

Modify the Kernel Parameters with sysctl

To modify a kernel parameter run the sysctl command followed by the parameter and the value you want to change to

The Syntax for editing the value:

sysctl -w parameter=value

Example:

[root@unixcop ~]# sysctl net.ipv6.route.gc_timeout
net.ipv6.route.gc_timeout = 50
[root@unixcop ~]# 
[root@unixcop ~]# sysctl -w net.ipv6.route.gc_timeout=60
net.ipv6.route.gc_timeout = 60
[root@unixcop ~]# 
[root@unixcop ~]# sysctl net.ipv6.route.gc_timeout
net.ipv6.route.gc_timeout = 60
[root@unixcop ~]#

Notes:

If the value contains empty space or special characters, enclose the value in double-quotes.

You can also pass multiple parameter=value in the same command.

Here we go

The change will be added successfully , but it is not persistent. After a system reboot, the default value is loaded.

So that

To set a parameter permanently, you’ll need to write the changes to /etc/sysctl.conf or another configuration file under the /etc/sysctl.d because it is an included file or sub file from the main configuration one.

sysctl -w net.ipv6.route.gc_timeout=60 >> /etc/sysctl.conf

The -p option allows you to load the changes from a configuration file as shown:

[root@unixcop ~]# sysctl -p /etc/sysctl.d/79-sysctl.conf 
net.ipv6.route.gc_timeout = 60
[root@unixcop ~]#

Conclusion

That’s all

We illustrated how to use the sysctl command to allow you to view and change Linux kernel parameters.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

x