Configuring Kernel Settings

Linux Kernel Tuning for High Performance Networking Series

John H Patton
Level Up Coding

--

Photo by Christian Wiediger on Unsplash

Linux Kernel Configuration

When any flavor of linux is installed, the kernel configuration is set to default values for every kernel setting. These defaults are typically meant to get the operating system up and running for general purpose environments, and in many cases these defaults are fine. However, when running a high-volume service, like nginx, these defaults must be adjusted to meet the requirements for high network and CPU throughput.

For example, nginx was originally created to solve the C10k problem and as a result is one of the best choices for a reverse-proxy web server for most enterprise websites. However, no matter what web server is chosen or how fat the pipe is on the network card, the linux kernel configuration controls the maximum throughput possible on any given host.

Configuration File Location

In current linux kernels, the default configurations can be modified by placing files in one of the following folders, in order of precedence:

  1. /etc/sysctl.d/
  2. /usr/lib/sysctl.d/
  3. /run/sysctl.d/

Settings in /etc/sysctl.conf are deprecated and may be ignored, so check your system’s documentation. However, the recommended best practice is to place these files under /etc/sysctl.d/ and to prefix these filenames with a 2-digit plus a dash, then a group name, and end with .conf (ie: 10-nginx.conf). These files will be loaded in lexicographical order, so settings that must always be configured should be in a file with highest precedence to ensure they override any settings made in files lexicographically lower (ie: a setting made in /etc/sysctl.d/99-sysctl.conf* will always override the same setting in /etc/sysctl.d/10-nginx.conf).

To activate the settings, run the following command (requires sudo) or reboot the system:

sudo sysctl -p /etc/sysctl.d/10-nginx.conf

A Best Practice

  • Place kernel setting files under /etc/sysctl.d.
  • Use 2-digit dash prefix to establish a hierarchy for loading files, in order of importance from lowest with lower numbers, and highest with higher numbers.
  • Establish a filename scheme, like XX-GROUPNAME.conf.
  • Create meaningful group names for the group of kernel settings in the file.
  • Comment each setting in the file with the following format, and space each setting with a newline or two for readability:
# full.setting.name
# Description of kernel setting from documentation.
# NOTES
# - Add notes about the setting.
# - Each note describes why it's being set, nuances about the
# setting, and what it is intended to solve.
# RECOMMENDATION
# - Describe any recommended handling of the kernel setting.
# SEE ALSO
# - Any other settings that need to be considered with this setting.
full.setting.name = value
  • Test each file before any reboots occur to catch any typos by running the following command, replacing the file name with the actual file on the system:
sysctl -p /etc/sysctl.d/10-nginx.conf
  • Put all kernel settings that must always be set in a file with a “99-” prefix and enforce a policy to only use values less than 99 for all other groups of settings. Example: 99-sysctl.conf

* NOTE: on some systems this is symlinked to /etc/sysctl.conf

Conclusion

This information is meant as a guide and should help organize linux kernel settings.

If any of the information in this article is inaccurate, please post a comment and I’ll update the article to correct the information.

--

--