# LPI Linux Certification/Change Runlevels And Shutdown Or Reboot System
## 101.3 Change runlevels / boot targets and shutdown or reboot system

(LPIC-1 Version 5.0)
Weight: 3
Description:
Candidates should be able to manage the SysVinit runlevel or systemd boot target of the system. This objective includes changing to single user mode, shutdown or rebooting the system. Candidates should be able to alert users before switching runlevels / boot targets and properly terminate processes. This objective also includes setting the default SysVinit runlevel or systemd boot target. It also includes awareness of Upstart as an alternative to SysVinit or systemd.
Key knowledge area(s):
* Set the default runlevel or boot target.
* Change between runlevels / boot targets including single user mode.
* Shutdown and reboot from the command line.
* Alert users before switching runlevels / boot targets or other major system event.
* Properly terminate processes.
* Awareness of acpid.
The following is a partial list of the used files, terms and utilities:
* /etc/inittab
* shutdown
* init
* /etc/init.d/
* telinit
* systemd
* systemctl
* /etc/systemd/
* /usr/lib/systemd/
* wall

## Runlevels Overview

Runlevels are used in Linux to customize the way operating system is initialized. Runlevel defines which services are started automatically during initialization. Each runlevel has a number (and possibly alias) which defines it. Common runlevel number can be found in a table below, unfortunately some of the differ between RedHat and Debian based distributions:
| Runlevel | RedHat | Debian |
| 0 | Halt the system. This runlevel should never be set as default | The same as RedHat |
| 1(single) | Single user mode. In this runlevel Linux starts only basic services and in most distributions will start root shell which does not requird password to login. No networks or NFS services are started. This runlevel can be used to reset root password or to perform some basic system maintenance | The same as RedHat |
| 2 | Multiuser without NFS support | Full multiuser, normally default |
| 3 | Full multiuser, normally default | Normally unused |
| 4 | Normally unused | Normally unused |
| 5 | Full multiuser with X11. This runlevel normally allows for graphical login | Normally unused |
| 6 | Reboot the system. This runlevel should never be set as default as it will cause system to enter reboot loop | The same as RedHat |

## Setting Default Runlevel

Default runlevel is controlled in /etc/inttab file in most of the distributions however this is currently changing as some new Linux distributions start using more advanced event driven upstart replacement for traditional init program. In /etc/inittab the following line controls default runlevel (example from Debian with runlevel 2 set as default):
When operating system loads /sbin/init program it will read the line above and determine that it should use runlevel 2 (unless another runlevel number was passed as kernel parameter). Following this init will run all scripts from /etc/rcX.d/ directory where X is runlevel number. Sample content of the directory might look similar to:
All services from the directory will be run in order starting from the lowest number to the highest. As you might have notices all file in the /etc/rcX.d/ directory are symbolic links to startup scripts in /etc/init.d/. When operating system enters the runlevel init program passes "start" parameter to all scripts prefixed with "S" character and "stop" to all scripts prefixed with "K". In above example one of the first programs to run is rsyslog:
This will cause init to execute the following command:
Above example is runlevel 2 from Debian which is full multiuser so no services have to be killed however if we look at runlevel 1 on the same system it looks very differently:
When system enters runlevel 1 it will kill all services beginning with "K" leaving bare minimum running.

## Changing Runlevels, shutting down and rebooting system

init and telinit - telinit command is just a link to init, functionality of both is the same. They can be used to change the current runlevel. They take only one parameter, which is the new runlevel number. Example usage: Change runlevel single user mode:
Runlevel 6 is reboot. init can be used to reboot the system:
To shutdown we go to runlevel 0
shutdown - shutdown command can be used to reboot, shutdown and send a warning message to all logged in users.
Common options:
    -h - halt or power off after shutdown
    -r - reboot after shutdown
    -k - Don't really shutdown; only send the warning message to everybody
Examples: Shutdown in 5 minutes and send warning message
Reboot immediately
Do not reboot or shutdown, only send message to users
Shutdown system at 23:59