In this lesson, you will learn how to make kernel parameter adjustments survive system reboots by utilizing the sysctl configuration framework.
The Limitation of Runtime Tuning
As you have learned previously, interacting with the /proc/sys/ directory allows for immediate, runtime adjustments to the kernel state. For example, if you wanted to increase the maximum number of open files for the entire system, you might run:
echo 2097152 > /proc/sys/fs/file-max
While this takes effect instantly, it is ephemeral. Upon reboot, the kernel reinitializes, and these changes are lost as the system reverts to its default values. To bridge the gap between runtime experimentation and production stability, we use persistent configuration files.
Leveraging /etc/sysctl.d/
The sysctl utility reads configuration files to apply kernel settings during the early stages of the boot process, handled by systemd-sysctl.service. Instead of modifying the monolithic /etc/sysctl.conf file, modern best practice is to place custom configuration files inside the /etc/sysctl.d/ directory.
Implementing a Persistent Change
To ensure your performance tuning persists, follow these steps:
-
Create a configuration file: Use a descriptive name ending in
.conf.sudo nano /etc/sysctl.d/99-custom-tuning.conf -
Define your parameters: Add the kernel parameters using
key = valuesyntax.# Increase file descriptor limits for high-concurrency services fs.file-max = 2097152 # Optimize TCP window scaling net.ipv4.tcp_window_scaling = 1 -
Apply the changes: You do not need to reboot to test your new file. You can instruct
sysctlto load your specific configuration immediately:sudo sysctl -p /etc/sysctl.d/99-custom-tuning.conf
By using this approach, you maintain a clean, declarative record of your system's custom state. This is critical for infrastructure-as-code practices, as it allows your kernel configuration to be version-controlled and deployed automatically across your fleet.
Key Takeaway: While /proc/sys/ provides a mechanism for immediate runtime changes, you must define your parameters within /etc/sysctl.d/ files to ensure they are automatically re-applied by the system during every boot cycle.