Run levels in Linux are predefined states of a system that determine which services and processes are running. They control the system’s operational state and help administrators configure the system to start in specific modes such as multi-user mode, single-user mode, or graphical interface mode.
1. What are Run Levels?
Run levels are numbered states that define the set of services or processes that are active on a Linux system. Different run levels are used for various tasks such as maintenance, troubleshooting, or full operational mode.
- Run levels range from 0 to 6 in traditional SysVinit systems.
- Each run level has a specific purpose.
2. Traditional SysVinit Run Levels
Run Level | Description |
0 | System halt (shutdown mode). |
1 | Single-user mode (maintenance). |
2 | Multi-user mode without networking. |
3 | Full multi-user mode with networking (text-based). |
4 | Undefined (user-defined/custom). |
5 | Multi-user mode with graphical interface (GUI). |
6 | Reboot. |
Details of Key Run Levels
- Run Level 0 (Halt)
- Shuts down the system.
- Activated with:
init 0
- Example use case: Powering off the machine.
- Run Level 1 (Single-User Mode)
- Minimal services started.
- No networking, and only the root user can log in.
- Activated with:
init 1
- Example use case: Troubleshooting system issues.
- Run Level 3 (Multi-User Mode with Networking)
- All system services are started, except the graphical interface.
- Activated with:
init 3
- Example use case: Server operations.
- Run Level 5 (Multi-User Mode with GUI)
- Full multi-user mode with a graphical desktop environment.
- Activated with:
init 5
- Example use case: Desktop workstations.
- Run Level 6 (Reboot)
- Restarts the system.
- Activated with:
init 6
3. Managing Run Levels
3.1 Checking the Current Run Level
- Command:
who -r
3.2 Changing Run Levels
- Command:
sudo init <runlevel>
- Example:
sudo init 3
3.3 Setting Default Run Level
- Default run level is defined in the /etc/inittab file (SysVinit systems):
id:3:initdefault:
4. Systemd Targets (Modern Linux Systems)
Most modern Linux distributions have replaced SysVinit with systemd, which uses targets instead of run levels. Targets provide more flexibility and modularity.
Systemd Targets and Their Equivalents
SysVinit Run Level | Systemd Target | Description |
0 | poweroff.target | Shutdown the system. |
1 | rescue.target | Single-user mode. |
3 | multi-user.target | Full multi-user mode (text-based). |
5 | graphical.target | Multi-user mode with GUI. |
6 | reboot.target | Reboot the system. |
4.1 Checking the Current Target
- Command:
systemctl get-default
4.2 Changing the Current Target
- Command:
sudo systemctl isolate <target>
- Example:
sudo systemctl isolate multi-user.target
4.3 Setting the Default Target
- Command:
sudo systemctl set-default <target>
- Example:
sudo systemctl set-default graphical.target
5. Best Practices
- Use Targets Over Run Levels:
- For modern systems with systemd, use targets as they offer better flexibility and control.
- Set Appropriate Default Mode:
- For servers, set multi-user.target to save resources by avoiding GUI.
- For desktops, use graphical.target.
- Secure Single-User Mode:
- Set a password for the root account to prevent unauthorized access in rescue.target.
- Monitor and Troubleshoot Services:
- Use systemd’s robust logging and service management tools for diagnosing issues.
6. Key Commands Summary
Command | Description |
who -r | Check current run level. |
sudo init <runlevel> | Switch to a specific run level. |
systemctl get-default | View the default systemd target. |
sudo systemctl isolate <target> | Change to a specific target. |
sudo systemctl set-default <target> | Set the default systemd target. |
7. Conclusion
Run levels in Linux (or their modern equivalents, systemd targets) are crucial for controlling the system’s operational state. They provide flexibility in configuring the system for different use cases such as troubleshooting, server management, or desktop operations. While traditional run levels are still relevant for legacy systems, most modern Linux distributions rely on systemd targets for better performance and functionality.