Linux is widely appreciated for its robust and flexible architecture, with process and service management being core components. Two critical concepts in this domain are session targets in systemd and run levels in System V init. While they share similar goals, their implementations differ significantly. This blog post dives into these concepts, highlighting their roles, differences, and how they interact with Linux systems.
What Are Run Levels in System V Init?
In the traditional System V init system, run levels define the state of the machine, specifying which services and processes should be running. These run levels are numbered from 0 to 6:
- 0: Shutdown the system.
- 1: Single-user mode (maintenance mode).
- 2: Multi-user mode without network services (varies across distributions).
- 3: Full multi-user mode with networking.
- 4: Undefined or user-definable (rarely used).
- 5: Multi-user mode with networking and graphical interface.
- 6: Reboot the system.
You can check the current run level using the runlevel
command, which outputs the previous and current levels (e.g., N 3
). To change the run level, the init
command is used (e.g., sudo init 5
).
Systemd Session Targets
Systemd, the modern initialization system replacing System V init in most Linux distributions, introduces the concept of targets. Targets serve the same purpose as run levels: they define the state of the system. However, systemd’s design is more flexible and granular.
Common targets include:
- poweroff.target: Equivalent to run level 0.
- rescue.target: Similar to run level 1.
- multi-user.target: Maps to run level 3.
- graphical.target: Corresponds to run level 5.
- reboot.target: Equivalent to run level 6.
Systemd also introduces special targets like default.target
, which is a symlink to the default session target. You can view or set the default target using:
bashsystemctl get-default
systemctl set-default graphical.target
To switch targets temporarily, use:
bashsudo systemctl isolate multi-user.target
Differences Between Run Levels and Targets
-
Flexibility:
- Run levels are numeric and limited to predefined states.
- Targets are named, descriptive, and allow custom states via additional targets.
-
Dependencies:
- Run levels have fixed service mappings.
- Targets support dependency trees, enabling complex service relationships and fine-grained control.
-
Parallelization:
- Systemd targets enable parallel service startup, improving boot times.
- System V init services start sequentially, making it slower.
Migrating from System V Init to Systemd
For users transitioning to systemd, the mapping between run levels and targets simplifies the process:
Run Level | System V State | systemd Target |
---|---|---|
0 | Halt | poweroff.target |
1 | Single-user mode | rescue.target |
2, 3 | Multi-user mode | multi-user.target |
5 | Graphical interface | graphical.target |
6 | Reboot | reboot.target |
Systemd also offers legacy compatibility. Commands like runlevel
and telinit
still work under systemd, ensuring a smoother transition.
Conclusion
Whether you’re managing services with System V init or systemd, understanding run levels and targets is essential for Linux system administration. While System V init offers simplicity, systemd’s targets bring powerful features like parallel service startup, dependencies, and customization, making it the preferred choice for modern Linux systems. Knowing these fundamentals will help you optimize and troubleshoot your system with confidence.