Last modified: August 04, 2024

This article is written in: πŸ‡ΊπŸ‡Έ

System Startup Process

What happens between the time you push the power button and the time you see the login prompt?

+------------------+          +-------------------+            +----------------------+
|    BIOS/UEFI     |   --->   |     Boot Loader   |    --->    |   Kernel & Initramfs |
| (Basic firmware) |          | (e.g., GRUB, LILO)|            |(Initializes hardware)|
+------------------+          +-------------------+            +----------------------+
         |                              |                                  |
         | System Startup               | Load the Kernel                  | Unpack Initramfs
         v                              v                                  v
+------------------+           +--------------------+           +----------------------+
| Hardware Testing |           | OS Selection (GRUB)|           |    Kernel Execution  |
| (POST sequence)  |           | and Kernel loading |           | (Mounts root FS and  |
+------------------+           +--------------------+           |  starts Init process)|
                                                                +----------------------+
                                                                          |
                                                                          | Launches System Services
                                                                          v
                                                                  +---------------------+
                                                                  |      Systemd        |
                                                                  | (or another init)   |
                                                                  | (System & Service   |
                                                                  |  Management)        |
                                                                  +---------------------+

1. Bootup Process

The bootup process is a sequence of events that occurs when you power on a computer. It involves several stages including POST, boot loader execution, loading and initializing the operating system.

1.1 Power-On Self-Test (POST)

When a computer is powered on, the BIOS or UEFI (Unified Extensible Firmware Interface) performs a Power-On Self-Test (POST). This test checks the integrity of the hardware components and ensures that everything is functioning correctly. In case of any errors, POST halts the boot process and sends out an error message.

1.2 Boot Loader Execution

After successfully passing POST, BIOS/UEFI locates the bootable device. It then searches for, loads, and executes the first-stage boot loader. This boot loader is responsible for transitioning the computer from firmware services to the actual operating system. To modify the boot sequence, you may need to press a specific key (like F12 or F2) during this stage.

Types of First-Stage Boot Loaders

There are two primary types of first-stage boot loaders:

Most computers either have BIOS or UEFI, not both.

2. Master Boot Record (MBR)

The Master Boot Record is the first sector of a bootable disk (e.g., /dev/sda or /dev/hda). It's typically 512 bytes in size and comprises:

Computer Starts
                         |
                         v
              +---------------------+
              |     BIOS/UEFI       |
              | (Basic Input/Output |
              |   System/Unified    |
              | Extensible Firmware |
              |     Interface)      |
              +---------------------+
                         |
                         | Reads MBR
                         v
              +---------------------+
              |                     |
              |   Master Boot       |
              |       Record        |
              |                     |
              +---------------------+
                         |
+------------------------+-----------------------+
|                        |                       |
|   Boot Loader Info     |   Partition Table     |
|     (loads second      |  (organizes disk into |
|     stage boot loader) |    partitions)        |
|                        |                       |
+------------------------+-----------------------+
                         |
                         | MBR Validation Check
                         v
              +---------------------+
              |     Second Stage    |
              |      Boot Loader    |
              |   (e.g., GRUB, LILO)|
              +---------------------+

3. Second-Stage Boot Loader

This stage takes over after the first stage. One of the most common second-stage boot loaders is GRUB (Grand Unified Bootloader). GRUB allows users to choose which kernel image should be executed and displays a splash screen.

In addition, GRUB understands file system layouts and can load modules, such as those for USB or graphics card support, into the kernel. The newer version of GRUB is GRUB2.

GRUB GRUB2
Version Legacy Newer Version
Configuration Files menu.lst, grub.conf /boot/grub/grub.cfg
Ease of Modification More Difficult Customize with /etc/default/grub
Live Boot Environments No Support Can Boot from ISO or USB
Boot Menu Display Usually Displays on Boot Hidden Boot Menu (Hold down SHIFT during boot)

