This brief guide explains what is a runlevel, how many runlevels are there in Linux and how to check the runlevel in Linux operating systems.
What is runlevel?
A runlevel is one of the modes that a Unix-based operating system will run in. In other words, a run level is a state of init and the whole system that defines what system services are operating.
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.
1. 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:
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
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
$ 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 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
2. Check the Runlevel In Linux (Systemd)
In recent versions of Linux systems (starting from RHEL 7, Ubuntu 16.04 LTS), the concept of runlevels has been replaced with
Here is the list of
Systemd targets in Linux distributions,which were distributed with
Systemd as default service manager.
rescue.target- Single-user text mode
multi-user.target- Not used (user-definable)
multi-user.target- Full multi-user text mode
multi-user.target- Not used (user-definable)
graphical.target- Full multi-user graphical mode (with an X-based login screen)
In Linux systems that are using
Systemd as default service manager, you can find the current target using command:
$ systemctl get-default
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 Local Encrypted Volumes getty.target loaded active active Login Prompts graphical.target loaded active active Graphical Interface local-fs-pre.target loaded active active Local File Systems (Pre) local-fs.target loaded active active Local File Systems multi-user.target loaded active active Multi-User System network-online.target loaded active active Network is Online network-pre.target loaded active active Network (Pre) network.target loaded active active Network nfs-client.target loaded active active NFS client services nss-lookup.target loaded active active Host and Network Name Lookups nss-user-lookup.target loaded active active User and Group Name Lookups paths.target loaded active active Paths remote-fs-pre.target loaded active active Remote File Systems (Pre) remote-fs.target loaded active active Remote File Systems rpc_pipefs.target loaded active active rpc_pipefs.target rpcbind.target loaded active active RPC Port Mapper 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 veritysetup.target loaded active active Local Verity Integrity Protected Volumes 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. 26 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
To change to a different target unit in the current session only, run the following command:
$ sudo systemctl isolate runlevel3.target
For more details, refer
Systemd man pages.
$ man systemd
Check Linux Runlevel Using 'who' command
As one of the reader suggested in the comment section below, we can also find the runlevel using
who command like below:
$ who -r
run-level 5 2021-06-09 10:21
Hope this helps.
Well, how to get run level programmatically? Via syscall, or similar, for example?
I don’t know how to do this programmatically, but I found a solution in Stackoverflow. https://stackoverflow.com/questions/55951143/is-it-possible-to-set-the-current-linux-runlevel-programmatically