Linux – What’s the right way to set Linux kernel runtime parameters

kernellinuxparametersysctl

What's the prescribed way to set Linux kernel runtime parameters? I've seen sometimes that people will set these in files such as /etc/rc.local.

Is this really the right way to do this?

Best Answer

You can use sysctl to set some of the kernel parameters, specifically the ones under /proc/sys. These can be set in the file /etc/sysctl.conf or added to a single file (the preferred method on some distro's such as Fedora) in the directory /etc/sysctl.d. On distros that have this directory it's meant for customization's.

excerpt from sysctl's man page

   sysctl - configure kernel parameters at runtime

Example

You can get a partial list of what kernel parameters are currently set using this command:

$ sudo sysctl -a | head -5
abi.vsyscall32 = 1
debug.exception-trace = 1
debug.kprobes-optimization = 1
dev.cdrom.autoclose = 1
dev.cdrom.autoeject = 0

Making a change

/etc/sysctl.conf

Simply add rules to the file sysctl.conf.

# sysctl.conf sample
#
kernel.domainname = example.com
; this one has a space which will be written to the sysctl!
kernel.modprobe = /sbin/mod probe

You can also use the sysctl.conf command line to make edits to this file without having to edit it directly.

$ sysctl -w kernel.domainname="example.com"

After making any changes be sure to make them active.

$ sysctl -p

/etc/sysctl.d

To add your override of this parameter simply put it in a file named similarly to the files that are already present in the /etc/sysctl.d directory.

$ ls -l /etc/sysctl.d
total 40
-rw-r--r-- 1 root root   77 Jul 16  2012 10-console-messages.conf
-rw-r--r-- 1 root root  490 Jul 16  2012 10-ipv6-privacy.conf
-rw-r--r-- 1 root root  726 Jul 16  2012 10-kernel-hardening.conf
-rw-r--r-- 1 root root 1184 Jul 16  2012 10-magic-sysrq.conf
-rw-r--r-- 1 root root  509 Jul 16  2012 10-network-security.conf
...

In a file named something like 99-myparam.conf.

$ more 10-console-messages.conf 

# the following stops low-level messages on console
kernel.printk = 4 4 1 7

Where the name of the parameter is on the left, and it's corresponding value is on the right.

See sysctl's man page for more details.

Related Question