Obtain Kernel Config from Currently Running Linux System

本文介绍了Linux系统中如何通过/proc和/boot目录获取当前运行内核的配置文件,包括使用`config.gz`文件和`config-$(uname-r)`文件,并提供了相应的命令示例。
摘要由CSDN通过智能技术生成

Obtain Kernel Config from Currently Running Linux System | Baeldung on Linux

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
      ...Copy

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.configCopy

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
        ....Copy

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.configCopy

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.configCopy

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
        ....Copy

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 closed on this article!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值