3

In Linux, it seems when you change/set values in sysctl, sysctl will apply these settings every boot on-the-fly, but the settings will remain default until sysctl starts and apply the user set values.

How to change the default value so they will constantly be the user-set ones, thus eliminate the time-window of them being default?

Or am I wrong in the first place (sysctl actually change the value permanently)?

1 Answer 1

5

Updated Answer

The "default" value of many sysctl kernel parameters is compiled into the kernel itself.

Example: kernel.numa_balancing_scan_delay_ms is set to 1000 by default in ./kernel/sched/fair.c, which is loaded in ./kernel/sysctl.c.

You can change these at runtime with sysctl3. systemd does this at boot with systemd-sysctl.service early in the boot process.

For some reason, it sounds like you are initializing something very early that a kernel parameter can't be set later and must be set on boot. If early boot service is still too late and you can't tolerate sysctl setting parameters that late, perhaps you can set the parameters at boot2 in your bootloader's kernel line.

If you can't do that, you may need to compile a custom kernel1 with the default values you want.

From the Arch Linux wiki:

There are three ways to pass options to the kernel and thus control its behaviour:

  1. When building the kernel. See Kernel Compilation for details.
  2. When starting the kernel (usually, when invoked from a boot loader).
  3. At runtime (through the files in /proc and /sys). See sysctl for details.

Original Answer

Set the values in /etc/sysctl.conf or in a *.conf file in /etc/sysctl.d/.

Once they're set, you can reload the configuration by running sudo sysctl --system.


Additional Resources

2
  • Sorry but you may not understand my question. I should've ask another question before this. Let's say value A's default is 0, now I set it to 1 via sysctl, I reboot the system and read A is 1 as expected. Then I disable sysctl from auto-start on boot, and reboot the computer, what is A this time?
    – Sam
    Jun 16, 2017 at 17:00
  • @Sam: A is 0. Defaults are often built into the kernel. Example: kernel.numa_balancing_scan_delay_ms is set to 1000 by default in ./kernel/sched/fair.c, which is loaded in ./kernel/sysctl.c. If you can't tolerate sysctl setting a parameter later in boot, perhaps you can set the parameter at boot in your bootloader's kernel line. If you can't do that, you may need a custom kernel.
    – Deltik
    Jun 16, 2017 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.