Lecture # 30 - Init, Systemd, Runlevels, and Target Units

Lecture # 30 - Init, Systemd, Runlevels, and Target Units

Init and Systemd:

Init and Systemd are both process management systems used in Unix-like operating systems to manage the startup, execution, and termination of prosses. However, they have significant differences in terms of design, features, and functionality.

Difference between init and systemd:

InitSystemd
The traditional init system was the original UNIX process initialization system.Systemd is a modern replacement for init that aims to provide more efficient and flexible process management.
It uses a simple and straightforward approach to manage system processes but lack advanced features.It adopts a complex design with a focus on parallelizing service initialization, dependency tracking, and resource management.
The traditional init system initializes processes sequentially, which can lead to slower system boot times due to serial execution.Systemd parallelize the initialize process, allowing services with independent dependencies to start concurrently. This cam significantly improve system boot times.
Dependency management in init is limited, often requiring manual configuration of dependencies between services.Systemd introduces explicit service unit dependencies, making it easier to define and manage dependencies between services. Services can be ordered based on their requirements and relationships.
Logging and debugging information is often sent to various log files scattered across the system.Systemd integrates a centralized logging system called the systemd journal that collects and manages log data from various services and processes in a structured format.
Basic service management is supported, but advanced features like automatic restarts, resource limits, and environment settings may require additional scripts or tools.Systemd provides extensive service management features, including service unit files that define service behavior, resource control, automatic restarts, and more.
Traditional init scripts are often specified to each distribution, leading to inconsistencies between systems.Systemd introduces a standardized unit file format that is used across different distributions, improving compatibility and portability of service configurations.
Traditional init systems are primarily focused on system level initialization and management.Systemd extends its capabilities to user sessions, providing features like user level services and session management.

Runlevels and Target Units:

Runlevels and target units are concepts used for managing the operational states of a unix like operating systems. They differ in their implementation and capabilities.

Runlevels:

A runlevel is an operational level describing the state of init and the whole system with respect to the services it is currently running. Runlevel is a mode or state in which a linux system operates. Runlevels are identified by numbers.

Runlevels are associated with specific directories that contains scripts and configuration files related to the services and processes. These directories are named as, rc0.d , rc2.d , rc3.d , rc4.d , rc5.d , rc6.d , and they are placed in /etc/ directory.

Runlevel Commands:

  • View Current Runlevel of System:

    To view the cuttent runlevel of a system there are two commands

    -> runlevel: To view the current runlevel of a system using runlevel, runlevel is used.

    -> who: To view the current runlevel of a system using who, who -r is used.

  • Change Runlevel:

To change the runlevel init [runlevel] is used. e.g. You want to change the current runlevel to multi user command line interface, use init 3 .

Target Units:

Target units are also operational states, but they offer more flexibility and granularity. Each target unit represents a specific system state, and they can be customized to start or stop specific services and dependencies.

Target Unit Commands:

  • View List of Target Units:

    To view list of target units available on the system,

    systemctl list-units --type=target is used.

  • View Active Target Unit:

    To view the active target unit systemctl get-default is used.

  • Change the Active Target Unit:

    To change the active target unit sudo systemctl set-default [target-unit] is used. After changing the default target unit, it's recommended to reload systemd and then reboot the system to apply the changes.

    -> Reload systemd: sudo systemctl daemon-reload

    -> Reboot System: sudo reboot

    After this the system will be rebooted and CLI will be opened.