+-------------------+                  +-------------------+
|                   |                  |                   |
|    MBR / EFI      |                  |   Configuration   |
| (Boot Instructions|----------------->|      Files        |
|     from BIOS)    |   Input Stage    |  (e.g., grub.cfg) |
|                   |                  |                   |
+-------------------+                  +-------------------+
                           |
                           |
                           v
              +-------------------------------+
              |        Boot Loader            |
              |-------------------------------|
              | Initializes and Checks Config |
              | Identifies OS Options         |
              | Displays Menu (if multi-boot) |
              | Loads Selected OS Kernel      |
              | Transfers Control to OS       |
              +-------------------------------+
                           |
                           |
                           v
              +-------------------------------+
              |          Operating            |
              |           System              |
              |   (Loaded and Ready to Run)   |
              +-------------------------------+

4. Kernel Initialization

After the bootloader has completed its tasks, the kernel is loaded and executed. The kernel plays a central role in managing the computer's resources, and is essentially the heart of the operating system. In a Linux system, the kernel is highly modular and efficient, adding to its flexibility and performance.

Kernel Tasks

5. Init Process

The init process, short for 'initialization', is the first process that starts on a Linux system. Init runs as a daemon with PID 1, marking the commencement of the user-space system.

Responsibilities of the Init Process

6. Runlevel

The runlevel is a system mode that defines the services and processes that the system should run. Each runlevel corresponds to a different state of the machine, from halted state to single-user mode, to full multi-user with GUI and networking.

Runlevel Description
0 Halt, shutting down the system
1 Single-user mode, usually for system maintenance
2 Multiuser, without NFS (network file system)
3 Full multiuser mode, with networking
4 Unused
5 Multiuser mode with GUI
6 Reboot, restarting the system

Interacting with Runlevels

You can display the current runlevel using the runlevel command:

runlevel

To switch to a different runlevel, for example runlevel 3, use the telinit command:

CODE_BLOCK_PLACEHOLDER To set a default runlevel, you need to edit the /etc/inittab file. For example, to set runlevel 3 as the default, find and replace the following line:

telinit 3

Remember to be cautious when editing this file, as incorrect settings can prevent your system from booting correctly.

7. Targets (SystemD)

In Linux distributions that utilize SystemD, the concept of runlevels has been replaced by targets. Targets represent different states that the system can be in, and each target is associated with a specific set of services and units that are started or stopped to reach that state. This allows for more flexibility and control over the system's operational mode.

Target Equivalent Runlevel Description
poweroff.target Runlevel 0 Halt, shutting down the system
rescue.target Runlevel 1 Single-user mode, usually for system maintenance
emergency.target Special Similar to rescue.target, but mounts the minimum number of file systems read-only
multi-user.target Runlevel 3 Multiuser mode, without GUI
graphical.target Runlevel 5 Multiuser mode with GUI
reboot.target Runlevel 6 Reboot, restarting the system

If you are using a SystemD-based distribution, the following command will list the symbolic links between the old runlevel targets and the new SystemD targets:

id:3:initdefault:

This will produce output similar to the following, showing how each runlevel is mapped to a corresponding SystemD target:

ls -l /usr/lib/systemd/system/runlevel*.target

Interacting with SystemD Targets

You can display the current active target with the following command:

/usr/lib/systemd/system/runlevel0.target -> poweroff.target
/usr/lib/systemd/system/runlevel1.target -> rescue.target
/usr/lib/systemd/system/runlevel2.target -> multi-user.target
/usr/lib/systemd/system/runlevel3.target -> multi-user.target
/usr/lib/systemd/system/runlevel4.target -> multi-user.target
/usr/lib/systemd/system/runlevel5.target -> graphical.target
/usr/lib/systemd/system/runlevel6.target -> reboot.target

To change to a different target, use the systemctl isolate command. For example, to switch to multi-user.target, use:

systemctl list-units --type target --state active

You can display the default target (the target that the system boots into by default) with this command:

systemctl isolate multi-user.target

And you can set a new default target with the systemctl set-default command. For instance, to make multi-user.target the default, use:

systemctl get-default

Once the services and units associated with the chosen target have been started, the system is ready for use. At this point, the login prompt is displayed, marking the end of the boot process.

Understanding Kernel Panic

A Kernel panic is a fatal error condition detected by the Linux kernel, triggering a state where the operating system cannot continue to function safely. It is equivalent to a system crash, and its causes are varied, ranging from hardware malfunctions and software bugs to potential security vulnerabilities. When a Kernel panic occurs, the system typically prints an error message, dumps the kernel memory for post-mortem debugging, and then either waits for a reboot or attempts an automatic reboot depending on the kernel settings.

