init

The init system is the first process started by the Linux kernel after it has loaded and initialized. It is responsible for initializing the system, starting essential services, and managing system processes. The init process is the parent of all other processes on a Linux system, and it runs with a process ID (PID) of 1.

Overview of Init Systems

Types of Init Systems

  1. SysVinit: Traditional init system used in older Unix and Linux distributions.

  2. Systemd: Modern init system that has become the standard for many Linux distributions.

  3. Upstart: Event-based init system developed by Ubuntu, now mostly replaced by systemd.

  4. OpenRC: Dependency-based init system used by Gentoo and Alpine Linux.

SysVinit

Key Features

  • Scripts-Based: Uses shell scripts to start and stop services.

  • Runlevels: Defines different states of the system, such as single-user mode, multi-user mode, and reboot.

  • Initialization Scripts: Located in /etc/init.d/ and linked to corresponding runlevel directories (/etc/rc.d/ or /etc/rc?.d/).

Runlevels

  • 0: Halt

  • 1: Single-user mode

  • 2: Multi-user mode without networking

  • 3: Multi-user mode with networking

  • 4: Undefined (user-definable)

  • 5: Multi-user mode with graphical interface

  • 6: Reboot

Example: Managing Services with SysVinit

  • Starting a Service:

    sudo /etc/init.d/apache2 start
  • Stopping a Service:

    sudo /etc/init.d/apache2 stop
  • Restarting a Service:

    sudo /etc/init.d/apache2 restart

Systemd

Key Features

  • Unit Files: Uses unit files to define services, sockets, devices, mounts, and other system resources.

  • Parallel Startup: Capable of starting services in parallel, leading to faster boot times.

  • Dependency Management: Handles dependencies between services to ensure they start in the correct order.

  • Journal: Provides a logging system called journal for collecting and managing logs.

Common Systemd Commands

  • Starting a Service:

    sudo systemctl start apache2
  • Stopping a Service:

    sudo systemctl stop apache2
  • Restarting a Service:

    sudo systemctl restart apache2
  • Enabling a Service (to start at boot):

    sudo systemctl enable apache2
  • Disabling a Service:

    sudo systemctl disable apache2
  • Checking Status of a Service:

    sudo systemctl status apache2

Example Unit File

A typical unit file for a service, located in /etc/systemd/system/ or /lib/systemd/system/, might look like this:

[Unit]
Description=Apache Web Server
After=network.target

[Service]
ExecStart=/usr/sbin/apachectl start
ExecStop=/usr/sbin/apachectl stop
ExecReload=/usr/sbin/apachectl reload
Type=forking

[Install]
WantedBy=multi-user.target

Upstart

Key Features

  • Event-Based: Uses events to start and stop services.

  • Backward Compatibility: Can run SysVinit scripts.

  • Configuration Files: Uses configuration files in /etc/init/.

Example Upstart Script

An example Upstart script, located in /etc/init/, might look like this:

description "Example Service"
start on filesystem
stop on runlevel [!2345]

script
    exec /usr/bin/example-service
end script

OpenRC

Key Features

  • Dependency-Based: Manages service dependencies explicitly.

  • Shell Script-Based: Uses shell scripts for service management.

  • Compatibility: Compatible with traditional init systems and scripts.

Example: Managing Services with OpenRC

  • Starting a Service:

    sudo rc-service apache2 start
  • Stopping a Service:

    sudo rc-service apache2 stop
  • Restarting a Service:

    sudo rc-service apache2 restart

Conclusion

The init system is a crucial component of the Linux boot process, responsible for starting and managing system services and processes. Understanding different init systems (SysVinit, systemd, Upstart, OpenRC) and their respective commands is essential for system administration and troubleshooting.

Last updated