List and Change Kernel Config Variables With Sysctl Command
Linux operating systems run on the kernel actually Linux kernel. The kernel name is Linux but the distributions are generally named as Linux. Kernel provides operating system services to the user space applications. Kernel have a lot of option and configurations possibilities to meet user space applications, libraries and system administrator needs. These configurations may be IP stack, disk read limit, loaded modules etc. There are different ways to manage this configuration variables or kernel parameters. Sysctl is a tool which provides easy configuration of these kernel parameter.
sysctl [options] [variable[=value] ...]
Kernel Parameter Configuration File
Linux Kernel configuration is stored in a file named
sysctl.conf . This file is locate at
/etc . Following is a sample part from this configuration file.
# Uncomment the next line to enable TCP/IP SYN cookies
# See http://lwn.net/Articles/277146/
# Note: This may impact IPv6 TCP sessions too
# Uncomment the next line to enable packet forwarding for IPv4
# Uncomment the next line to enable packet forwarding for IPv6
# Enabling this option disables Stateless Address Autoconfiguration
# based on Router Advertisements for this host
There is also a directory named
sysctl.d which holds configuration files too. There are some prepared files by operating system. For example kernel hardening configurations can be put into
10-kernel-hardening.conf file for easy management. This files are generally used to make configuration persistent and effect through boots.
$ ls /etc/sysctl.d/
Display Kernel Parameters
Previous part we have examined configuration files. These files only stores some of the kernel configuration variables. There are some default values those can not configured explicitly. Default configuration parameters can be listed with
-a option which means all.
$ sysctl -a
As we can see from screenshot the parameters are arranged like
category.parametername = value
there is a lot of parameters we can not list here.
Display Specific Kernel Parameter
In previous example we listed all kernel parameters. There are a lot of kernel parameters which will fill our screen. If we just want to get a single parameter we can specify it to list. But we need the exact name of the parameter. If we do not know exact name but some part of the name we can filter it. To learn filtering look example below.
$ sysctl -n vm.laptop_mode
Next line provide the value
vm.laptop_mode currently holding.
Filter Kernel Parameters
We can easily list single kernel parameter with
-n option. But what if we want to list a whole category or we only know some part of the kernel parameter name? We can use
grep tool to find what we want. In the example we will list kernel parameter those names have the term
$ sudo sysctl -a | grep "laptop"
Kernel Parameter Categories
There a different type of kernel parameter categories. We will look them briefly with most popular parameters names.
devicecategory used to set connected devices information, hid, mouse click key codes, raid speeds etc.
fscategory is used to store file system related nfs configuration, quoata parameters etc.
kernelcategory provides kernel related config hostname, numa, pty, random entropy etc.
netcategory provides network related parameters like appletalk, ipv4, ipv6 stack, icmp, route
vmcategory provides memory configuration like huge pages, over commit, swap, laptop mode etc.
Set Kernel Parameter Temporarily
Kernel parameters can be changed from user space with sysctl. To change this parameters we need root privilege because it will effect system wide. In this example we will change the
vm.laptop_mode and set it true.
$ sysctl -w vm.laptop_mode=1
Set Kernel Parameter Persistently
In previous example we have set the kernel parameter but it is temporary. After a reboot this kernel parameter will be set to its default value which is 0. We can prevent the lost of kernel parameter values and settings by writing them in to persistent configuration file. We looked these configuration files in the beginning of the tutorial. The simplest way is adding following line to the
OR more tidy and manageable way is create a file as
/etc/sysctl.d/30-laptop for this type of laptop settings and and ad to this file.