What Happens When You Power On a Linux Machine? Inside the Kernel Boot Process
This article explains step by step how a Linux system boots—from hardware initialization by BIOS/UEFI, through the GRUB bootloader loading the compressed kernel, to kernel decompression, initramfs setup, root filesystem mounting, init system startup, user login, and finally the orderly shutdown process, providing practical commands and troubleshooting tips.
When using a Linux system, you may wonder what actually happens between pressing the power button and seeing the desktop or command line. The answer lies in the Linux kernel's boot process.
1. Hardware Initialization: The Prelude to System Startup
Pressing the power button triggers a complex sequence where BIOS/UEFI and the bootloader play essential roles.
BIOS or firmware loads and runs the bootloader.
The bootloader finds the kernel image on disk and loads it into memory.
The kernel initializes devices and its early programs.
The kernel mounts the root filesystem.
The kernel starts init as PID 1, beginning user‑space execution. init launches other system processes.
Finally, init starts a login process for the user.
1.1 BIOS/UEFI: The Hardware Gatekeeper
BIOS (Basic Input/Output System) is a firmware program stored in the motherboard ROM that performs power‑on self‑test (POST), initializes hardware, and selects a boot device. UEFI (Unified Extensible Firmware Interface) is the modern replacement, supporting 32‑ and 64‑bit modes, larger disks via GPT, graphical interfaces, network boot, and Secure Boot for enhanced security.
1.2 Bootloader: Bridging Hardware and Kernel
After BIOS/UEFI hands control to the bootloader, the loader reads the kernel image and loads it into memory, passing necessary boot parameters. GRUB2 is the default bootloader for most Linux distributions; it reads /boot/grub/grub.cfg, locates the kernel, and supplies parameters such as root=/dev/sda1 and ro.
2. Kernel Loading and Initialization: Core System Startup
2.1 Kernel Decompression and Core Initialization
The kernel image is often compressed (e.g., zImage). At boot, a decompressor expands it in memory, then the kernel initializes critical subsystems: process management, memory management, device drivers, and the system‑call interface.
2.2 Loading initramfs
Initramfs (initial RAM filesystem) provides a temporary toolbox containing minimal drivers and utilities needed to mount the real root filesystem. It resolves the chicken‑egg problem of needing drivers to access the disk that contains those drivers.
2.3 Mounting the Root Filesystem
After initramfs runs its scripts, the kernel identifies the root partition (e.g., ext4 on /dev/sda2) and mounts it, establishing the base of the entire file hierarchy.
3. Init System: The Scheduler of System Services
3.1 systemd vs. init: The Commanders
Traditional init follows SysV runlevels and starts services sequentially. systemd replaces runlevels with target units, starts services in parallel, and manages dependencies, logging (journald), and user sessions (logind).
3.2 Runlevels and Service Startup
Runlevels 0‑6 define system states (halt, single‑user, multi‑user, graphical, reboot). systemd maps these to targets such as multi‑user.target (runlevel 3) and graphical.target (runlevel 5).
4. Terminal and User Login: Beginning Human‑Machine Interaction
4.1 Terminal Initialization
Legacy getty manages console login, while systemd provides systemd‑[email protected] and systemd‑login for modern handling of virtual terminals and remote logins.
4.2 Login Methods
Linux supports text‑mode login, graphical login managers (GDM, LightDM, etc.), and remote login via SSH (secure) or Telnet (insecure). Security measures include strong passwords and SELinux enforcement.
5. Shutdown Process: The System’s Curtain Call
5.1 sync – The Data Guardian
The sync command forces all dirty pages in memory to be written to disk, preventing data loss on power loss or abrupt shutdown.
5.2 shutdown/reboot – Ordered Power‑Off
shutdownallows scheduled or immediate halt/reboot with optional warning messages; reboot performs an immediate restart. Proper use avoids filesystem corruption and ensures a clean restart.
6. Linux Kernel Boot Practical Guide
6.1 Environment Preparation
Operating System: Ubuntu 22.04 (x86_64)
Kernel version: 5.15.0 (target compile 5.15.100)
Dependencies: build‑essential, libncurses5‑dev, bison, flex, libssl‑dev, grub2‑common
Kernel files reside in /boot (e.g., vmlinuz‑5.15.100, initrd.img‑5.15.100, System.map‑5.15.100).
GRUB configuration files (e.g., /boot/grub/grub.conf) define menu entries that point to kernel and initramfs images.
Each menu entry includes root, kernel, and initrd directives.
6.2 Practical Steps
① Download and compile the kernel
# Download kernel source
wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.15.100.tar.xz
tar -xf linux-5.15.100.tar.xz
cd linux-5.15.100
# Use current config as base
cp /boot/config-$(uname -r) .config
make menuconfig # optional: adjust modules
# Build kernel (adjust -j according to CPU cores)
make -j4
sudo make modules_install # install modules
sudo make install # install kernel to /bootAfter compilation, three key files appear in /boot:
vmlinuz‑5.15.100 – compressed kernel image
initrd.img‑5.15.100 – initramfs image
System.map‑5.15.100 – kernel symbol table
② Edit GRUB configuration to add a custom entry
# Edit GRUB custom file (path may vary)
sudo vim /etc/grub.d/40_custom③ Add the following menu entry
menuentry 'Custom Linux 5.15.100' --class ubuntu --class gnu-linux {
root=(hd0,1) # adjust to your /boot partition
kernel /vmlinuz-5.15.100 root=/dev/sda2 console=tty0 debug
initrd /initrd.img-5.15.100
}Then update GRUB: sudo update-grub # generate new grub.cfg ④ Reboot and select the custom entry
# View kernel boot log
dmesg | less
# Look for key lines such as:
# [ 0.000000] Linux version 5.15.100
# [ 0.000000] Command line: root=/dev/sda2
# [ 0.5xxxxx] VFS: Mounted root (ext4 filesystem)
# [ 1.2xxxxx] systemd[1]: Starting System Initialization...⑤ Common troubleshooting
Root not found – verify root=/dev/sdaX matches your actual root partition (use lsblk).
Missing drivers – re‑run make menuconfig and enable required filesystem or hardware drivers.
GRUB path error – check root=(hd0,X) and use GRUB’s command line ( c) to list partitions.
6.3 Core Configuration Analysis
The kernel line in the GRUB entry is crucial; it specifies the kernel image location and boot parameters, especially root=, which tells the kernel where the real root filesystem resides.
Deepin Linux
Research areas: Windows & Linux platforms, C/C++ backend development, embedded systems and Linux kernel, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
