If we trace the evolution of computers, we can see that initially, we don’t have Mouse. So, we used to interact with computers having Command Line Interface (CLI) only through keyboards passing inputs to perform actions via command line program.
As progressed, now we can easily interact and perform actions via a graphical user interface (GUI) that provides various menus like windows, and buttons. Nowadays people are more inclined and used to GUI-based OS.
Altogether, the computer now comes with an option to work either in the command line or graphical mode. Further, you can also start GUI from the command line and CLI from graphical mode, whichever suits your requirement.
In this article, we’ll see how can we switch between the command line and the graphical interface on Linux Server. But before that, let’s also get an overview of the concept that actually makes changing GUI to CLI and CLI to GUI work.
Init-based Run Levels in Linux
In Linux with an init system manager, the run level defines the operating state of the init process and the whole system and indicates system services that are running.
When the Linux Kernel boots, the init process is the first thing that gets started, and further, it leads to the initialization of other Linux processes.
The moment the init process begins it looks for the value of the default run level of the system. Run level i.e. state of the system is represented in a single-digit integer.
The Standard Linux kernel supports the following seven different run levels:
- 0 – Halt the system
- 1 – Single user mode
- 2 – Multiple user mode with no network file system
- 3 – Multiple user mode under CLI
- 4 – User-definable
- 5 – Multiple user mode under GUI
- 6 – Reboot
By default, if a system has a desktop environment like GNOME or KDE, it boots to graphical run level 5, or else the command line run level 3.
That is why by simply changing the run level of the system you can switch between user interfaces.
Change Init-Based Run Levels in Linux
If you want to know the current run level of your system it is running, type:
$ runlevel
To switch to CLI from GUI, change the run level to 3 by running:
$ sudo init 3
Enter and password and you’ll be redirected to the command line mode where you need to log in again.
Likewise, if you want to come back to GUI from CLI, type the following in the command line:
$ sudo init 5
Set A Default Init Run Level
Want to boot your system directly to the command line at startup? You need to make run level 3 as default by modifying the following line in the /etc/inittab file (you’ll not find this file in Linux with Systemd system manager):
Id:3:initdefault:
Similarly, replace 3 with 5 to make GUI as default run level in Linux.
Systemd-Based Boot Target In Linux
Currently, the majority of Linux distributions have already replaced the old init system with the modern systemd system manager. Hence, instead of the runlevel concept, we need to use the Systemd-based target concept to switch between CLI and GUI in Linux.
Similar to runlevel 3 for CLI and runlevel 5 for GUI, systemd has a corresponding target multi-user.target for CLI and graphical.target for GUI.
By executing the below command, you can list all systemd active targets:
$ systemctl list-units --type target
Switching Boot Targets Between CLI and GUI in Linux
Now, to change the target manually or switch to command line mode from GUI, you can use the systemctl command with isolate subcommand in the given below format:
$ sudo systemctl isolate multi-user.target
To switch back to GUI from command line mode, specify graphical.target with the command:
$ sudo systemctl isolate graphical.target
Set A Default SystemD Target in Linux
As you boot the Linux system, it loads the default target. So, you can identify the current default target using the systemctl command along with the get-default subcommand:
$ sudo systemctl get-default
And to change the current target and set it as default for all next boot, use the set-default option with multi-user.target for CLI or graphical.target for GUI as argument:
$ sudo systemctl set-default multi-user.target [For CLI] $ sudo systemctl set-default graphical.target [For GUI]
Next, reboot the system to apply the default target:
$ sudo systemctl reboot
Keyboard Shortcut To Switch Between CLI and GUI
If you already have a desktop environment on your Linux system, you can also switch between your graphical desktop and command line text mode by simply pressing CTRL + ALT + N
. Here Replace N
with numbers 1 to 7.
Linux has by default six text terminals and one graphical terminal. So, to switch to command line mode, you can press CTRL + ALT + F1/F2/F3/F4/F5/F6
and switch back to the graphical mode by pressing CTRL + ALT + F7
.
Keyboard Shortcut is only helpful temporarily as we cannot use it set the default mode for permanent the way we can in the above Init or Systemd-based methods.
Conclusion
Though GUI is the most prevalent way of using Linux systems, CLI is still relevant as it helps in troubleshooting if GUI fails or system resources is too less to support GUI.
That is why it’s worth knowing how we can switch between graphical and command modes and also set either as default. As we learned, this can be done in various ways like by the modern way of changing the boot target in systemd, Keyboard shortcuts, and legacy init system manager.