How Does Linux Boot?

This article was originally written by Tillman Hodgson. The most current version of it (and the copyright notice) can be found on his site.

This document assumes that you are using a System V style init, which most Linux distributions do use. BSD style inits are similar, but differ in how they implement the rc files.

In talking about booting Linux, you're going to learn about that mysterious init process that always seems to be at the top of your ps waux process listings.

In the shortest possible form, the Linux boot sequence looks something like this:

  1. Computer gets powered on, BIOS runs whatever it finds in the Master Boot Record (MBR), usually lilo
  2. lilo, in turn, starts up the Linux kernel
  3. The Linux kernel starts up the primal process, init. Since init is always started first, it always has a PID of 1.
  4. init then runs your boot scripts, also known as "rc files". These are similar in concept to DOS's autoexec.bat and config.sys, if those had been developed to a fine art. These rc files, which are generally shell scripts, spawn all the processes that make up a running Unix system.

One interesting consequence of this multi-step booting process is that it's very flexible. Once lilo has been loaded, and control over the boot process has been passed to it, it can run any sort of arbitrary (but self-contained) program that you'd care to run. This means that you can use lilo to boot into multiple operating systems.

Once lilo has started to run whatever operating system you've chosen (Linux would be a good choice ;-), it is no longer running (like a relay runner passing the baton). The lilo config file is usually located at /etc/lilo.conf; remember to run the lilo command after editing it to save your changes to the master boot record on disk.

The next step in the Linux boot process is for the Linux kernel to run. This does all sorts of things, but the most important thing that we're interested in is that kernel spawns a copy of init, the First Process. Being the First Process, init will be assigned a PID of 1.

init is in charge of starting all of the normal processes that a running Linux system needs, including the mingetty processes that give you your virtual consoles (ALT-F1 through ALT-F6 on most default Linux installations), starting up any needed services (like networking), and anything else that you might want to do while booting. These are controlled by shell scripts known as "rc files", which are started by commands in the init config file. The config file is usually located at /etc/inittab, and you'll have to tell init to re-read its config file by running telinit q as root. You can find a lot more information on this under the man pages for inittab and init (man inittab and man init).

There are two types of rc files that could be used: System V style or BSD style. Most Linux distributions use System V system (Slackware being the notable exception), so that is what will be covered here. In general, System V rc files are considered more powerful than BSD ones, at the expense of simplicity. BSD rc files are generally stored in the /etc directory, while System V expects to find its files in the /etc/rc.d directory (and any subdirectories from there).

The idea behind System V rc files is that you may want to boot in different ways. For example, you may want to boot into single-user mode to fix a hard drive problem, or you may want regular multi-user console mode, or perhaps you want to boot straight to the X window system. All of these different types of boot sequences can be automated into "run levels"; Red Hat's convention is:

Of course, this convention is arbitrary and other distributions can (and do!) vary. The most common variations are for run level 2 to be the default mode (but have it operate the same as run level 3 in the chart above), and for X to be run level 4 rather than 5. There is also a special run level 6 that isn't really a run level, its a shortcut for rebooting.

Inside the /etc/rc.d/init.d directory are all of the shell scripts that do the actual work. The rc#.d directories simply contain symbolic links to the scripts in the init.d directory.

In the /etc/rc.d directory there is also a file called rc.local, which is the rc file that init will run after everything else is done. You can add simple things to the end of it if you don't want to go through the process of setting up a full script in the /etc/rc.d/init.d directory, but it's not a good idea if you want an easy to understand and consistent boot process.

Knowing a bit about how the System V boot process works now, it's easer to understand how the boot sequence operates: