what does startx command do?

While reading about Ubuntu I found the following statement.....

If the display manager is not started by default in the default runlevel, you can start X a different way, after logging on to a text-mode console, by running startx from the command line.

What does it mean to be a default runlevel? And when I tried this on my terminal I got this:

anupam@JAZZ:~$ startx

X: user not authorized to run the X server, aborting.
xinit: giving up
xinit: unable to connect to X server: Connection refused
xinit: server error

Once upon a time(1), when the memory of the computers was measured in kilobytes and the disks in megabytes, running the graphic interface all the time was considered harmful.

Most Unix computers were used for scientific computations and simulation in multi-user environments, and the graphic interface running on them would reduce the memory and CPU power available to them.

So when you needed a graphical interface you just started it with startx (2).

startx basically runs an Xserver (the graphical "driver") and a command which run on it, which is typically a window manager. By default the commands that are run are in ~/.xinitrc file in your home directory, or some generic system file otherwise.

Modern systems are thought from the bottom up to have a graphical system running all the time, so no one has probably checked the working of startx for ages -- that explains a lot of strange behavior you can have.

If you want to experiment and the feel the good old times, the best thing is doing the following:

A) install Xnest and fvwm. Xnest is a graphic server-within-a-server, a kind of server that will open as a window in your normal system. Fvwm is a very simple window manager which was very popular back then. You will need old pixmap fonts, too.

sudo apt-get install xnest fvwm 
sudo apt-get install xfonts-100dpi xfonts-100dpi-transcoded xfonts-75dpi-transcoded xfonts-75dpi 

B) write this file somewhere, for example in you home dir, and call it ~/test:

#!/bin/bash 
#
xterm & 
exec fvwm2

C) Run (notice: startx is normally run with first the client command, then a double dash, and then a server command. As I said, most new systems do not have sensible defaults for startx alone.).

cd ~
startx ~/test -- /usr/bin/Xnest -ac :1 -geometry 800x600

...and you have a 80ies workstation screen:

enter image description here

(You can have the menu by clicking on the "Xnest" desktop).

...and if you feel really adventurous, you can start a native session on another virtual console (read the other answers) by going to one of them with Ctrl-Alt-F1, loggin in, and

startx ~/test 

which will normally open on Ctrl-Alt-F8.

Notice: modern desktop environments are not designed to run simultaneously, for the same user, in two different consoles. So do not use gnome-shell or unity or modern things when doing this experiments, or you could mess up your configuration.


Footnotes:

(1) Speaking about around 1980-90 here.

(2) For example, I had a laptop with 256k of RAM. It was painfully slow in the (B&W!) graphic interface, but snappy in the console. So I did most of my work (editing C, LaTeX and similar files) in console mode, and switched to the graphic environment only when really needed.


startx starts the xsession or the graphical interface where you see a login screen and anything more than just an ascii console (text session).

You are receiving this error because the xsession is already started and you are trying to execute the command from inside the xsession on tty7.

tty7 is the user interface that exists at ctrl + alt + f7 (default xsession).

tty1 is the user interface that exists at ctrl + alt + f1.

tty2 is the user interface that exists at ctrl + alt + f2 . . . and so on.

tty1 - tty6 are text sessions and you can login with your username and password on one of these screens. After you login, you can run the command startx and the xsession will start in tty7 if it is not already running and tty8 if tty7 is already running an xsession.


Run Levels:

run-level 0 Halt - Shuts down the system.

run-level 1 Single-user mode - Mode for administrative tasks.

run-level 2 Graphical multi-user with networking - Starts the system normally.

run-level 3-5 Unused but configured the same as runlevel 2

run-level 6 Reboot - Reboots the system.

The term runlevel refers to a mode of operation in one of the computer operating systems that implement Unix System V-style initialization. Conventionally, seven runlevels exist, numbered from zero to six; though up to ten, from zero to nine[citation needed], may be used. S is sometimes used as a synonym for one of the levels. Only one "runlevel" is executed on bootup - run levels are not executed sequentially, i.e. either runlevel 2 OR 3 OR 4 is executed, not 2 then 3 then 4. - en.wikipedia.org/wiki/Runlevel


It simply means at a different tty.

Quotying the linked question:

By default Ubuntu has 7 tty's.

1-6 are command line only, 7 runs your X session (your normal graphical desktop).

To access them, use this keyboard shortcut: Ctrl + Alt + F1

(changing F1 to F1-F6 to access the terminal that you need)

To get back to your X session (the normal desktop), use: Ctrl + Alt + F7

after logging on to a text-mode console simply means logging in to a different tty and typing the command:

startx

QEMU + Buildroot minimal example

My favorite way to learn what something does is by making a minimal example that runs it.

On Ubuntu, this is hard since a second startx might conflict with your current desktop.

https://askubuntu.com/a/519164/52975 proposes Xnest, but with QEMU + Buildroot we can go even more minimal and create a micro distro with only X11 installed.

This way it should be easier to understand what is going on.

I've described the distro creation at: https://unix.stackexchange.com/a/306116/32558

Once you've got the image running on QEMU, you start on a TTY shell.

Then when you do:

root
startx

it starts an X11 GUI:

enter image description here

Now take a look at the source for startx inside that distro, which is just a shell script.

It is a simple wrapper over /usr/bin/xinit, and that it passes /etc/X11/xinit/xinitrc to xinit.

If you open /etc/X11/xinit/xinitrc, it contains the following lines:

twm &
xclock -geometry 50x50-1+1 &
xterm -geometry 80x50+494+51 &
xterm -geometry 80x20+494-0 &

so we see that it starts up:

  • twm: a very simple and old window manager
  • xclock and xterm which we see on screen

For /usr/bin/xinit, we can do:

man xinit

which says that:

If no specific client program is given on the command line, xinit will look for a file in the user's home directory called .xinitrc to run as a shell script to start up client programs. If no such file exists, xinit will use the following as a default:

xterm  -geometry  +1+1  -n  login  -display  :0

If no specific server program is given on the command line, xinit will look for a file in the user's home directory called .xserverrc to run as a shell script to start up the server. If no such file exists, xinit will use the following as a default:

X  :0