Skip to content

Init_systems


The init (initialization) system is the first process started by the Linux kernel (PID 1). It is responsible for starting and managing all other processes, services, and the overall system state. Understanding the init system is crucial for Linux system administration.

Linux Boot Process
+------------------------------------------------------------------+
| |
| 1. BIOS/UEFI - Power on self test |
| |
| 2. Bootloader (GRUB2) - Loads kernel |
| |
| 3. Kernel - Initializes hardware, starts PID 1 |
| |
| 4. init (PID 1) - Starts all services |
| | |
| v |
| 5. Services start in order (based on init system) |
| |
| 6. Login prompt / Graphical interface |
| |
| Init System Responsibilities: |
| +----------------------------------------------------------+ |
| | • Start essential system services | |
| | • Manage service dependencies | |
| | • Handle runlevels/targets | |
| | • Provide service management (start/stop/restart) | |
| | • Handle logging | |
| | • Manage user sessions | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
Init Systems Overview
+------------------------------------------------------------------+
| |
| SysV init (System V): |
| +----------------------------------------------------------+ |
| | • Original Unix System V init | |
| | • Sequential startup (scripts in /etc/init.d) | |
| | • Runlevels 0-6 | |
| | • No dependency management | |
| | • Simple but slow | |
| +----------------------------------------------------------+ |
| |
| systemd: |
| +----------------------------------------------------------+ |
| | • Modern default for most distributions | |
| | • Parallel startup (socket-based) | |
| | • Targets instead of runlevels | |
| | • Dependency management | |
| | • cgroups for resource control | |
| | • Journal for logging | |
| +----------------------------------------------------------+ |
| |
| Upstart: |
| +----------------------------------------------------------+ |
| | • Event-based init system | |
| | • Developed by Ubuntu | |
| | • Replaced by systemd in Ubuntu 16.04 | |
| | • Job definitions in /etc/init/ | |
| +----------------------------------------------------------+ |
| |
| OpenRC: |
| +----------------------------------------------------------+ |
| | • BSD-style init | |
| | • Used by Alpine, Gentoo, Void | |
| | • Simple and lightweight | |
| | • Dependency-based service management | |
| +----------------------------------------------------------+ |
| |
| runit: |
| +----------------------------------------------------------+ |
| | • Minimal init system | |
| | • Used by Void Linux | |
| | • Process supervision | |
| | • Three-stage: run, finish, check | |
| +----------------------------------------------------------+ |
| |
| OpenRC vs systemd: |
| +----------------------------------------------------------+ |
| | Feature | OpenRC | systemd | |
| | ---------------|--------------|----------------------------| |
| | Parallel start | Limited | Yes | |
| | Dependencies | Yes | Yes | |
| | cgroups | No | Yes | |
| | Logging | syslog | journal | |
| | Service files | Shell scripts| Unit files | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

SysV init is the traditional init system used in Unix System V. It uses shell scripts and runlevels to manage services.

SysV init Process Flow
+------------------------------------------------------------------+
| |
| /sbin/init (PID 1) |
| | |
| /etc/init.d/rc 2 |
| | |
| Scripts in /etc/rc2.d/ (runlevel 2) |
| | |
| S01networking → S02sshd → S03cron → ... |
| |
| Runlevel Scripts: |
| +----------------------------------------------------------+ |
| | S## = Start script (S = Start) | |
| | K## = Kill script (K = Kill) | |
| | ## = Order (lower = earlier) | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
SysV Runlevels
+------------------------------------------------------------------+
| |
| Runlevel | Description | Usage |
| ---------|----------------------------------|------------------|
| 0 | Halt | Shutdown |
| 1 | Single user mode | Recovery |
| 2 | Multi-user (no network) | Rarely used |
| 3 | Multi-user (text mode) | Server mode |
| 4 | Multi-user (custom) | Unused |
| 5 | Multi-user (graphical) | Desktop mode |
| 6 | Reboot | Reboot |
| |
| Default Runlevels by Distribution: |
| +----------------------------------------------------------+ |
| | Debian/Ubuntu: 2 (multi-user) | |
| | RHEL/CentOS: 3 (text) or 5 (graphical) | |
| | Arch Linux: systemd (no traditional runlevels) | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
Terminal window
# Check current runlevel
runlevel
who -r
# Change runlevel
init 3 # Text mode
init 5 # Graphical
init 0 # Halt
init 6 # Reboot
telinit 3
# Start/stop service manually
/etc/init.d/service start
/etc/init.d/service stop
/etc/init.d/service restart
/etc/init.d/service status
# Update runlevels
update-rc.d service defaults
update-rc.d service start 20 2 3 4 5 . stop 20 0 1 6 .
update-rc.d service enable
update-rc.d service disable
# chkconfig (RHEL)
chkconfig --list
chkconfig --level 3 service on
chkconfig --add service
#!/bin/bash
### BEGIN INIT INFO
# Provides: myservice
# Required-Start: $network $remote_fs $syslog
# Required-Stop: $network $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: My Service
### END INIT INFO
case "$1" in
start)
echo "Starting myservice..."
/usr/bin/myservice &
;;
stop)
echo "Stopping myservice..."
kill $(pidof myservice)
;;
restart)
$0 stop
$0 start
;;
status)
if pgrep myservice > /dev/null; then
echo "myservice is running"
else
echo "myservice is not running"
fi
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit 0

