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.
Following are the 7 Linux runlevels.
|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|
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.
[root@server1]# 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.
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
Not only does the entire system have a runlevel, but each individual deamon is also configured with certain runlevels. The chkconfig command without any options can be used to view the runlevel of each service on the system. The following example is a very short example. An actual system will have many services. The --list option can be used to only display a certain service, such as chkconfig --list postfix.
[root@server1 ~]# chkconfig netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off . . .
The chkconfig service_name on or chkconfig service_name off commands can be used to turn a service on or off. This will turn the service on or off at every runlevel. For example, to turn the HTTPD service on or off.
[root@server1 ~]# chkconfig httpd on [root@server1 ~]# chkconfig httpd off
To only change the runlevel status for certain runlevels, use the --level option.
[root@server1 ~]# chkconfig httpd on --level 345
When using the chkconfig command to change the runlevel of a service, a file in the /etc/init.d/ directory will be updated to contain the new runlevel. For example, the prior commands turns on the HTTPD deamon for runlevels 3, 4, and 5. This command updates the /etc/init.d/httpd file to contain the following. Even though this line is commented out, this still turns on the HTTPD deamon for runlevels 3, 4, and 5.
#!/bin/sh . . . # chkconfig: 345 95 05
|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|
|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 ~]#