Switch between CLI and GUI in Linux

How to Switch Between CLI and GUI in Linux

In this article, we’ll cover how to switch between CLI and GUI in Linux to explore both interfaces. So, let’s get started!

Common-Line Interface vs. Graphical-User Interface

Initially, the computers were designed to be CLI. Users would access it through the keyboard. As the technology progressed, the GUI came into production, and it is widely used now.

However, Linux systems are primarily built to be used as CLI, even though GUI versions are widely available in the market. Most Linux administrators use the command-line version of Linux because it is easier to perform some tasks that aren’t easy with a graphical user interface. For instance, scheduling cron jobs can only be done via CLI.

Init Run Levels in Linux

Unlike Windows OS, Linux has a built-in init system manager. This system manager defines the state of the operating system and the services that are running. In other words, init is the first process that starts once the Linux system is booted. It leads to the initialization of all the other processes. 

The init process then begins to look for the default run levels of the systems. These run levels are represented in single digits and are defined as:

  • 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

The GUI-based systems, such as GNOME or KDE, run on level 5. On the other hand, the CLI runs on level 3. Hence, we can change this level to switch between CLI and GUI in Linux. 

How to Switch Between CLI and GUI in Linux

In this section, we’ll look at several ways we can use to switch between command-line and graphical user interfaces. Continue reading this tutorial to find out more. 

Switch Between CLI and GUI by Changing the Init Run Level

Let’s assume that you are running the CLI in Linux. To check the init-based run level, type the command given below:

runlevel

You should get a similar output:

switch between CLI and GUI in Linux

To switch to GUI, use the init level 5 as shown below:

sudo init 5

You’ll be asked to enter the username and password. Once you hit the “Enter” key, you’ll be back in GUI mode. 

Alternatively, if you are in GUI and want to switch to CLI, open the command prompt by pressing “Ctrl + Alt + T”, and run the runlevel command:

runlevel

After that, change the init level to 3 and hit the “Enter” key.

sudo init 3

Switch Between CLI and GUI in Linux by Setting the Default Init Level

Likewise, you can also set a default init-based run level to CLI or GUI. That way, your system will directly boot to that level. For this step, we’ll make the changes in the /etc/inittab file. First, switch to the root user by using the su command. Specifically, type:

su -

Enter the password and hit the “Enter” key to use the root account.

Next, open the file using the text editor:

# nano editor
nano /etc/inittab
#vim editor
vim /etc/inittab

After that, add the following lines:

Id:3:initdefault:

This will set the system default to CLI. 

Hit “Ctrl + S” to save the file and “Ctrl + X” to exit the nano editor. Lastly, reboot the system using the reboot command. Specifically, type:

sudo reboot

Switch Between CLI and GUI in Linux Using the Systemd-Based Boot 

This is the modern way to switch between CLI and GUI in Linux. Since most systems have replaced init with the systemd system manager, you can use the systemd-based target instead of the init-based system levels. 

Currently, most Linux distributions have already replaced the old init system with the modern systemd system manager. Hence, instead of the run level concept, we need to use the Systemd-based target concept to switch between CLI and GUI in Linux.

To view all the systemd levels, use the systemctl command. Specifically, type:

systemctl list-units --type target

The output should look something like this:

switch between cli and gui in Linux

However, if you want to view the current default target, type:

sudo systemctl get-default

You should get a similar output:

Systemctl default mode

Now to switch from GUI to CLI, we’ll use the systemctl command with isolate argument. For instance, type:

sudo systemctl isolate multi-user.target

Likewise, to switch from CLI to GUI, we’ll type:

sudo systemctl isolate graphical.target

Make sure to reboot the system using the reboot command. For example, type:

sudo systemctl reboot

Switch Between CLI and GUI by Setting the Default Systemd Target

Similar to the init-run systems, you can also set the default startup interface system for your Linux machine. For this, we’ll first get the systemctl defaults as shown below:

sudo systemctl get-default

Now to change the current target to the default level, we’ll use the set-default option. Specifically, type:

# for command-line interface
sudo systemctl set-default multi-user.target [For CLI]

Alternatively, you can use the graphical.target for the GUI. for example, type:

# for graphical-user interface
sudo systemctl set-default graphical.target [For GUI]

Lastly, we’ll restart the system to apply the settings. For example:

sudo systemctl reboot

Keyboard Shortcut To Switch Between CLI and GUI in Linux

Another method to switch from GUI to CLI is to use the keyboard shortcut keys. You can use this if you want to switch between CLI and GUI temporarily. This method is also very useful when your terminal does not work in GUI. 

To open CLI from GUI, press “Ctrl + Alt + F7”. to switch back to GUI, press “Ctrl + Alt + F2”.

This opens the virtual console where you can run terminal commands.

How to Switch Between CLI and GUI If You Are Stuck

If you are unable to go back to GUI using the shortcut key, run this command:

sudo systemctl start lightdm.service  

Run this command for the Ubuntu version above 16.04:

sudo systemctl start graphical.target  

Output:

Switch between CLI and GUI in Linux

However, if none of these commands work, remove the /var/lib/dpkg/lock file, update your system, and try again. Specifically, run these commands in order:

sudo rm -Rf /var/lib/dpkg/lock    
sudo apt dist-upgrade -y  
sudo apt update

Although GUI is the most commonly used interface, many users still prefer CLI as it is useful for administrative tasks and troubleshooting system failures. The article has covered several methods which you can use to switch between CLI and GUI in Linux, which are crucial for users. To learn more about the systemd commands, check out its official documentation.

If you liked this article, please share it 

Related Posts