systemd is the current standard init system for most Linux distributions. It provides parallel service startup, dependency management, and comprehensive logging.

systemd Boot Process
+------------------------------------------------------------------+
| |
| systemd (PID 1) |
| | |
| default.target (usually graphical.target or multi-user.target) |
| | |
| basic.target |
| | |
| sysinit.target |
| | |
| local-fs.target @swap.target @remote-fs.target |
| |
| Services start in parallel (dependencies resolved) |
| |
+------------------------------------------------------------------+
systemd Key Concepts
+------------------------------------------------------------------+
| |
| Units: |
| +----------------------------------------------------------+ |
| | .service | Running services | |
| | .socket | Unix sockets for activation | |
| | .target | Group of units (like runlevels) | |
| | .mount | Filesystem mount points | |
| | .timer | Scheduled tasks | |
| | .path | Path-based activation | |
| +----------------------------------------------------------+ |
| |
| Targets (Runlevels): |
| +----------------------------------------------------------+ |
| | graphical.target | Multi-user with GUI (runlevel 5) | |
| | multi-user.target| Multi-user text (runlevel 3) | |
| | rescue.target | Single user (runlevel 1) | |
| | emergency.target| Emergency shell | |
| | shutdown.target | System shutdown | |
| | reboot.target | System reboot | |
| +----------------------------------------------------------+ |
| |
| Commands: |
| +----------------------------------------------------------+ |
| | systemctl start/stop/restart <service> | |
| | systemctl enable/disable <service> | |
| | systemctl status <service> | |
| | systemctl list-units --type=service | |
| | journalctl -u <service> | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
/etc/systemd/system/myservice.service
# Convert SysV script to systemd service
[Unit]
Description=My Service
After=network.target
[Service]
Type=forking
ExecStart=/usr/bin/myservice --daemon
PIDFile=/var/run/myservice.pid
Restart=on-failure
[Install]
WantedBy=multi-user.target
# Enable and start
systemctl daemon-reload
systemctl enable myservice
systemctl start myservice

OpenRC is a dependency-based init system used by Alpine Linux, Gentoo, and Void Linux. It’s lighter than systemd while providing dependency management.

OpenRC Architecture
+------------------------------------------------------------------+
| |
| /sbin/init (OpenRC) |
| | |
| /etc/rc.conf |
| | |
| /etc/init.d/ (service scripts) |
| | |
| /etc/runlevels/ |
| | |
| Services start based on dependencies |
| |
| Service Dependencies: |
| +----------------------------------------------------------+ |
| | need net | Requires network | |
| | need net.eth0 | Requires specific interface | |
| | after firewall | Starts after firewall | |
| | before sshd | Starts before sshd | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
Terminal window
# Start/stop service
rc-service service start
rc-service service stop
rc-service service restart
rc-service service status
# Enable/disable service
rc-update add service default
rc-update del service default
# List services
rc-status
rc-update show
# Runlevels
rc-update -v show
#!/sbin/openrc-run
name="myservice"
command="/usr/bin/myservice"
command_args="--daemon"
pidfile="/var/run/${RC_SVCNAME}.pid"
depend() {
need net
after firewall
}
start_pre() {
checkpath --directory --owner root:root --mode 0755 /var/run/myservice
}

runit is an extremely minimal init system used by Void Linux. It provides process supervision and simplicity.

