
Linux runlevels distinguish between operating states of the system. Changing the runlevel will change the services that are running. You can customize the runlevels so that each tier starts with a specific set of services.
Standard run levels
The Linux kernel defines seven standard runlevels. Individual operating system distributions can customize these runlevels or add additional ones. Users are also free to create and modify runlevels.
- Run level 0 – Without activity, equivalent to off. The system is ready for a physical loss of power.
- Run level 1 – Running in single user mode (rescue).
- Run level 2 – Running in multi-user mode without networking or graphics.
- Run level 3 – Multi-user mode with the addition of network support.
- Run level 4 – Left for the users / distributions define it.
- Run level 5 – Multiple user mode with a graphical user interface.
- Run level 6 – System reboot mode.
Most Linux systems will boot to the run level 5. This is the runlevel you are probably most familiar with. The widest range of services is available, including a display server and a full network stack. If you are on a headless server and without a screen stack, it may be at run level 3.
Some single-user systems will boot to the run level 1. You will also find this runlevel if you use a recovery mode to rescue your system. This allows you to access the shell without logging in as a normal user.
Runlevels 2, 3 and 4 may vary by distribution. Some distributions will create a clear distinction between each runlevel; in others, all three can have the same effect. Generally, you can expect any of these three to provide you with a single user text shell with available networks.
Your system can only be in one runlevel at a time. Only one runlevel will be selected after boot. You won't transition runlevels unless you explicitly initiate a change. In general, the only run-level transitions that occur are from levels 1-5 to the level 0 O 6, when your system shuts down or reboots.
Higher run levels indicate increasing levels of available functionality. This convention is followed by all distributions. A low execution level generally means that only a subset of the system services are running.
Run levels and startup services
Startup services are coupled to runlevels. Linux traditionally adds startup services to runlevels. When your system enters a run level, all services associated with that level will be started.
How services are actually handled depends on the service manager you are using. Individual distributions are shipped with different service managers. The original service manager, init
, define your services within /etc/init.d
. The scripts created here are symbolically linked to /etc/rc
where they are sorted by runlevel:
/etc/rc0.d
– Scripts placed here are executed by runlevel 0 …/etc/rc1.d
– Scripts for runlevel 1…/etc/rc2.d
-… etc.
To add new startup services, create or edit a script in /etc/init.d
:
ms:345:respawn:/usr/bin/service_executable
Next, use chkconfig
to enable the service:
sudo chkconfig service_executable on
the 345:respawn
on the dash line instructs init
to run service_executable
when the system enters run levels 3, 4 O 5.
Many newer distributions have replaced init
with more modern alternatives. These can provide a higher level abstraction on runlevels and the boot system.
Most Linux distributions, including Debian, Ubuntu, CentOS, Arch y Red Hat, now they use systemd
. This does not refer to runlevels directly; instead, they turn into “objectives” who are identified by name. While Linux can only be on one runlevel at a time, systemd
supports multiple active targets simultaneously. The mapping between runlevels and targets is only approximate; it is provided for compatibility purposes.
The special one default
target defines what the system will start on. the default
the objective is usually linked to multi-user
objective: equivalent to a run level of 2 the superior.
The services for each target are stored in a directory of “wishes”. This will contain .service
records. These are also symbolic links pointing to service definitions in /usr/lib/systemd/system
.
ls -l /etc/systemd/system/multi-user.target.wants/*.service
This allows service definitions to be created once and shared with multiple “objectives”. When Linux boots, systemd
select the target indicated by the runlevel. Next, the services desired by that objective will be loaded. systemd
supports service dependencies, so individual services can be made to wait on other services before starting.
Inspection of the execution level of your system
You can find out the runlevel of your system using the runlevel
command. This will print two characters to the terminal. They show the previous and current run levels of your system.
N 5
is a typical output for a desktop Linux system. the 5
indicates that you are in a multi-user graphical session with available networks. N
means the previous runlevel could not be determined, usually because the system was off.
In some systems, you may be able to get the current and previous runlevels using the $RUNLEVEL
and $PREVLEVEL
Environment Variables. If these variables are set, the runlevel
command just outputs its values.
You can also inspect your current runlevel using the who
command. To run who -r
to see the runlevel number and the time the runlevel was entered.
Switch between runlevels
You can switch between runlevels using the telinit
command. This allows you to manually activate different modes, such as single user mode or rescue mode.
In a hurry telinit 0
will turn off your machine; telinit 6
must initiate a reboot. Using telinit
with runlevels 1 – 5 will activate the corresponding operating mode. If you are currently in text-only mode, telinit 5
will try to enable the display server and enter graphical mode.
When you use systemd
, the systemctl isolate
The command allows you to activate a “objective” different. All running services will be stopped; the services associated with the new destination and then start.
Here's how to activate the multi-user
objective:
sudo systemctl isolate multi-user.target
If you want to make a permanent change to the runlevel, Use the systemctl set-default
command:
sudo systemctl set-default multi-user.target
Now him multi-user
The target will be selected whenever your system reboots.
Summary
Linux runlevels describe different states within the operating system. Each runlevel layers additional functionality in the form of additional run-time services.
Low run levels are commonly used for recovery modes and text-only operation. A desktop Linux system with a graphical environment will generally be at run level 5
. You should consult your distribution documentation for a detailed description of the available runlevels, as implementations may vary.