37

I am using 32-bit Red Hat Linux in my VM. I want to boot it to command-line mode, not to GUI mode. I know that from there I can switch to GUI mode using startx command. How do I switch back to command-line mode?

Peter Mortensen
  • 1,029
  • 1
  • 8
  • 10
0xF1
  • 649
  • 2
  • 8
  • 15

10 Answers10

33

Update for RedHat/CentOS 7 that has switched from sysvinit to systemd.

To switch from GUI to CLI: systemctl isolate multi-user.target

To switch from CLI to GUI: systemctl isolate graphical.target

To set the CLI as a default runlevel (target in systemd terminology): systemctl set-default multi-user.target. Analogously for GUI: systemctl set-default graphical.target

*CLI = Command Line Interface = command-line mode

golem
  • 2,278
  • 5
  • 20
  • 32
  • Also see: [Related Documentation](https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/sect-Managing_Services_with_systemd-Targets.html#sect-Managing_Services_with_systemd-Targets-Change_Default). – Mark Edington Feb 20 '16 at 11:51
  • 2
    Thank you.. Every forum is still dumping the old knowledge about /etc/inittab even though its obsolete. – Shashank Vyas Dec 26 '16 at 04:44
27

Update: The answer below is now obsolete

For a lot of distros now, the default is systemd rather than sysvinit. The answer below was written with sysvinit in mind. The more-up-to-date answer (and the one you should use if you have systemd as your init system) is golem's answer.

sysvinit answer (obsolete on most current distros):

You want to make runlevel 3 your default runlevel. From a terminal, switch to root and do the following:

[user@host]$ su
Password:
[root@host]# cp /etc/inittab /etc/inittab.bak #Make a backup copy of /etc/inittab
[root@host]# sed -i 's/id:5:initdefault:/id:3:initdefault:/' /etc/inittab #Make runlevel 3 your default runlevel

Anything after (and including) the second # on each line is a comment for you, you don't need to type it into the terminal.

See the Wikipedia page on runlevels for more information.

Explanation of sed command

  • The sed command is a stream editor (hence the name), you use it to manipulate streams of data, usually through regular expressions.
  • Here, we're telling sed to replace the pattern id:5:initdefault: with the pattern id:3:initdefault: in the file /etc/inittab, which is the file that controls your runlevles. The general syntax for a sed search and replace is s/pattern/replacement_pattern/.
  • The -i option tells sed to apply the modifications in place. If this were not present, sed would have outputted the resulting file (after substitution) to the terminal (more generally to standard output).

Update

To switch back to text mode, simply press CTRL+ALT+F1. This will not stop your graphical session, it will simply switch you back to the terminal you logged in at. You can switch back to the graphical session with CTRL+ALT+F7.

Joseph R.
  • 38,849
  • 7
  • 107
  • 143
  • Thanks for the answer. Also, when I switch to GUI using `startx` how can I switch back to command line? – 0xF1 Sep 13 '13 at 19:19
  • @0xF1 Answer updated. Please see if this helps. – Joseph R. Sep 13 '13 at 19:25
  • 1
    @0xF1 Added an explanation for the `sed` command. – Joseph R. Sep 13 '13 at 19:31
  • 1
    I would generally advise against using `sed` on configuration files, unless you know exactly what you are doing. The line in question might be also a comment and yet would still be changed. Hence the regular expression should be more precise, at least require the string `id:...` to be at the beginning of the line. Since the man page doesn't say anything about how white spaces are treated, you probably want to use `s/^ *id:5:initdefault:` (which doesn't include `Tab` character, by the way). – peterph Sep 14 '13 at 08:19
  • @peterph Agreed; my MO is to usually do such edits _in situ_. I'm using `sed` here for 2 reasons: a)spare myself the extra explanation of switching to root with environment so as to fire up a graphical editor. b) get the OP interested in `sed`. – Joseph R. Sep 14 '13 at 08:25
  • I usually use a light-weight tty-based emacslike editor for root-owned files. `sudo jed /etc/inittab`. I don't want to run GUI anything as root. – Peter Cordes Aug 23 '15 at 01:44
  • 1
    This answer is no longer valid, at least on my current OS. Take a look at @golem's answer below. That worked for me. I'm using fedora 27. – Lance Mar 22 '18 at 14:52
  • 1
    @Lance Very good point. Thanks for the notice. I have updated my answer to reflect that. – Joseph R. Mar 22 '18 at 20:05
11

First switch user to root.