Potential Causes of Kernel Panic

Handling Kernel Panic

Essential System Management Commands

In Linux, several command-line utilities allow you to manage your system effectively. Here are some of the most commonly used commands.

Managing the Hostname

A hostname is a unique name that identifies a system within a network. It's essential for communication in a network environment as it differentiates one machine from another.

Viewing the hostname: Use the hostname command to print the current hostname of the system:

systemctl set-default multi-user.target

Setting the hostname: In SystemD-based systems, you can change the hostname using the hostnamectl command. Alternatively, you can manually modify the /etc/hostname file.

hostname

Checking System Uptime

The uptime command is used to check how long the system has been running without rebooting. It also displays the current time, number of logged-in users, and system load averages over the last 1, 5, and 15 minutes.

bash
hostnamectl set-hostname your_new_name

Rebooting the System

Immediate reboot: Use the reboot command or systemctl reboot to restart the system immediately.

uptime

Scheduled reboot: To reboot at a specific time, use the shutdown command with the -r option followed by the time in minutes. For instance, to reboot in 5 minutes:

# Using reboot
reboot

# Using systemctl
systemctl reboot

Shutting Down the System

Immediate shutdown: The system can be shut down immediately using the shutdown now command or systemctl poweroff.

shutdown -r +5

Scheduled shutdown: A shutdown can be scheduled at a specific time using the shutdown command with the -h option followed by the time in minutes. For example, to shut down the system in 30 minutes:

# Using shutdown
shutdown now

# Using systemctl
systemctl poweroff

Understanding and correctly using these commands can be incredibly beneficial when managing Linux-based systems. It allows users to maintain control over their systems, schedule tasks, and keep track of their system's status.

Recovering the Root Password

If you forget the root password on your Linux system, you can recover it by following these steps. We will interrupt the boot process to gain access to a shell, then change the root password.

  1. Reboot the machine. Start by restarting your computer. You can do this with the reboot command or by manually turning it off and on again.

  2. Interrupt the boot process. During the boot process, you'll see the Grub bootloader screen. This is where you select which kernel you want to boot. Interrupt this by pressing any key before the timer runs out.

  3. Edit the boot options. Highlight the kernel you want to boot and press e to edit the boot options.

  4. Modify the kernel parameters. In the kernel parameters line (usually starting with linux16 or linux), navigate to the end and add rd.break which interrupts the boot process before control is passed from the initramfs to the actual kernel. Press Ctrl-x or F10 to continue booting.

  5. Gain write access to the system root. After booting, you'll be in an emergency shell. The system root is mounted as read-only. Remount it as read-write with the following command:

shutdown -h +30

  1. Change to the system root directory. Use the chroot command to change the apparent root directory to /sysroot:

mount -o remount,rw /sysroot

  1. Change the root password. You are now in a position to change the root password. Use the passwd command and follow the prompts to enter a new root password.

  2. Update SELinux parameters. If your system uses SELinux, you need to update the SELinux parameters. Run the following command to ensure the context labels are reset on the filesystem:

chroot /sysroot

  1. Exit and reboot. Finally, type exit twice. The first exit will leave the chroot environment, and the second will continue the boot process. Your system will then reboot and you should be able to log in with the new root password.

Challenges

I. Reboot your system

II. Change your system's hostname

III. Recover the root password

IV. Practice handling kernel panic

V. Manage system runlevels/targets

πŸ”΄ Remember to switch back to your default runlevel or target after you've completed the challenge.

VI. Schedule system tasks

πŸ”΄ Remember to make sure to cancel these tasks if you are doing this on a production machine, to avoid unwanted system downtime.

Table of Contents

  1. 1. Bootup Process
    1. 1.1 Power-On Self-Test (POST)
    2. 1.2 Boot Loader Execution
  2. 2. Master Boot Record (MBR)
  3. 3. Second-Stage Boot Loader
    1. 4. Kernel Initialization
    2. 5. Init Process
    3. 6. Runlevel
    4. 7. Targets (SystemD)
    5. Interacting with SystemD Targets
  4. Understanding Kernel Panic
  5. Essential System Management Commands
    1. Managing the Hostname
    2. Checking System Uptime
    3. Rebooting the System
    4. Shutting Down the System
  6. Recovering the Root Password
  7. Challenges