Linux › Section F · How user space starts › Topic F.1

Introduction to init

Once the kernel has finished setting up the hardware, it hands control to a single user-space program, init. Everything you later see running, from logging daemons to the login prompt, descends from that one process.

Handing over to user space

When the kernel has initialised memory, devices and its built-in drivers (see "Kernel initialization and parameters"), it starts exactly one user-space program and gives it process ID 1. From then on, user space builds itself up in a rough order: init itself; essential low-level services such as udev and the system logger; network configuration; mid-level services such as cron and printing; and finally login prompts, graphical desktops and application servers such as web servers. Unlike the kernel's start, this stage is modular and easy to change.

What init is responsible for

init is an ordinary program, usually reached through the path /sbin/init, but it holds a special position. It runs as root with PID 1 for as long as the system is up. Its main jobs are to start the essential services in a sensible order, keep track of them, and stop them cleanly at shutdown. It also has a duty no other process has: when a process's parent exits, the orphan is re-parented to PID 1 (or to a nearer process that has registered as a subreaper), and init collects its exit status so it does not linger as a zombie. Because the whole system depends on it, the kernel panics if PID 1 ever exits.

How the kernel finds init

The kernel decides which program becomes PID 1. If the kernel command line contains init=, for example init=/bin/bash, it runs that program and panics if it cannot. Otherwise it tries a default chosen when the kernel was built, if any, and then, in order, /sbin/init, /etc/init, /bin/init and finally /bin/sh. If none can be run, it stops with the message "No working init found". When an initial RAM filesystem is in use, the kernel first runs /init inside it instead (the rdinit= parameter changes that name), and that program later starts the real init on the real root filesystem. "The initial RAM filesystem" covers that hand-off.

A family of init systems

Several programs can play the part of init. systemd is the standard on all major current distributions: Fedora was the first major distribution to adopt it, in 2011, and Red Hat Enterprise Linux 7, Debian 8 and Ubuntu 15.04 followed in about 2014 to 2015. System V init (SysV, pronounced "sys-five"), the traditional design inherited from Unix System V, is now legacy, though you may meet it on old servers and some embedded devices. Upstart was Ubuntu's init from 2006 until it moved to systemd. Other designs survive in niches: OpenRC on Gentoo and Alpine, runit on Void Linux, the small init built into BusyBox on embedded systems, and Android's own init.

Why sequential init fell short

A traditional init runs a series of shell scripts one at a time, each starting one service or configuring one piece of the system. That design is easy to follow and easy to customise, but it has real costs. Boots are slow, because no two scripts run at once even when they are unrelated. Once a daemon is running, init has no reliable record of it: finding its process ID means searching with ps or trusting a PID file such as /var/run/myservice.pid. There is little support for starting a service only when it is first needed, and if a daemon crashes, nothing restarts it.

What modern init systems changed

Newer init systems attack each of those weaknesses. They start services in parallel, limited only by declared dependencies, instead of in a fixed list. They track every process that belongs to a service; systemd does this with the kernel's control groups, which "Control groups and performance tuning" explains. They supervise services and can restart one that fails. They can start a service on demand, when a client first connects to its network socket, much as the old inetd daemon once did. And they replace long scripts with short declarative configuration files. The next topic, "systemd units and unit files", shows what that configuration looks like.

Identifying your init

A few checks tell you which init a machine runs. The command ps -p 1 -o comm= prints the name of PID 1; on a systemd machine it prints systemd, even though ps -p 1 -o args= may still show the path /sbin/init that the kernel ran. On many distributions ls -l /sbin/init shows a symbolic link to the systemd binary. The most reliable test is whether the directory /run/systemd/system exists, which is the check systemd's own tools use. Directories such as /usr/lib/systemd and /etc/systemd prove only that systemd is installed, not that it is running. An /etc/inittab file with no systemd present points to System V init, and an /etc/init directory full of .conf files points to Upstart.

Worked example: three machines

You are handed three machines. 1. On machine A, ps -p 1 -o comm= prints systemd and test -d /run/systemd/system && echo yes prints yes. The init is systemd. 2. On machine B, PID 1 is named init, /run/systemd/system is missing, and /etc/inittab contains a line beginning id:3:initdefault:. That is System V init, booting to runlevel 3 by default. 3. Machine C is a container, and PID 1 is the application itself, say nginx. There is no init at all, so nothing but nginx will reap orphaned children or pass on signals in the way init would. 4. Check: each verdict rests on the running process, not on installed files.

Worked example: an orphaned process

A script runs a slow job in the background and then exits without waiting. 1. While the script runs, ps -o pid,ppid,comm shows the job's parent process ID (PPID) as the script's PID. 2. The script exits. Every process needs a parent, so the kernel re-parents the job to PID 1, or to a subreaper if one sits between them, such as a per-user systemd instance on a desktop. 3. Running ps again would now show a PPID of 1 (or the subreaper's PID). 4. When the job finishes, it becomes a zombie until its new parent calls wait to read its exit status; init does this promptly, so the zombie disappears. 5. Check: without this duty, zombies would pile up.

Common mistakes to avoid

First, thinking init is part of the kernel: it is a user-space program, and the kernel merely starts it. Second, assuming systemd is running because its directories exist; check PID 1 or /run/systemd/system instead. Third, believing init only matters at boot, when in fact it supervises services, adopts orphans and runs the shutdown. Fourth, expecting System V tools and runlevels to behave the same on a systemd machine; treat SysV init as legacy, as the topic "System V runlevels and init" explains. Fifth, putting an application directly at PID 1 in a container without considering who will reap its children.

Key takeaways

The kernel starts one user-space program, init, as PID 1, and all other user processes descend from it. init starts, tracks and stops services, adopts and reaps orphaned processes, and its exit makes the kernel panic. The kernel runs the program named by init= or else tries /sbin/init, /etc/init, /bin/init and /bin/sh; with an initramfs it runs /init first. systemd is the standard on current major distributions; System V init is legacy. Sequential init was slow and could not track or restart services. Identify your init with ps -p 1 -o comm= or by checking for /run/systemd/system.

Flashcards

Flashcard

init

The first user-space program the kernel starts

It always has process ID 1.

Test your understanding

Question 1 of 5

A kernel built with no default init boots without an initramfs and with no init= parameter. Which program does it try first to run as PID 1?

Question 2 of 5

A shell script starts a background job and exits. What normally happens to the still-running job?

Question 3 of 5

Which check most reliably shows that systemd is the running init?

Question 4 of 5

Which was a weakness of traditional sequential init that modern init systems address?

Question 5 of 5

In a container, PID 1 is the nginx server itself rather than an init program. What duty of init may now go undone?

← Previous topicNext topic →