How to check if a kernel config option is set in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore different methods to check if a specific kernel configuration option is set in Linux. We'll start by examining the /proc/config.gz file, using zcat to decompress and view the kernel configuration if the file exists. This file contains the configuration options used to build the currently running kernel.

If /proc/config.gz is not available, we'll proceed to check for the kernel configuration file in the /boot/ directory, specifically looking for files named config-*. This provides an alternative way to verify the kernel configuration. Finally, we will inspect config logs in dmesg.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/TextProcessingGroup(["Text Processing"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux(("Linux")) -.-> linux/CompressionandArchivingGroup(["Compression and Archiving"]) linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/BasicFileOperationsGroup -.-> linux/less("File Paging") linux/TextProcessingGroup -.-> linux/grep("Pattern Searching") linux/SystemInformationandMonitoringGroup -.-> linux/uname("System Information Displaying") linux/CompressionandArchivingGroup -.-> linux/gzip("Gzip") subgraph Lab Skills linux/ls -.-> lab-558861{{"How to check if a kernel config option is set in Linux"}} linux/cat -.-> lab-558861{{"How to check if a kernel config option is set in Linux"}} linux/less -.-> lab-558861{{"How to check if a kernel config option is set in Linux"}} linux/grep -.-> lab-558861{{"How to check if a kernel config option is set in Linux"}} linux/uname -.-> lab-558861{{"How to check if a kernel config option is set in Linux"}} linux/gzip -.-> lab-558861{{"How to check if a kernel config option is set in Linux"}} end

Check kernel config with zcat /proc/config.gz

In this step, we'll explore how to check the kernel configuration using the zcat command and the /proc/config.gz file. The kernel configuration determines which features are built into the kernel, and understanding it can be helpful for debugging and customization.

First, let's understand what /proc/config.gz is. It's a gzipped file that contains the kernel configuration options used to build the currently running kernel. However, it might not always be present depending on the kernel build configuration.

To check if the file exists, use the following command:

ls -l /proc/config.gz

If the file exists, you'll see output similar to this:

-r-------- 1 root root [占位符: 文件大小] [占位符: 日期] /proc/config.gz

If the file doesn't exist, you'll see an error message:

ls: cannot access '/proc/config.gz': No such file or directory

If the file exists, we can use zcat to decompress and view its contents. zcat is a command-line utility that decompresses gzipped files and prints the output to standard output.

Execute the following command to view the kernel configuration:

zcat /proc/config.gz | less

This command pipes the output of zcat /proc/config.gz to the less command, which allows you to scroll through the configuration file.

You'll see a long list of configuration options, each prefixed with CONFIG_. For example:

CONFIG_POSIX_MQUEUE=y
CONFIG_FHANDLE=y
CONFIG_XFRM=y
CONFIG_XFRM_ALGO=y
CONFIG_XFRM_USER=y
CONFIG_XFRM_INTERFACE=y
CONFIG_INET=y

Press q to exit less.

If /proc/config.gz does not exist, you will need to proceed to the next step to check /boot/config-*.

Verify config in /boot/config-*

In this step, we'll check for the kernel configuration file in the /boot/ directory. This is another common location where the kernel configuration is stored. The file is usually named config-*, where * represents the kernel version.

First, let's list the files in the /boot/ directory to see if a config-* file exists. Use the following command:

ls -l /boot/config-*

You might see output similar to this:

-rw------- 1 root root [占位符: 文件大小] [占位符: 日期] /boot/config-[占位符: kernel version]

If you see a config-* file, it means the kernel configuration is stored in that file. If you don't see any config-* files, it means the kernel configuration is not stored in the standard location in /boot/.

To view the contents of the config-* file, you can use the cat command. Replace [占位符: kernel version] with the actual kernel version from the file name. For example, if the file is named /boot/config-5.15.0-76-generic, the command would be:

cat /boot/config-5.15.0-76-generic | less

This command pipes the output of cat /boot/config-[占位符: kernel version] to the less command, which allows you to scroll through the configuration file.

You'll see a long list of configuration options, each prefixed with CONFIG_. For example:

CONFIG_POSIX_MQUEUE=y
CONFIG_FHANDLE=y
CONFIG_XFRM=y
CONFIG_XFRM_ALGO=y
CONFIG_XFRM_USER=y
CONFIG_XFRM_INTERFACE=y
CONFIG_INET=y

Press q to exit less.

If you have multiple config-* files, you can check the kernel version using the uname -r command:

uname -r

This will output the kernel version currently running on your system. You can then use this information to select the correct config-* file to view.

Inspect config logs in dmesg

In this step, we'll inspect the kernel logs using the dmesg command to find information about the kernel configuration. dmesg is a command-line utility that prints the kernel's message buffer. This buffer contains information about hardware, drivers, and other system events, including details about the kernel configuration at boot time.

To view the kernel logs, use the following command:

dmesg | less

This command pipes the output of dmesg to the less command, which allows you to scroll through the logs.

The kernel configuration information is usually printed at the beginning of the logs during the boot process. To filter the logs and find configuration-related messages, you can use the grep command.

Try the following command to search for lines containing "config":

dmesg | grep config | less

This command pipes the output of dmesg to grep config, which filters the lines containing the word "config", and then pipes the result to less for easy viewing.

You might see output similar to this:

[占位符: 时间戳] Kernel command line: BOOT_IMAGE=/boot/vmlinuz-[占位符: kernel version] root=UUID=[占位符: UUID] ro quiet splash  vt.handoff=7
[占位符: 时间戳]  Kernel command line: BOOT_IMAGE=/boot/vmlinuz-[占位符: kernel version] root=UUID=[占位符: UUID] ro quiet splash vt.handoff=7
[占位符: 时间戳]  DMI: QEMU Standard PC (i440FX + PIIX, 1996), BIOS [占位符: BIOS version] 04/01/2014

You can also search for specific configuration options. For example, to find out if CONFIG_EXT4_FS is enabled, use the following command:

dmesg | grep CONFIG_EXT4_FS

If the option is enabled, you'll see a line similar to this:

[占位符: 时间戳]  EXT4-fs (sda1): mounted filesystem with ordered data mode. Opts: (null)

If the option is not enabled, you won't see any output.

By inspecting the dmesg output, you can verify the kernel configuration and identify any potential issues related to missing or incorrect configuration options.

Press q to exit less.

Summary

In this lab, we explored methods to check the kernel configuration in Linux. We first attempted to access the kernel configuration via zcat /proc/config.gz, learning that this file contains the kernel configuration options used to build the currently running kernel, but its presence depends on the kernel build configuration. We used ls -l to check for its existence and zcat piped to less to view its contents if available.

If /proc/config.gz was not found, the lab directs us to proceed to the next step, which involves checking for the kernel configuration file in the /boot/ directory, typically named config-*.