Linux set sysctl variables

How do I set sysctl variables (kernel parameters) under a Debian/Ubuntu or any Linux distributions using command line options?

You need to use the sysctl command which is used to modify kernel parameters at runtime. The parameters available are those listed under /proc/sys/. Procfs is required for sysctl support in Linux. [donotprint][/donotprint]You can use sysctl command to both read and write sysctl variables.

You must login as the root user to use any one of the following command on a Linux operating systems.

Display Linux kernel parameters

The syntax is:
# sysctl variable
####################################
### Display all sysctl variables ###
####################################
# sysctl -a
# sysctl -a | more
# sysctl -a | grep 'something'
# sysctl -a | grep memory

Sample outputs:

vm.memory_failure_early_kill = 0
vm.memory_failure_recovery = 1
vm.overcommit_memory = 0

To see value for kernel variable called kernel.hostname, enter:
# sysctl -n kernel.hostname

How do I set new values?

There are three methods to set new values for given kernel parameters as follows:

Method # 1: Setting value via procfs

You can use standard echo command to write data to variables (this temporary change):
# echo "value" > /proc/sys/location/variable

Method # 2: Temporary on the command line

Use sysctl command with -w option when you want to change a sysctl setting:
sysctl -w variable=value

Method # 3: Configuration file /etc/sysctl.conf

This is recommended way. First open /etc/sysctl.conf file, enter:
# vi /etc/sysctl.conf
Now add value:
variable = value
Close and save the changes. Type the following command to load sysctl settings from the file /etc/sysctl.conf file:
# sysctl -p
OR
# sysctl -p /etc/sysctl.conf
The last method will load settings permanently at boot time from /etc/sysctl.conf file. Read man page of sysctl for information:
$ man sysctl

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.