1. Overview

In a Linux system, a kernel config file is an important component of building the kernel. Using this file, we can modify our system configuration as per our requirement or check the currently configured devices.

In this tutorial, we’ll see how can we get the kernel configuration for our currently running Linux system using two simple methods.

2. Kernel Configuration

In a Linux system, a kernel configuration file is used to build a kernel. It contains a list of kernel options, a list of devices, and device options. We can consider it a template that the make command uses to build the kernel.

As part of the kernel building process, we can set up our config file using different configuration options like make config or make menuconfig. Using these options, we can customize the kernel configuration and generate the files required to compile and link the kernel.

Almost all Linux distributions provide kernel configuration files as part of their package. We can look into distribution-specific documentation in order to find such configurations. The Linux kernel source will have the default configuration provided.

3. Obtaining Kernel Configuration

The method of obtaining the kernel configuration will vary depending on the Linux distribution. For most of the known Linux distributions, we can get the kernel configuration using one of two methods.

3.1. From the /proc Directory

In Linux, the /proc (process information) pseudo-filesystem contains the files that represent the current state of the kernel.

To obtain the currently running kernel config from /proc, we can decompress the config.gz file. However, this is only possible if access to the .config file through /proc/config.gz was enabled during the generation of the initial configuration. This option comes under “General setup” in kernel configuration:

General setup --->
      ...
      [*] Kernel .config support
          [*] Enable access to .config through /proc/config.gz
      ...

Using the zcat command, we can decompress the config.gz file. Let’s capture the output as a file to see the contents easily:

$ zcat /proc/config.gz > filename.config

On successful execution of the above command, a config file with a .config extension will be generated in the current directory:

$ cat filename.config
#
# Automatically generated file; DO NOT EDIT.
# Linux/x86_64 3.16.3 Kernel Configuration
#
CONFIG_64BIT=y
CONFIG_X86_64=y
CONFIG_X86=y
CONFIG_INSTRUCTION_DECODER=y
CONFIG_OUTPUT_FORMAT="elf64-x86-64"
CONFIG_ARCH_DEFCONFIG="arch/x86/configs/x86_64_defconfig"
CONFIG_LOCKDEP_SUPPORT=y
        ....

As an alternative, we can first capture the configuration from the /proc directory using the cat command. Then, using the gunzip tool, we can decompress the configuration in a .config file. This will achieve the same result as zcat:

$ cat /proc/config.gz | gunzip > filename.config

3.2. From the /boot Directory

In Linux, the /boot directory contains important files required for the boot process of the operating system, a config file, and the map installer. Here, we can find the config file for our respective Linux kernel versions:

$ cat /boot/config-$(uname -r) > filename.config

Moreover, we can get the configuration for specific devices using the combination of the pipe operator and the grep command. For instance, to check the modules related to USB, we can run:

$ cat /boot/config-$(uname -r) | grep -i USB
# CAN USB interfaces
CONFIG_CAN_EMS_USB=m
CONFIG_CAN_ESD_USB2=m
CONFIG_CAN_GS_USB=m
CONFIG_CAN_KVASER_USB=m
CONFIG_CAN_PEAK_USB=m
CONFIG_CAN_8DEV_USB=m
CONFIG_USB_IRDA=m
        ....

4. Conclusion

In this article, we discussed config files in Linux systems. We also explored two ways to get the kernel configuration for our currently running kernel from the /proc and /boot directories.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.