Command line mode in linux

How to boot Linux to command-line mode instead of GUI?

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?

10 Answers 10

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

Thank you.. Every forum is still dumping the old knowledge about /etc/inittab even though its obsolete.

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).

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 .

Thanks for the answer. Also, when I switch to GUI using startx how can I switch back to command line?

Читайте также:  Load balance no linux

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).

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.

First switch user to root.

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

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

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.

@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).

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 .

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).

Читайте также:  Linux enable disable user

Источник

How to Switch Between CLI and GUI on Linux Server

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:

Check Linux Run Level

To switch to CLI from GUI, change the run level to 3 by running:

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:

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):

Читайте также:  Открыть порт 443 линукс

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

List Systemd Targets

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:

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.

Источник

Оцените статью
Adblock
detector