su -
Password:

Enter root password.

Use your favorite editor to modify this line in /etc/inittab:

id:5:initdefault:

Change the 5 to 3. When you (re)boot the computer it will take you to the command line rather than to the GUI.

Joseph R.
  • 38,849
  • 7
  • 107
  • 143
Timothy Martin
  • 8,447
  • 1
  • 34
  • 40
  • Please include instructions about how the OP should do this as root and to which file. This answer, as it stands, is not fit to help someone who's new to Linux. – Joseph R. Sep 13 '13 at 19:06
  • 1
    Thank you for pointing out my oversight. I have edited the answer accordingly. – Timothy Martin Sep 13 '13 at 19:20
  • Does this mean that the GUI is not running, or is it hidden? – Aaron Franke Jan 12 '18 at 08:57
  • @AaronFranke For Redhat systems in run level 3 the GUI is not enabled (or turned off in the case of a system which was switched from runlevel 5 to 3 without a reboot). – Timothy Martin Jan 12 '18 at 09:10
3

Even being a quite old thread, may be useful. On system using systemd, /etc/inittab is no longer in use. The method is quite simple:

Terminal mode

ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target

Graphic Mode

ln -sf /lib/systemd/system/graphical.target  /etc/systemd/system/default.target
fcm
  • 427
  • 4
  • 15
  • This is a very helpful answer, for the case where you want/need to change the setting when the system isn't running. – Time4Tea Feb 14 '21 at 21:49
3

On a side note, if you've already booted into graphical mode and would like to switch to text mode, you could just press Ctrl + Alt + F1 and back again to graphical mode by Ctrl + Alt + F7.

Linux has by default 6 text terminals and 1 graphical terminal. You can switch between these terminals by pressing Ctrl + Alt + Fn. Replace n with 1-7. F7 would take you to graphical mode only if it booted into run level 5 or you have started X using startx command; otherwise, it will just show a blank screen on F7.

Joseph R.
  • 38,849
  • 7
  • 107
  • 143
Punit Arya
  • 300
  • 1
  • 8
3

Apart from changing /etc/inittab, you can also tell the kernel on its command line what target runlevel should be passed to init once it is started. This is done by simply appending the desired runlevel to the command line (it has to be the last argument I believe).

You can do this either as a one-off thing during boot, provided your bootloader allows you to change the kernel command line, or you can duplicate entry in the bootloader configuration and pick the right one when booting (useful when you are booting into various runlevels often).

For systems using systemd the process is similar, but means more typing since the magic string appended to the kernel command line is in the form of systemd.unit=desired.target.

As for startx, it can also start additional sessions by giving it an unused X display number (numbering starts from 0): startx -- :1 will start X server on display :1, locating it at the first unused VT (often VT8, since first 6 are usually Linux consoles and 7 is used by the first running X session). Note that the X server usually needs root privileges so you either have to do this as root (which is not a good idea), or the binary has to be setuid root (the need for this is normally removed by using a display manager).

peterph
  • 30,520
  • 2
  • 69
  • 75
0

After replace the default runlevel to id:3:initdefault: in /etc/inittab using your preferred text editor (as said previously), it's very important check if there's some plymouth (splash screen) installed in your system. In this case, will be need to remove the installation of it, or just remove the splash keyword from you /boot/grub/grub.cfg using your preferred text editor (vim, pico, or others). To remove plymouth:

sudo apt-get remove --purge plymouth

Edit your grub.cfg:

sudo vim /boot/grub/grub.cfg

Finally, upgrade the grub configuration:

sudo update-initramfs -u

In next boot, you'll have just text mode login screen. That's it.

Enjoy Linux box!

user
  • 28,161
  • 13
  • 75
  • 138
S4nD3r
  • 101
  • 1
-1

The oracle-linux 7 installation by defaults takes minimal installation option . You must change it to installation with GUI mode at the time of installation .This has solved my issue on not able to use the graphical user mode .

Guhan
  • 1
  • It seems you have the OP's request backwards. The OP appears to be struggling to get to the CLI, not to the GUI. – HalosGhost Feb 11 '15 at 13:32
-1

I've tried systemctl, update-rc.d and even editing the GRUB config, but nothing worked. Then I commented out the default display manager in /etc/X11/default-display-manager, rebooted and finally: the terminal!!!! It's as simple as that!

Hehe
  • 1
-1

Use

systemctl set-default multi-user.target

and reboot the VM. The VM will get into CLI mode.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936