Init_systems
Chapter 19: init Systems Comparison
Section titled “Chapter 19: init Systems Comparison”Comprehensive Guide to Linux Init Systems
Section titled “Comprehensive Guide to Linux Init Systems”19.1 Understanding Linux Init Systems
Section titled “19.1 Understanding Linux Init Systems”What is an Init System?
Section titled “What is an Init System?”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 System Comparison
Section titled “Init System Comparison” 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 | || +----------------------------------------------------------+ || |+------------------------------------------------------------------+19.2 SysV init (System V)
Section titled “19.2 SysV init (System V)”Understanding SysV Init
Section titled “Understanding SysV Init”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) | || +----------------------------------------------------------+ || |+------------------------------------------------------------------+Runlevels
Section titled “Runlevels” 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) | || +----------------------------------------------------------+ || |+------------------------------------------------------------------+Managing SysV Services
Section titled “Managing SysV Services”# Check current runlevelrunlevelwho -r
# Change runlevelinit 3 # Text modeinit 5 # Graphicalinit 0 # Haltinit 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 runlevelsupdate-rc.d service defaultsupdate-rc.d service start 20 2 3 4 5 . stop 20 0 1 6 .update-rc.d service enableupdate-rc.d service disable
# chkconfig (RHEL)chkconfig --listchkconfig --level 3 service onchkconfig --add serviceSysV Service Script Example
Section titled “SysV Service Script Example”#!/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 019.3 systemd (Modern Standard)
Section titled “19.3 systemd (Modern Standard)”systemd Overview
Section titled “systemd Overview”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) || |+------------------------------------------------------------------+Key systemd Concepts
Section titled “Key systemd Concepts” 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> | || +----------------------------------------------------------+ || |+------------------------------------------------------------------+systemd Migration from SysV
Section titled “systemd Migration from SysV”# Convert SysV script to systemd service[Unit]Description=My ServiceAfter=network.target
[Service]Type=forkingExecStart=/usr/bin/myservice --daemonPIDFile=/var/run/myservice.pidRestart=on-failure
[Install]WantedBy=multi-user.target
# Enable and startsystemctl daemon-reloadsystemctl enable myservicesystemctl start myservice19.4 OpenRC (Lightweight Alternative)
Section titled “19.4 OpenRC (Lightweight Alternative)”OpenRC Overview
Section titled “OpenRC Overview”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 | || +----------------------------------------------------------+ || |+------------------------------------------------------------------+OpenRC Service Management
Section titled “OpenRC Service Management”# Start/stop servicerc-service service startrc-service service stoprc-service service restartrc-service service status
# Enable/disable servicerc-update add service defaultrc-update del service default
# List servicesrc-statusrc-update show
# Runlevelsrc-update -v showOpenRC Service Script Example
Section titled “OpenRC Service Script Example”#!/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}19.5 runit (Minimal Init)
Section titled “19.5 runit (Minimal Init)”runit Overview
Section titled “runit Overview”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) | || +----------------------------------------------------------+ || |+------------------------------------------------------------------+runit Service Management
Section titled “runit Service Management”# Start/stop servicesv start /service/myservicesv stop /service/myservicesv restart /service/myservicesv status /service/myservice
# Enable serviceln -s /etc/sv/myservice /service/
# Disable servicerm /service/myservice
# View service statussv status /service/myservicerunit Service Script Example
Section titled “runit Service Script Example”#!/bin/shexec 2>&1exec /usr/bin/myservice --daemon#!/bin/shexec chpst -u loguser svlogd -tt /var/log/myservice19.6 Choosing an Init System
Section titled “19.6 Choosing an Init System”Comparison Matrix
Section titled “Comparison Matrix” 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 | || +----------------------------------------------------------+ || |+------------------------------------------------------------------+19.7 Interview Questions
Section titled “19.7 Interview Questions”Basic Questions
Section titled “Basic Questions”-
What is an init system?
- First process (PID 1) that starts all other services
-
What are the main init systems in Linux?
- SysV init, systemd, Upstart, OpenRC, runit
-
What is the difference between SysV and systemd?
- SysV is sequential, systemd is parallel with dependencies
-
What is a runlevel?
- System state defining which services run
-
What is the default runlevel for a Linux server?
- Usually 3 (multi-user text) or 5 (graphical)
Intermediate Questions
Section titled “Intermediate Questions”-
What are systemd targets?
- Groups of units equivalent to SysV runlevels
-
What is the purpose of runlevels 0 and 6?
- 0 = halt, 6 = reboot
-
What is OpenRC?
- Lightweight init system used by Alpine and Gentoo
-
What is socket activation in systemd?
- Service starts on-demand when socket receives connection
-
How do you convert a SysV script to systemd?
- Create .service file in /etc/systemd/system/
Advanced Questions
Section titled “Advanced Questions”-
Why did systemd become the default?
- Parallel startup, dependency management, resource control, logging
-
What is the difference between enable and start?
- enable = start at boot; start = start now
-
How does systemd achieve parallel startup?
- Socket-based activation, dependency resolution, D-Bus
-
What are the advantages of runit over systemd?
- Simplicity, smaller footprint, easier to understand
-
How do you troubleshoot service startup failures?
- journalctl -u service, systemctl status, check logs
Summary
Section titled “Summary” 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/ | || +----------------------------------------------------------+ || |+------------------------------------------------------------------+