How to switch between the CLI and GUI on a Linux server
Learn how to change between the command-line interface and the graphical user interface by editing the boot target.
What do you do if your server boots to a graphical user interface (GUI), but you need it to boot to the command-line interface (CLI) for security and performance reasons? In days past, you would have edited the /etc/fstab file so that, instead of booting to runlevel 5 (the GUI), it booted to runlevel 3 (the CLI). However, with the advent of the systemd system manager, you must do something a bit different.
Training & certification
First, you no longer use the runlevel term. Instead, you refer to boot choices as targets. The two primary targets for this scenario are multi-user.target (the CLI) and graphical.target (the GUI). The multi-user.target may be loosely associated with the legacy runlevel 3 concept, while the graphical.target is similar to runlevel 5.
It’s possible to switch targets manually, and you can also configure automatic or default boot targets. This article aims to explain both options.
Identify the current target
It’s probably clear when you boot the system which target is the default. However, to confirm the default, use the systemctl command along with the get-default subcommand:
$ sudo systemctl get-default
The result will likely display either the multi-user.target or graphical.target.
Change targets manually
You can switch between targets on the fly by using the isolate subcommand. Perhaps you have a long checklist of administrative tasks, and it would be simplest for you to accomplish these in a GUI, but your server boots to the CLI. You can switch to the GUI, perform your tasks, and then switch the system back to the more efficient CLI.
Here’s the command to launch the GUI from the multi-user.target:
$ sudo systemctl isolate graphical.target
Switch back by specifying the multi-user.target with this command:
$ sudo systemctl isolate multi-user.target
Those commands are useful for the current runtime, but what about configuring a default for when the system boots?
Set a default target
The default target represents the interface presented when the system first boots. Booting a server to a CLI for efficiency and security is common. The CLI consumes far fewer resources and contains less software to patch and worry over. Newer administrators or those migrating from another server platform may not be comfortable enough with Bash to work effectively at the command line. End-user workstations rarely boot to the CLI because the user typically needs graphics-based software, such as productivity suites and web browsers. For these users, the performance hit is worth the convenience.
Use the following systemctl command to configure the default startup target as the CLI:
$ sudo systemctl set-default multi-user.target
Use the graphical.target argument to set the GUI as the default.
Try it
Try the following exercise if you have a systemd-based distribution available, such as Red Hat Enterprise Linux (RHEL).
Check the current default target:
$ sudo systemctl get-default
Switch to the opposite target (for example, if your system boots to the GUI, switch to the CLI):
$ sudo systemctl set-default multi-user.target
Reboot and confirm the appropriate target launched:
$ sudo systemctl isolate graphical.target
Configure the system back to the original target:
$ sudo systemctl set-default graphical.target
Reboot and then confirm the default target:
If you don’t have access to a system to experiment with, I encourage you to create a simple lab environment for these types of learning opportunities.
Understand target files
Targets are managed by .target files that simply group units and dependencies into a convenient format. It’s really the .unit files that define exactly what services and other features start when the target is initiated. Enabling and disabling services and daemons adds and removes these components from the startup options:
$ sudo systemctl enable sshd $ sudo systemctl disable sshd