How to change runlevels on Linux
A runlevel defines the state of the machine after boot. In simpler terms, a runlevel is a configurable mode of operation in which the system enters after boot and runs the startup scripts associated with that runlevel.
To start with, whenever we boot a system, the Linux kernel creates the first process called the init process, to start all the other processes on the system. It then reads a file /etc/inittabs which has a table of processes to start automatically on bootup. (This has changed for some of the latest Linux distros) This is also used to determine the behavior for each runlevel.
On startup, only one runlevel is executed. runlevels are not executed one after another. Conventionally, seven runlevels exist, numbered from zero to six. Each runlevel designates a different system configuration and allows access to a different combination of processes. By default Linux boots either on runlevel 3 or runlevel 5.
Standard runlevels
Runlevels 2–5 vary depending on the distribution. The most commonly used runlevels are 0, 1, 3, 5 and 6. As you can see you use runlevels every time you shutdown (runlevel 0) or reboot (runlevel 6) your machine. Most users boot to runlevel 5 with X11 support and a GUI login.
At runlevel 1, only the basic system processes are started, along with the console terminal access. This is called single-user mode. Single user mode is most often used for emergency filesystem maintenance when something is broken. The standard init runlevel is 3. At this runlevel most application software such as network support software etc are started. Another popular runlevel is 5. Here, the system starts the graphical X window software & allows you to log in using graphical desktop window.
How to check the current runlevel:
There are multiple ways we can check the current runlevel. Few of them listed below:
- Using runlevel command: which prints the previous and current runlevel.
Here, N means runlevel was never changed since system boot & 5 indicates current runlevel
$ runlevel N 5
2. Using who -r command: which prints information about users who are currently logged in with runlevel.
$ who -r
run-level 5 2021-15-06 11:32
3. By checking /etc/inittab file:
$ cat /etc/inittabid:5:initdefault:
How to change runlevel:
- Using init command:
# init 3
2. By editing /etc/inittab file:
To change the default run level, edit /etc/inittab file and edit entry initdefault
# vi /etc/inittabid:5:initdefault:
Save and close the file. Reboot the system to see changes:# reboot
References:
- Linux command line and shell scripting bible by Richard Blum
- Operating System Concepts by Galvin and Gagne
- https://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/runlevels.html