Linux initialization.

The System V to-the-point guide and cheat sheet

System V is the classic Linux init system, this article is a to-the-point guide and cheatsheet intended to help you quick access to all System V commands, directories and tools.

Introduction

System V init, commonly referred to as SysVinit, is a traditional initialization system for Unix-like operating systems. It manages the system’s boot process and service control using a series of scripts executed in a specific order. Although largely replaced by systemd in modern Linux distributions, understanding SysVinit remains valuable for managing older systems or troubleshooting legacy environments.

SysVinit is a straightforward and reliable system initialization tool, ideal for legacy systems or environments with minimal requirements. Understanding its structure, runlevel concept, and service management is crucial for maintaining older Unix/Linux systems. As systemd becomes the standard, familiarity with both systems helps bridge the gap between legacy and modern environments.

For the history of System V and its evolution along with the history and evolution of other init systems, see the following article: An Introduction to Linux Init Systems and their evolution through time.


Runlevels Overview

SysVinit uses runlevels to define system states and relies on scripts located in /etc/init.d and symbolic links in runlevel directories such as /etc/rc.d* for service management.

Runlevels represent different system states, each defining a specific set of services or tasks:

  • 0: Halt the system (shutdown).
  • 1: Single-user mode (maintenance).
  • 2: Multi-user mode without networking (often unused).
  • 3: Full multi-user mode with networking (common for servers).
  • 4: Undefined/custom.
  • 5: Multi-user mode with graphical UI (default on desktops).
  • 6: Reboot.

Checking and Changing Runlevels

Check the current runlevel:

who -r

Change the runlevel:

init <runlevel>

Example:

init 3  # Switch to multi-user mode without GUI

SysVinit Structure and Files

Key Directories

  • /etc/init.d: Contains service scripts.
  • /etc/rc*.d: Runlevel-specific directories with symbolic links to init scripts.

Init Script Structure

Init scripts in /etc/init.d control services and follow a basic structure:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          my-service
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: My Custom Service
# Description:       A longer description of my custom service.
### END INIT INFO

Enable/Disable Services

Symbolic links in the /etc/rc.d* directories control which services run at each runlevel:

  • Sxxservice: Starts the service.
  • Kxxservice: Stops the service.

Enable a service:

ln -s /etc/init.d/my-service /etc/rc3.d/S99my-service

Disable a service:

rm /etc/rc3.d/S99my-service

Managing Services with Service Command

The service command simplifies interaction with init scripts.

Start, stop, and restart services:

sudo service <service> start    # Start a service
sudo service <service> stop     # Stop a service
sudo service <service> restart  # Restart a service

Check service status:

sudo service <service> status

Example:

sudo service apache2 start

Boot Sequence and Troubleshooting

Boot Sequence

  1. BIOS/UEFI Initialization: Hardware initialization.
  2. Bootloader: Loads the kernel.
  3. Kernel Initialization: Mounts the root filesystem and starts /sbin/init.
  4. Init Process: Executes scripts in the default runlevel directory.

Debugging Boot Issues

View boot logs:

dmesg

Check service logs:

cat /var/log/syslog

Manually start troubleshooting mode (single-user):
At the bootloader, add single to the kernel parameters.


Adding Custom Services

  1. Create the init script:
    Save it in /etc/init.d/my-service and make it executable:
chmod +x /etc/init.d/my-service

Add symbolic links:

ln -s /etc/init.d/my-service /etc/rc3.d/S99my-service
ln -s /etc/init.d/my-service /etc/rc0.d/K01my-service

Test the script:

sudo service my-service start

Switching Between SysVinit and Systemd

For distributions supporting both systems, you can switch between them:

Systemd vs SysV.
Systemd vs SysV.

To use SysVinit:

  1. Install SysVinit if necessary:
sudo apt install sysvinit-core

Reboot the system.

To revert to systemd:

  1. Install systemd:
sudo apt install systemd

Reboot the system.


Advantages and Limitations

Advantages

  • Simple structure, easy to understand.
  • Supported by older distributions and Unix systems.
  • Lightweight and minimal dependencies.

Limitations

  • Sequential startup limits speed compared to parallel methods in systemd.
  • Lacks advanced features like dependency resolution and timer units.
  • Complex configurations for large-scale systems.

The Long Legacy of System V

System V certainly left its mark in the history of Linux. You can read about that rich history following this link: The history and evolution of Linux distros, or more specifically, the history of Linux commands following this link: The history and evolution of Unix, GNU and Linux commands.

Leave a Reply

Your email address will not be published. Required fields are marked *