runit Architecture
+------------------------------------------------------------------+
| |
| /sbin/runit (PID 1) |
| | |
| /etc/runit/1 |
| | |
| /etc/runit/2 (normal operation) |
| | |
| /etc/runit/3 (shutdown) |
| |
| Service Supervision: |
| +----------------------------------------------------------+ |
| | /service/ directory | |
| | /service/myservice/run (start script) | |
| | /service/myservice/log/run (logging) | |
| | /service/myservice/finish (stop script) | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+
Terminal window
# Start/stop service
sv start /service/myservice
sv stop /service/myservice
sv restart /service/myservice
sv status /service/myservice
# Enable service
ln -s /etc/sv/myservice /service/
# Disable service
rm /service/myservice
# View service status
sv status /service/myservice
/etc/sv/myservice/run
#!/bin/sh
exec 2>&1
exec /usr/bin/myservice --daemon
/etc/sv/myservice/log/run
#!/bin/sh
exec chpst -u loguser svlogd -tt /var/log/myservice

Init System Selection Guide
+------------------------------------------------------------------+
| |
| Use systemd if: |
| +----------------------------------------------------------+ |
| | • Using RHEL, CentOS, Fedora, Ubuntu 16.04+ | |
| | • Need advanced features (cgroups, socket activation) | |
| | • Want parallel startup for faster boot | |
| | • Need resource control for containers | |
| +----------------------------------------------------------+ |
| |
| Use OpenRC if: |
| +----------------------------------------------------------+ |
| | • Using Alpine Linux, Gentoo | |
| | • Want lightweight system | |
| | • Prefer simpler configuration | |
| | • Need dependency management without complexity | |
| +----------------------------------------------------------+ |
| |
| Use runit if: |
| +----------------------------------------------------------+ |
| | • Using Void Linux | |
| | • Want absolute minimal footprint | |
| | • Prefer simple process supervision | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+

  1. What is an init system?

    • First process (PID 1) that starts all other services
  2. What are the main init systems in Linux?

    • SysV init, systemd, Upstart, OpenRC, runit
  3. What is the difference between SysV and systemd?

    • SysV is sequential, systemd is parallel with dependencies
  4. What is a runlevel?

    • System state defining which services run
  5. What is the default runlevel for a Linux server?

    • Usually 3 (multi-user text) or 5 (graphical)
  1. What are systemd targets?

    • Groups of units equivalent to SysV runlevels
  2. What is the purpose of runlevels 0 and 6?

    • 0 = halt, 6 = reboot
  3. What is OpenRC?

    • Lightweight init system used by Alpine and Gentoo
  4. What is socket activation in systemd?

    • Service starts on-demand when socket receives connection
  5. How do you convert a SysV script to systemd?

    • Create .service file in /etc/systemd/system/
  1. Why did systemd become the default?

    • Parallel startup, dependency management, resource control, logging
  2. What is the difference between enable and start?

    • enable = start at boot; start = start now
  3. How does systemd achieve parallel startup?

    • Socket-based activation, dependency resolution, D-Bus
  4. What are the advantages of runit over systemd?

    • Simplicity, smaller footprint, easier to understand
  5. How do you troubleshoot service startup failures?

    • journalctl -u service, systemctl status, check logs

Quick Reference
+------------------------------------------------------------------+
| |
| SysV Commands: |
| +----------------------------------------------------------+ |
| | /etc/init.d/service start|stop|restart | |
| | runlevel | Current runlevel | |
| | init 3 | Change runlevel | |
| | update-rc.d | Enable/disable (Debian) | |
| | chkconfig | Enable/disable (RHEL) | |
| +----------------------------------------------------------+ |
| |
| systemd Commands: |
| +----------------------------------------------------------+ |
| | systemctl start/stop/restart service | |
| | systemctl enable/disable service | |
| | systemctl status service | |
| | journalctl -u service | |
| | systemctl daemon-reload | |
| +----------------------------------------------------------+ |
| |
| OpenRC Commands: |
| +----------------------------------------------------------+ |
| | rc-service service start | |
| | rc-update add service default | |
| | rc-status | |
| +----------------------------------------------------------+ |
| |
| runit Commands: |
| +----------------------------------------------------------+ |
| | sv start/stop/service | |
| | ln -s /etc/sv/service /service/ | |
| +----------------------------------------------------------+ |
| |
+------------------------------------------------------------------+