kexec – Boot into a New Kernel from the Currently Running Kernel

The kexec command is used in Linux to boot into a new kernel from the currently running kernel. It allows you to load and execute a new kernel image without rebooting the system. This is useful for testing new kernels or loading a kernel with different parameters without having to go through the entire boot process.

Overview

The kexec command requires root privileges to execute. To use kexec, you need to have a new kernel image available on your system. You can download a new kernel from the official Linux kernel website or from your distribution’s package manager.

Once you have a new kernel image, you can use the following command to load and execute it:

sudo kexec -l /path/to/new/kernel
sudo kexec -e

The first command loads the new kernel image into memory, and the second command executes it. Note that the -l option is used to specify the location of the new kernel image, and the -e option is used to execute it.

You can also pass additional parameters to the new kernel using the --append option. For example:

sudo kexec -l /path/to/new/kernel --append="root=/dev/sda1"
sudo kexec -e

This command loads the new kernel image and passes the root=/dev/sda1 parameter to it.

Options

The following table lists all available options for the kexec command:

Option Description
-f, --force Force the kernel to reboot even if it detects errors during the loading process.
-l, --load Load the new kernel image into memory.
-e, --exec Execute the new kernel image.
-p, --protocol Specify the protocol to use for loading the new kernel image.
-a, --append Pass additional parameters to the new kernel image.
-s, --soft Use a soft reboot instead of a hard reboot.
-H, --halt Halt the system instead of rebooting it.
-u, --unload Unload the currently loaded kernel image.

Troubleshooting Tips

  • Make sure you have the necessary privileges to execute the kexec command. You need to be logged in as root or use the sudo command.
  • If the new kernel image fails to load, try using the -f option to force a reboot.
  • If the system fails to boot after executing the new kernel image, try passing different parameters using the --append option.

Notes

  • The kexec command is not a replacement for a full system reboot. It should only be used for testing purposes or for loading a new kernel with different parameters.
  • The kexec command may not work on all systems. It is recommended to test it on a non-production system before using it in a production environment.