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?
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).
How To Boot Into Linux Command Line
There may be times where you need or want to boot up a Linux system without using a GUI, that is with no X, but rather opt for the command line. Whatever the reason, fortunately, booting straight into the Linux command-line is very simple. It requires a simple change to the boot parameter after the other kernel options. This change specifies the runlevel to boot the system into.
Why Do This?
If your system does not run Xorg because the configuration is invalid, or if the display manager is broken, or whatever may prevent the GUI from starting properly, booting into the command-line will allow you to troubleshoot by logging into a terminal (assuming you know what you’re doing to start with) and do whatever you need to do. Booting into the command-line is also a great way to become more familiar with the terminal, otherwise, you can do it just for fun.
Accessing GRUB Menu
On startup, you will need access to the GRUB boot menu. You may need to hold the SHIFT key down before the system boots if the menu isn’t set to display every time the computer is started. In the menu, the Linux distribution entry must be selected. Once highlighted, press ‘e’ to edit the boot parameters.
Older GRUB versions follow a similar mechanism. The boot manager should provide instructions on how to edit the boot parameters.
Specify the Runlevel
An editor will appear and you will see the options that GRUB parses to the kernel. Navigate to the line that starts with ‘linux’ (older GRUB versions may be ‘kernel’; select that and follow the instructions). This specifies parameters to parse into the kernel. At the end of that line (may appear to span multiple lines, depending on resolution), you simply specify the runlevel to boot into, which is 3 (multi-user mode, text-only).
Pressing Ctrl-X or F10 will boot the system using those parameters. Boot-up will continue as normal. The only thing that has changed is the runlevel to boot into.
This is what was started up:
Runlevels
You can specify different runlevels to boot into with runlevel 5 being the default one. 1 boots into “single-user” mode, which boots into a root shell. 3 provides a multi-user, command-line only system.
Switch From Command-Line
At some point, you may want to run the display manager again to use a GUI, and the quickest way to do that is running this: $ sudo init 5 And it is as simple as that. Personally, I find the command-line much more exciting and hands-on than using GUI tools; however, that’s just my preference.
Linux equivalent of the DOS «start» command?
In windows I can do start abcd.pdf that opens the pdf file in the registered viewer. Is there anything similar in Linux?
Just to add the importance of the final & , in python it does not execute the next line of code without it. So if you are automating, that & right there is the most important thing, if you want to wait until it ends, not put it there, but if you do not want to wait put it there, that is what I observed, @vartec highlighted, very important part.
maybe it´s not a seperate window that gets started, but you can run some executables in background using «&»
means your script will not wait until myexecutable has finished but goes on immediately. maybe this is what you are looking for. regards
xdg-open is a good equivalent for the MS windows commandline start command: xdg-open file opens that file or url with its default application xdg-open . opens the currect folder in the default file manager
One of the most useful terminal session programs is screen.
screen -dmS title executable
You can list all your screen sessions by running
And you can connect to your created screen session (also allowing multiple simultaneous/synchronized sessions) by running
This will open up the emulated terminal in the current window where executable is running. You can detach a screen session by pressing C-a C-d, and can reattach as many times as you wish.
If you really want your program started in a new terminal window, you could do something like this: xterm yourtextmodeprogram or gnome-terminal -e yourtextmodeprogram or konsole -e mc
Trouble is that you cannot count on a particular terminal emulator being installed, so (again: if you really want to do this) you would need to look for the common ones and then execute the first one encountered.
As Joachim mentioned: The normal way to do this is to background the command (read about shell job control somewhere, if you want to dig deeper).
There are also cases where you want to start a persistent shell, i.e. a shell session which lives on when you close the terminal window. There are two ways to do this: