Home / Articles / Linux Bootup

Linux Bootup

Understanding how Linux boots from power-on to a login prompt reveals the elegant layering of the OS. The process involves the kernel, the init system, runlevel scripts, and getty — each handing off to the next.

Kernel

The kernel creates a process with process id 0 which eventually gets converted into the swapper process. The kernel then optionally runs the initrd (which stands for init RAM disk) — a temporary filesystem in RAM used by the kernel until the filesystem on the hard-disk is initialized.

Process 0 then invokes the init process (usually located at /sbin/init).

Init

All processes are invoked directly or indirectly by the init process (with the exception of the pager process). The init process has process id 1. When started, it takes care of the following:

  1. Checks filesystem integrity
  2. Starts vital programs and determines the run level from /etc/inittab

The inittab file is a configuration file where each entry has the following format:

id:runlevels:action:process

If the system's current runlevel matches the runlevel in an entry, the action or process in that entry is executed.

Determining the Runlevel

Typically the first line in inittab uses the initdefault action to set the system runlevel:

id:5:initdefault:

Different runlevels configure the system with different capabilities. Linux maintains directories for each runlevel containing scripts to run at startup (S-prefixed) or shutdown (K-prefixed). The inittab calls the rc process for each runlevel:

l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6

TTY Initialization

The inittab also contains entries for terminal initialization:

1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4

These four lines spawn four TTY consoles. Even with a single monitor, multiple consoles are configured — the second console is accessible with CTRL+ALT+2, the third with CTRL+ALT+3, and so on.

Getty and Login

  1. init spawns getty processes (via fork and exec). getty initializes itself, displays the login prompt, and waits for a username.
  2. Once the username is provided, getty invokes the login program.
  3. login calls getpass to collect and validate the password. If validation fails, login exits — init notices (due to the respawn action) and forks again.
  4. If login succeeds, login does the following:
    • Changes to the user's home directory (from /etc/passwd)
    • Changes ownership and permissions of the terminal to the user
    • Sets group IDs and user ID of the tty
    • Initializes environment variables (PATH, HOME, SHELL, etc.)
    • Invokes the shell specified in /etc/passwd for the user