Switching between console and GUI in ubuntu

When in GUI mode, is there a CLI commands that kills the GUI and drops me into the console?

When in CLI mode, is there a CLI command that drops me into the GUI?

I found startx, but this drop me into the GUI without the unity interface, how do I launch anything?

I found service lightdm start which drops me into the GUI with the unity interface.

I found service lightdm stop which drops me into a blank black screen with no CLI input capability.

I found the Ctl + Alt + F* sends me back between console and GUI, but it does not kill the GUI. I want to kill the GUI if I don't want to use it anymore.


Solution 1:

To go in tty1 and stop the GUI, run from terminal:

sudo xdotool key Ctrl+Alt+F1 && sudo service lightdm stop

You can test now in the tty1 the fact that the GUI is stopped using:

sudo service lightdm status

Note: xdotool is not installed by default in Ubuntu, so you must to install it first using sudo apt-get install xdotool command.

To start the GUI again from tty1, you can run (as you said):

sudo service lightdm start

If you want also to close (exit) tty1 session, you can use:

sudo service lightdm start && logout

Solution 2:

Since 16.04 (and possibly 15.10, but I skipped everything between 14.04 and 16.04...), you are expected to use systemctl instead:

To start lightdm, you will get the lightdm prompt (login screen) first:

sudo systemctl start graphical.target

Then to leave X-Windows start multi-user instead:

sudo chvt 1 && sudo systemctl start multi-user.target

chvt means "Change Virtual Terminal", because otherwise you get on vt 7 which will look like something is broken. You can use the Alt-F1 to Alt-F7 or even Alt-F8 to switch between terminals.

For more information about systemd, check out the systemd wiki. I am still learning myself!


The graphical.target file is found under /lib/systemd/system/graphical.target and looks like this (16.04 version):

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Graphical Interface
Documentation=man:systemd.special(7)
Requires=multi-user.target
Wants=display-manager.service
Conflicts=rescue.service rescue.target
After=multi-user.target rescue.service rescue.target display-manager.service
AllowIsolate=yes

The multi-user.target file is found under /lib/systemd/system/multi-user.target and looks like this (16.04 version):

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Multi-User System
Documentation=man:systemd.special(7)
Requires=basic.target
Conflicts=rescue.service rescue.target
After=basic.target rescue.service rescue.target
AllowIsolate=yes