In Linux-based operating systems, runlevels are predefined states that determine the system’s behavior and services that are running. Each runlevel has a specific purpose, such as single-user mode, multi-user mode, or graphical interface mode. However, some runlevels are user-definable, allowing administrators to configure custom behaviors.
This topic explores which runlevel number is user-definable, how runlevels work, and how they can be modified to suit specific needs.
Understanding Runlevels in Linux
A runlevel is a mode of operation in a Unix-based system that defines which services and processes are active. The concept of runlevels originated from the System V (SysV) init system, which was widely used before the adoption of systemd in modern Linux distributions.
Standard Runlevels in SysV Init
The traditional runlevels in Linux are numbered from 0 to 6, each with a predefined function:
- Runlevel 0 – Halt (shuts down the system)
- Runlevel 1 – Single-user mode (maintenance mode)
- Runlevel 2 – Multi-user mode (without networking)
- Runlevel 3 – Full multi-user mode (with networking, but no graphical interface)
- Runlevel 4 – User-definable runlevel
- Runlevel 5 – Multi-user mode with graphical interface
- Runlevel 6 – Reboot (restarts the system)
Among these, runlevel 4 is the user-definable runlevel. Unlike other runlevels with fixed purposes, runlevel 4 does not have a predefined function, making it a flexible option for system administrators to configure as needed.
Why Is Runlevel 4 User-Definable?
The reason runlevel 4 is designated as user-definable is that Linux distributions generally do not assign it a specific function. While runlevels 0, 1, 2, 3, 5, and 6 serve specific roles, runlevel 4 is left open for customization.
Use Cases for Runlevel 4
Since runlevel 4 is customizable, system administrators can configure it for various purposes, such as:
- Custom Maintenance Mode – A modified version of single-user mode with additional services.
- Alternative Multi-User Mode – A specialized configuration for a server environment.
- Testing and Debugging – A separate runlevel for troubleshooting system issues.
- Secure Environment – A restricted mode for running only essential services.
How to Configure Runlevel 4
Checking the Current Runlevel
To check which runlevel the system is currently running on, use the following command:
runlevel
This will output two numbers. The first number represents the previous runlevel, while the second indicates the current runlevel.
Changing to Runlevel 4
To manually switch to runlevel 4, use the command:
init 4
Or, on some systems:
telinit 4
This will transition the system to runlevel 4, provided that it has been configured.
Customizing Runlevel 4
To modify runlevel 4, follow these steps:
- Create or Modify the Init Script
- The configuration for each runlevel is stored in
/etc/inittab
in SysV init systems. - Edit the file using a text editor:
sudo nano /etc/inittab
- Locate the section for runlevel 4 and modify the associated script.
- The configuration for each runlevel is stored in
- Set Up Startup Scripts
- Runlevels are managed by scripts stored in
/etc/rc.d/
or/etc/init.d/
. - The directory
/etc/rc.d/rc4.d/
contains scripts for runlevel 4. - You can add, remove, or modify scripts in this directory to define custom services.
- Runlevels are managed by scripts stored in
- Enable Custom Services
- Use the
chkconfig
orupdate-rc.d
command to enable or disable services in runlevel 4. - Example:
sudo chkconfig --level 4 custom-service on
- This ensures the service starts automatically when runlevel 4 is activated.
- Use the
Runlevels in Modern Linux Systems (systemd)
With the adoption of systemd, traditional runlevels have been replaced by target units. The equivalent of runlevel 4 in systemd is the multi-user.target or a custom-defined target.
Checking the Current Target
To check the current target in a systemd-based system, use:
systemctl get-default
Changing to a Custom Target
To switch to a custom-defined target (equivalent to runlevel 4), use:
systemctl isolate custom.target
Creating a Custom Target
To define a custom target, create a .target
file in /etc/systemd/system/
. For example:
sudo nano /etc/systemd/system/mycustom.target
Then, add the following content:
[Unit]Description=My Custom TargetRequires=multi-user.targetAllowIsolate=yes
Save the file and reload systemd:
sudo systemctl daemon-reload
To set it as the default boot target:
sudo systemctl set-default mycustom.target
The user-definable runlevel in Linux systems is runlevel 4, allowing system administrators to configure a custom environment based on their needs. Whether used for specialized server configurations, security measures, or testing, runlevel 4 provides flexibility in traditional SysV init systems.
In modern Linux distributions using systemd, the concept of runlevels has evolved into targets, but the principle remains the same-users can create custom boot configurations to control system behavior.
Understanding runlevels and targets is crucial for effective system administration, ensuring that Linux systems are optimized for specific use cases.