When maintaining large-scale Linux enterprise infrastructure, standard server reboots are a source of significant downtime. For bare-metal servers equipped with massive RAM arrays, redundant power supplies, and complex RAID controllers, the hardware initialization phase (POST – Power-On Self-Test) performed by the system BIOS or UEFI can take upwards of 5 to 15 minutes before the bootloader (GRUB) is even loaded. If a security patch or kernel upgrade requires a reboot, this hardware initialization time drastically inflates the maintenance window. To bypass this entirely, Linux administrators utilize kexec, a mechanism that allows the running kernel to seamlessly load and execute a completely new kernel directly from memory, skipping the hardware reboot cycle entirely.
The Architecture of kexec
The kexec utility (Kernel Execution) operates by exploiting a specific system call of the same name. It is a two-stage process:
- Load Phase: The system administrator uses the user-space
kexecutility to load a new kernel image (vmlinuz) and an initial RAM disk (initramfs) directly into a reserved segment of the currently running system RAM. - Execute Phase: The administrator triggers the reboot. Instead of shutting down the motherboard and dropping power, the running Linux kernel cleanly unmounts filesystems, shuts down peripheral devices, and then executes a direct jump instruction to the memory address where the new kernel was loaded in Phase 1.
The new kernel boots instantly, initializing the operating system in seconds rather than minutes, as it assumes the hardware is already in a functioning, powered state.
Installing the kexec-tools Package
To implement this capability, you must install the user-space tools on your Linux distribution. On Ubuntu, Debian, or other apt-based systems, execute the following command:
sudo apt update
sudo apt install kexec-tools
During the installation, a critical prompt will appear asking: “Should kexec-tools handle reboots?”
If you select Yes, the package will modify your systemd configuration (or legacy sysvinit scripts) so that every standard sudo reboot command automatically attempts a kexec bypass instead of a cold hardware reboot. While powerful, this can be dangerous if a specific hardware peripheral requires a hard power cycle to clear a fault state. It is generally safer to select No and execute kexec manually when required.
Manually Executing a kexec Reboot
If you opted to control kexec manually, you must explicitly load the new kernel image into memory before triggering the soft reboot.
Assume you have just upgraded your system via apt upgrade, and a new kernel (e.g., version 6.2.0-35-generic) has been placed in the /boot directory.
Step 1: Load the New Kernel
Use the kexec -l flag to load the specific kernel image and its corresponding initial RAM disk. You must also append the exact kernel boot parameters currently utilized by your system.
# Extract current boot parameters
BOOT_PARAMS=$(cat /proc/cmdline)
# Load the new kernel into RAM
sudo kexec -l /boot/vmlinuz-6.2.0-35-generic \
--initrd=/boot/initrd.img-6.2.0-35-generic \
--append="$BOOT_PARAMS"
This command returns silently if successful. The new kernel is now staged in memory.
Step 2: Trigger the Execution
To initiate the instantaneous reboot, instruct systemd to trigger the kexec target.
sudo systemctl kexec
Alternatively, if you are not utilizing systemd, you can trigger the execution directly using the -e flag:
sudo kexec -e
The active SSH session will terminate immediately. The server will not lose power, the server fans will not spin up to maximum RPM, and the BIOS screen will not appear. Within seconds, you will be able to reconnect via SSH, and verifying the system via uname -r will mathematically confirm that the new kernel (6.2.0-35-generic) is successfully running.