Linux Boot Architecture and Initialization Flow
Upon powering on, the firmware performs hardware checks before handing control to the bootloaedr. The Linux startup sequence generally proceeds through five distinct phases: kernel loading, init execution, system initialization, terminal establishment, and user authentication.
Kernel Loading Phase
The process begins with the BIOS or UEFI firmware identifying the boot device. The bootloader loads the compressed kernel image from /boot into memory. Once decompressed, the kernel initializes hardware components.
Crucially, the kernel utilizes an initial ramdisk (initramfs or initrd). Since the kernel image itself is minimal and lacks specific storage drivers, it cannot immediately access the root filesystem. The initramfs resides in memory during early boot, providing necessary drivers to mount the actual root partition. Once the root filesystem is mounted read-only, the kernel executes /sbin/init and transfers control to user space.
The Init Process
The /sbin/init program becomes the first user-space process (PID 1). All subsequent processes are descendants of this parent. Its primary responsibility is establishing the system environment.
Runlevels and Targets
Historically, System V init used runlevels to define system states. While modern systems often use systemd targets, the conceptual levels remain relevant for understanding legacy configurations:
- 0: Halt (Power off)
- 1: Single-user mode (Maintenance)
- 2: Multi-user without networking
- 3: Full multi-user with networking (CLI)
- 4: Unused/Reserved
- 5: Multi-user with GUI (X11)
- 6: Reboot
Configuration for traditional init systems resided in /etc/inittab, whereas systemd utilizes unit files in /usr/lib/systemd/system or /etc/systemd/system.
System Initialization Scripts
During the boot sequence, the init process invokes initialization scripts. In SysV-based systems, /etc/rc.d/rc.sysinit handles core tasks such as activating swap, checking filesystem integrity, and loading hardware modules.
Subsequently, the system executes runlevel-specific scripts located in directories like /etc/rc.d/rc3.d/. These directories contain symbolic links pointing to actual scripts in /etc/rc.d/init.d/. Links starting with S trigger the script with a start argument, while K links trigger a stop argument. This mechanism ensures services are correctly started or stopped when transitioning between runlevels. Service management tools like chkconfig allow administrators to configure which daemons launch at specific levels.
Terminal Configuration
Once background services are active, init spawns getty processes to manage virtual consoles. Typically, six text-based terminals (tty1 through tty6) are configured to respawn automatical. This ensures a login prompt is always available on these consoles. The getty program prepares the terminal line and invokes the login binary to handle authentication.
User Authentication
Access methods include local console login, SSH remote acess, or graphical display managers. For text-based logins, the login program validates credentials. Security checks involve several files:
/etc/nologin: If present, non-root users are denied access and shown the file's contents./etc/securetty: Restricts root login to specific terminals listed within./etc/usertty: Applies additional per-user access restrictions.
Console Switching
Linux provides multiple virtual consoles. Users can toggle between text terminals (tty1-tty6) using Ctrl + Alt + F1 through F6. If a graphical session is active on tty7 (or higher), Ctrl + Alt + F7 returns to the GUI. In virtualization environments, host key combinations may differ, often requiring Alt + Space or additional modifiers to capture keystrokes.
System Power Management
Proper shutdown procedures are critical to prevent data loss. The standard sequence involves syncing disk buffers, stopping services, and halting hardware.
The shutdown command is the preferred method for powering off or rebooting. It notifies logged-in users and terminates processes gracefully.
# Sync memory to disk manually
sync
# Schedule a halt in 5 minutes with a warning message
shutdown -H +5 "System maintenance starting soon"
# Immediate power off
shutdown -P now
# Reboot immediately
shutdown -r now
# Alternative reboot command
reboot
# Alternative halt command
poweroff
Administrators should always execute sync before forcing a power loss, though shutdown handles this automatically. Valid commands for halting include shutdown -h now, halt, poweroff, or init 0. Rebooting can be achieved via shutdown -r now, reboot, or init 6.