Kernel – Difference Between modprobe and sysctl -w for System Parameters

kernelkernel parametersmodprobesysctl

We know that sysctl command can change kernel parameters with :

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

or by directly editing the file in /proc/sys directory. And for persistent changes, the parameters must be written to /etc/sysctl.d/<moduleName>.conf files as:

# echo kernel.domainname="example.com" > /etc/sysctl.d/domainname.conf

However, we can also change the kernel parameters using the modprobe command:

# modprobe kernel domainname="example.com"

And then there's the modprobe.conf file in the /etc/modprobe.d directories, which is present in multiple locations: /etc/modprobe.d and /usr/lib/modprobe.d. It contains multiple .conf files, and the options can be provided in the appropriate conf file for the module as:

options kernel domainname="example.com"

So, what's the difference between each of these methods? Which method should be used under what specific circumstances?

Best Answer

As far as I know, you can use modprobe to adjust parameters only when the feature in question has been compiled as a module - and you're loading the module in the first place. For setting module parameters persistently, you'll have the /etc/modprobe.d directory. (Generally you should leave /usr/lib/modprobe.d for distribution's default settings - any files in there may get overwritten by package updates.)

If the module in question has been built into the main kernel, then you must use the <module_name>.<parameter_name>=<value> syntax, typically as a boot option. If the parameter in question is available as a sysctl setting, then you can use the sysctl -w command to adjust it too.

All the available sysctl parameters are presented under /proc/sys: for example, kernel.domainname is at /proc/sys/kernel/domainname. Not all module parameters are available as sysctls, but some might be.

If a loadable module has already been loaded, and you wish to change its parameters immediately without unloading it, then you can write the new value to /sys/module/<module_name>/parameters/<parameter_name>. If the module cannot accept dynamic reconfiguration for that parameter, the file will be read-only.

At least on my system, kernel.domainname is a sysctl parameter for the main kernel, and trying to change it with modprobe won't work:

# sysctl kernel.domainname
kernel.domainname = (none)
# modprobe kernel domainname="example.com"
modprobe: FATAL: Module kernel not found in directory /lib/modules/<kernel_version>
# sysctl kernel.domainname
kernel.domainname = (none)

In a nutshell: If you are unsure, first look into /proc/sys or the output of sysctl -a: if the parameter you're looking for is not there, it is not a sysctl parameter and is probably a module parameter (or the module that would provide the sysctl is not currently loaded, in which case it's better to set the value as a module parameter anyway - trying to set a sysctl belonging to a module that is not currently loaded will just produce an error).

Then, find out which module the parameter belongs to. If the module is built into the kernel, you'll probably have to use a boot option; if it is loadable with modprobe (i.e. the respective <module>.ko file exists somewhere in the /lib/modules/<kernel version>/ directory tree), then you can use modprobe and/or /etc/modprobe.d/.

Related Question