command line, linux

Linux kernel configuration

There are three basic methods to configure a Linux kernel (easiest to hardest methods):

  • modifying tunable kernel configuration parameters
  • by loading new drivers and modules on the fly
  • building a kernel from scratch (by compiling it from the source code)

Tuning Linux kernel parameters

To increase kernel flexibility, special hooks allow parameters to be adjusted on the fly by the admin. There hooks are accessible through an interface represented by files in the /proc filesystem (procfs).

You can view and set kernel options at runtime via special files in /proc/sys.

$ ls  /proc/sys
abi  crypto  debug  dev  fs  kernel  net  sunrpc  user  vm

For example, to change max number of files that system can have open at once, try sth like:

$ cat /proc/sys/fs/file-max
392792
$ echo 392700 > !:1
echo 392700 > /proc/sys/fs/file-max
$ cat /proc/sys/fs/file-max
392700

!:1 gets the first argument from previous command

NOTE: above changes are not remembered across reboots

To save changes permanently, use sysctl command or edit /etc/sysctl.conf file which is read at boot time and its contents are used to set the initial values.

IPv4 & IPv6

Note there are two separate directories of /proc/sys/net/ipv4 and /proc/sys/net/ipv6. When you change a parameter for IPv4, you should also do the same thing to IPv6 – if you support both protocols – which you should.

Loading a Linux device driver

Device drivers are typically distributed on one of three forms:

  • patch
  • loadable kernel module
  • an installation script that installs the driver

The most common form is installation script or package. There should be installation documentation which you should be able to follow.

In case where you have a patch, following command should do:

$ cd kernel_directory
$ patch -p1 < patch_file

Building a custom kernel

First of all, be careful when a new release of some kind of software comes into play – is it as stable as current version? A rule of thumb is to upgrade or apply patches only when it’s worth the risk.

It’s unlikely that you need to build a kernel while using a distro that uses a stable kernel version (even number should indicate stable version – though double check that on kernel.org).

In most distributions kernel source files lies in /usr/src/kernels. Main kernel configuration file lies in /usr/src/kernels/4.14.287-215.504.amzn2.x86_64/.config but it’s not advisable to edit it straight away due to cryptic format and interdependent options. To make admin life easier there are a few make files that help you configure the kernel through a CLI, eg KConfig:

$ cat Kconfig
# SPDX-License-Identifier: GPL-2.0
#
# For a description of the syntax of this configuration file,
# see Documentation/kbuild/kconfig-language.txt.
#
mainmenu "Linux/$ARCH $KERNELVERSION Kernel Configuration"

config SRCARCH
        string
        option env="SRCARCH"

source "arch/$SRCARCH/Kconfig" 

Here’s an outline of the entire process:

  1. Change directory (cd) to the top level of the kernel source directory.
  2. Run make xconfig, make gconfig, or make menuconfig.
  3. Run make clean.
  4. Run make.
  5. Run make modules_install.
  6. Run make install.