Linux Boot Camp: How Linux Boots (part 1)
Linux Boot Camp Part I: SysV Init
Booting. Sometimes it seems like it takes forever. What's the computer doing all that time? How do you find out?
The Linux boot sequence is surprisingly simple, and the best part is that almost all of it is controlled by shell scripts you can read -- and even edit yourself.
Today's tutorial will take you through boot camp, focusing on the classic "SysV init" methods Linux has always used. Part II will discoss some of the changes on systems like Ubuntu as they gradually migrate to a new "Upstart" model.
First, Grub loads the kernel
When you power your machine on, a program called grub takes control. It reads the first sector of your disk, figures out where to look for the operating system, and starts the kernel running. Then it gets out of the way and the kernel takes over: usually you'll see a brief message like "OK, loading the kernel".
Do those messages fly by too fast on the graphical boot screen? If you want more information while you're booting, you can disable graphical booting by editing the kernel line in grub and removing quiet.
The kernel takes anywhere from a few seconds to nearly a minute to load, depending on your hardware and how many modules it has to load. Recent distros have improved this a lot, so look for faster kernel load times in the future.
init takes over
Once the kernel is finished loading, it passes control to a process called init. init is the master process from this point until you finally shut the machine down. It's running on your system right now: run ps aux and you'll see that it's process 1, at the top of the list.
There are two types of inits. The classic "SysV" model dates back to the days of AT&T System V Unix, long before Linux even existed -- but it's still used in every Linux distro today. The newer "Upstart" model is being gradually phased in by Ubuntu and a few others -- but even after years of migration, there's still a lot of SysV in the boot process.
A SysV-init Linux system has several possible runlevels. Generally you'll boot into runlevel 2 (Debian/Ubuntu), 3 or 5 (most others). Some distros (Redhat/Fedora) use runlevel 3 to boot into a text console and 5 for a full graphical desktop. You can find your current runlevel by typing runlevel at the command line.
There are a few special runlevels. 0 means halt, while 1 and S both mean to boot single-user, with only a root shell, no window system and fewer daemon processes -- useful for debugging if things go wrong.
In theory, you can boot into other runlevels by adding the runlevel number to the end of the kernel line in grub. In practice, that doesn't always work, but adding "single" should always get you into single-user mode.
The default runlevel is traditionally set in a file called /etc/inittab. Some distros change that; for instance, Ubuntu sets it in two places, /etc/init/rc-sysinit.conf and /etc/event.d/rc-default.
- 1Linux Top 3: Fedora 24, Peppermint 7 and Solus 1.2
- 2Linux Top 3: Alpine Linux 3.4, deepin 15.2 and Linux Lite 3.0
- 3Linux 4.7 Set to Boost Live Patching, Security and Power Management
- 4Linux 4.6 Charred Weasel adds USB 3.1 Support
- 5Linux Top 3: OpenIndiana 2016.04, Ubuntu 16.04 and Debian's New Leader