How to check if a kernel panic setting is active in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a kernel panic setting is active in Linux. The lab guides you through using the sysctl command to inspect and temporarily modify the kernel.panic setting, which determines the system's behavior after a critical error.

You'll also explore how to verify the sysctl configuration by examining the /etc/sysctl.conf file and inspect kernel parameters in the /proc/sys/kernel directory to understand how these settings influence system behavior.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/UserandGroupManagementGroup(["User and Group Management"]) linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/InputandOutputRedirectionGroup(["Input and Output Redirection"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux/BasicSystemCommandsGroup -.-> linux/echo("Text Display") linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/InputandOutputRedirectionGroup -.-> linux/tee("Output Multiplexing") linux/SystemInformationandMonitoringGroup -.-> linux/service("Service Managing") linux/UserandGroupManagementGroup -.-> linux/sudo("Privilege Granting") linux/UserandGroupManagementGroup -.-> linux/env("Environment Managing") subgraph Lab Skills linux/echo -.-> lab-558866{{"How to check if a kernel panic setting is active in Linux"}} linux/ls -.-> lab-558866{{"How to check if a kernel panic setting is active in Linux"}} linux/cat -.-> lab-558866{{"How to check if a kernel panic setting is active in Linux"}} linux/tee -.-> lab-558866{{"How to check if a kernel panic setting is active in Linux"}} linux/service -.-> lab-558866{{"How to check if a kernel panic setting is active in Linux"}} linux/sudo -.-> lab-558866{{"How to check if a kernel panic setting is active in Linux"}} linux/env -.-> lab-558866{{"How to check if a kernel panic setting is active in Linux"}} end

Check panic setting with sysctl kernel.panic

In this step, we'll explore how to check the kernel's panic setting using the sysctl command. The kernel.panic setting determines how the kernel behaves when a panic occurs. A kernel panic is a critical error from which the system cannot recover, often leading to a system halt or reboot.

The sysctl command is a powerful tool for reading and modifying kernel parameters at runtime. These parameters control various aspects of the system's behavior.

To check the current value of the kernel.panic setting, open your terminal and type the following command:

sysctl kernel.panic

This command will display the current value of the kernel.panic setting. You should see output similar to this:

kernel.panic = 0

The value 0 indicates that the system will not automatically reboot after a kernel panic. If the value is greater than 0, it represents the number of seconds the kernel will wait before automatically rebooting.

Now, let's try changing the value of kernel.panic. To set it to 10 seconds, use the following command with sudo:

sudo sysctl kernel.panic=10

You'll be prompted for your password. After entering it, the command will set the kernel.panic value to 10.

To verify that the change was successful, run the sysctl command again:

sysctl kernel.panic

You should now see:

kernel.panic = 10

This confirms that the kernel.panic setting has been successfully modified.

It's important to note that changes made with sysctl are temporary and will be lost after a reboot. To make the changes permanent, you need to modify the /etc/sysctl.conf file, which we'll cover in the next step.

Verify sysctl config with cat /etc/sysctl.conf

In the previous step, we learned how to temporarily change the kernel.panic setting using the sysctl command. However, these changes are not persistent and will be lost after a reboot. To make the changes permanent, we need to modify the /etc/sysctl.conf file.

The /etc/sysctl.conf file is the main configuration file for sysctl. It contains a list of kernel parameters and their desired values. When the system starts, it reads this file and applies the specified settings.

To view the contents of the /etc/sysctl.conf file, open your terminal and use the cat command:

cat /etc/sysctl.conf

This command will display the contents of the file. You might see some comments (lines starting with #) and some existing settings.

## /etc/sysctl.conf - Configuration file for setting system variables
## See /etc/sysctl.d/ for additional system variables.
## See sysctl.conf (5) for more information.

Now, let's add our kernel.panic setting to this file. We'll use the echo command to append the setting to the end of the file.

echo "kernel.panic = 10" | sudo tee -a /etc/sysctl.conf

Here's a breakdown of the command:

  • echo "kernel.panic = 10": This part creates the text we want to add to the file.
  • |: This is a pipe, which takes the output of the echo command and sends it as input to the tee command.
  • sudo tee -a /etc/sysctl.conf: The tee command reads from standard input and writes to both standard output and one or more files.
    • sudo: Allows us to write to a file that requires administrator privileges.
    • -a: Appends the text to the end of the file instead of overwriting it.
    • /etc/sysctl.conf: The file we want to modify.

After running this command, let's verify that the setting has been added to the file. Use the cat command again:

cat /etc/sysctl.conf

You should now see the kernel.panic = 10 line at the end of the file.

## /etc/sysctl.conf - Configuration file for setting system variables
## See /etc/sysctl.d/ for additional system variables.
## See sysctl.conf (5) for more information.
kernel.panic = 10

To apply the changes, you can either reboot the system or run the following command:

sudo sysctl -p

This command tells sysctl to read the /etc/sysctl.conf file and apply the settings.

Now, the kernel.panic setting will be persistent across reboots.

Inspect kernel params in /proc/sys/kernel

In this step, we'll explore another way to view kernel parameters: the /proc/sys/kernel directory. This directory provides a virtual file system interface to kernel variables. Each file in this directory represents a kernel parameter.

The /proc directory is a special directory in Linux that provides information about running processes and the kernel. It's a virtual file system, meaning that the files and directories within it don't actually exist on the disk. Instead, they are dynamically created by the kernel when accessed.

To list the files in the /proc/sys/kernel directory, open your terminal and use the ls command:

ls /proc/sys/kernel

This command will display a list of files, each representing a kernel parameter. You'll see files like hostname, domainname, osrelease, version, and many others.

acpi_video_flags  ctrl-alt-del  dmesg               hostname       modules_disabled  osrelease  powersave-nap  pty  random  realtime-max-usleep  shmmax  shmall  shmmni  sysrq  tainted  threads-max  unknown_nmi_panic  version  yama

To view the value of a specific kernel parameter, you can use the cat command on the corresponding file. For example, to view the system's hostname, use the following command:

cat /proc/sys/kernel/hostname

This will display the current hostname of the system.

labex

Similarly, to view the kernel version, use the following command:

cat /proc/sys/kernel/osrelease

This will display the kernel version.

5.15.0-76-generic

You can also use the sysctl command to view these parameters, as we saw in the first step. For example, the following command is equivalent to cat /proc/sys/kernel/hostname:

sysctl kernel.hostname

The /proc/sys/kernel directory provides a wealth of information about the kernel's configuration and state. It's a valuable resource for system administrators and developers who need to understand and monitor the system's behavior.

It's important to note that while you can view these files, you typically need root privileges to modify them directly. However, using sysctl is the preferred way to modify kernel parameters, as it provides a more controlled and consistent interface.

Summary

In this lab, we explored how to check and temporarily modify the kernel.panic setting in Linux using the sysctl command. We learned that kernel.panic determines the kernel's behavior upon encountering a critical error, with a value of 0 indicating no automatic reboot and a value greater than 0 representing the delay in seconds before rebooting.

We used sysctl kernel.panic to view the current setting and sudo sysctl kernel.panic=10 to temporarily change it. The lab also highlighted that changes made with sysctl are not persistent across reboots and require modification of the /etc/sysctl.conf file for permanent configuration, which will be covered in subsequent steps.