Linux systems use different run levels or targets to determine how the operating systems will be configured on startup. Run levels are considered legacy, and targets are used by modern Linux systems.
Run levels
Following are the 7 Linux runlevels.
Runlevel | Label | Short Description |
---|---|---|
0 | halt | Powers off the machine |
1 | single-user mode | Text only interface |
2 | multi-user mode | Graphical desktop mode for Debian |
3 | multi-user mode | Extended Multiuser mode |
4 | multi-user mode | This can be used to create your own custom runlevel |
5 | graphical mode | Graphical desktop mode for Red Hat |
6 | reboot | reboots the system |
System runlevels
The runlevel command without any options can be used to view the current and previous runlevel for the system. The first character is the previous runlevel, and the second character is the current runlevel. "N" stands for nonexistent, meaning there was no previous runlevel. For example, if you boot a system and do not change the default runlevel, the previous runlevel will be N.
The who -r command can also be used to view the current system runlevel.
/sbin/runlevel
. . .
N 1
The init and telinit commands can be used to immediately change the runlevel of the entire system. This is a temporary change, and will not change the default runlevel of the system.
[root@server1]# init 5
[root@server1]# telinit 5
The /etc/inittab file can be edited to permanently change the default runlevel of the system. In this example, the system is configured to use runlevel 5.
id:5:default
While not pertient to setting the default runlevel, it can be helpful to recongize that the /etc directory contains 7 files for each runlevel. If /etc/inittab is set to id:1:default, the /etc/rc1.d runtime configuration script will be used. This script tells the system to put the system into runlevel 1.
[user1@server1]# ls /etc | grep rc..d
rc0.d
rc1.d
rc2.d
rc3.d
rc4.d
rc5.d
rc6.d
chkconfig
The chkconfig command can be used to view and change deamon runlevels. The chkconfig command is legacy. Modern Linux distributions use systemctl to view and change deamon runlevels.
Systemd
Runlevels are used by systems using INIT Systems using systemd do not use runlevels, and instead take a different approach. Following is a comparison between Init and SystemD.
Target | Description | Equivalent Runlevel |
---|---|---|
poweroff.target | Powers off the machine | 0 |
rescue.target | Text only shell in emergency mode | 1 |
multi-user.target | Text only shell | 2, 3, 4 |
graphical.target | Graphical desktop | 5 |
reboot.target | Reboots the system | 6 |
With Systemd, the systemctl get-default command is used to determine the target for the entire system. In this example, the entire system is in graphical.target.
[root@server1]# systemctl get-default
multi-user.target
The following command can be used to set the target. In this example, the target is set to graphical.target.
[root@server1]# systemctl set-default graphical.target
The chkconfig command has an option to set a deamon to be on or off at a certain runlevel. SystemD does not have this type of option. Each deamon will be configured to be enabled at a certain target. In this example, the nginx deamon is configured to be enabled at multi-user.target.
[root@server1]# systemctl cat nginx
. . .
[Install]
WantedBy=multi-user.target
Rescue Target (emergency mode)
In Rescue mode, the runtime configuration scripts (/etc/rc*.d) are not used, which means certain daemons, such as firewalld, will not be started on boot. After booting into rescue mode, the following welcome message will appear.
Welcome to emergency mode! After logging in, type "journalctl -xb" to view
system logs, "systemctl reboot" to reboot, "systemctl default" or ^D to
boot into default mode.
Give root password for maintenance
(or type Control-D to continue):
[root@server1 ~]#
Did you find this article helpful?
If so, consider buying me a coffee over at