A runlevel is one of the modes that a Unix-based operating system will run in. In Linux Kernel, there are 7 runlevels exists, starting from 0 to 6. The system can be booted into only one runlevel at a time. By default, a system boots either to runlevel 3 or to runlevel 5. Runlevel 3 is CLI, and 5 is GUI. The default runlevel is specified in /etc/inittab file in most Linux operating systems. Using runlevel, we can easily find out whether X is running, or network is operational, and so on. In this brief guide, we will talk about how to check the runlevel in Unix-like operating systems.
Check the Runlevel In Linux (SysV init)
Here is the list of runlevels in Linux distributions,which were distributed with SysV init as default service manager.
- 0 - Halt
- 1 - Single-user text mode
- 2 - Not used (user-definable)
- 3 - Full multi-user text mode
- 4 - Not used (user-definable)
- 5 - Full multi-user graphical mode (with an X-based login screen)
- 6 - Reboot
Now, let us see how to find the runlevels.
To find out the system runlevel, open your Terminal and run the following command:
Sample output for the above command would be:
In the above output, the letter 'N' indicates that the runlevel has not been changed since the system was booted. And, 3 is the current runlevel i.e the system is in CLI mode.
In newer versions of init, you can find the current and previous runlevel details using the environment variables called RUNLEVEL and PREVLEVEL.
Say for example, to find out the current runlevel, you need to run:
$ echo $RUNLEVEL
To find the previous runlevel:
$ echo $PREVLEVEL
If you'd like to change the RunLevel to something else, edit /etc/inittab file:
$ sudo vi /etc/inittab
Find and edit entry initdefault to the runlevel of your choice. Say for example, to set the runlevel to multi-user graphical mode (runlevel 5), modify the it as shown below.
Save and close the file. Reboot your system to login to your new runlevel.
For more details about runlevels, refer man pages.
$ man runlevel
Check the Runlevel In Linux (Systemd)
Here is the list of Systemd targets in Linux distributions,which were distributed with Systemd as default service manager.
- runlevel0.target, poweroff.target - Halt
- runlevel1.target, rescue.target - Single-user text mode
- runlevel2.target, multi-user.target - Not used (user-definable)
- runlevel3.target, multi-user.target - Full multi-user text mode
- runlevel4.target, multi-user.target - Not used (user-definable)
- runlevel5.target,graphical.target - Full multi-user graphical mode (with an X-based login screen)
- runlevel6.target,reboot.target - Reboot
In Linux systems that are using Systemd as default service manager, you can find the current target using command:
$ systemctl get-default
Sample output would be:
As you see in the above output, my current runlevel (target in other words) is 5, which is graphical mode.
To view all currently loaded targets, run:
$ systemctl list-units --type target
UNIT LOAD ACTIVE SUB DESCRIPTION basic.target loaded active active Basic System bluetooth.target loaded active active Bluetooth cryptsetup.target loaded active active Encrypted Volumes getty.target loaded active active Login Prompts local-fs-pre.target loaded active active Local File Systems (Pre) local-fs.target loaded active active Local File Systems network-online.target loaded active active Network is Online network.target loaded active active Network nss-user-lookup.target loaded active active User and Group Name Lookups paths.target loaded active active Paths remote-fs.target loaded active active Remote File Systems slices.target loaded active active Slices sockets.target loaded active active Sockets sound.target loaded active active Sound Card swap.target loaded active active Swap sysinit.target loaded active active System Initialization timers.target loaded active active Timers LOAD = Reflects whether the unit definition was properly loaded. ACTIVE = The high-level unit activation state, i.e. generalization of SUB. SUB = The low-level unit activation state, values depend on unit type. 17 loaded units listed. Pass --all to see loaded but inactive units, too. To show all installed unit files use 'systemctl list-unit-files'.
The above command will show only the active targets.
To view all loaded targets (active and inactive), run:
$ systemctl list-units --type target --all
If you'd like to change the RunLevel to something else, for example runlevel3.target, set it as shown below:
$ sudo systemctl set-default runlevel3.target
$ sudo systemctl isolate runlevel3.target
For more details, refer Systemd man pages.
$ man systemd
Check the Runlevel In Linux Using 'who' command
As our readers suggested in the comment section below, we can find the runlevel using following command:
$ who -r
run-level 5 2018-06-06 12:22