Runlevels

Most users run X from one of two runlevels: 3 or 5. Runlevel 3 places your system in multi-user mode with full networking capabilities. The machine will boot to a text-based login prompt with all necessary preconfigured services started. Most servers are run in runlevel 3, as X is not necessary to provide any services utilized by most users. Runlevel 5 is similar to 3, except that it automatically starts X and provides a graphical login screen. Many workstation users prefer this method, because it never forces them to see a command prompt.

The default runlevel used when your system boots can be found in the /etc/inittab file. If you have a line in that file that looks like id:3:initdefault:, then your system will boot to runlevel 3. If you have a line that looks like id:5:initdefault:, your system is set to boot into runlevel 5. As root, change the runlevel number in this file to set a different default. Save the file and restart your system to verify that it boots to the correct runlevel. More information on runlevels can be found in the section called Init Runlevels in Chapter 3.

Each of these runlevels start X in a slightly different way.

Runlevel 3: startx

When in runlevel 3, the preferred way to start an X session is to type the startx command. startx is a front-end to the xinit program, which launches the XFree86 server and connects the X clients to it. Because you must already be logged into the system at runlevel 3 to be able to type commands, startx is only designed to bring up certain X clients, such as a desktop environment, in a particular manner. It does not provide any user authentication.

When startx begins, it looks for a user-defined .xinitrc file in the user's home directory to define the X clients to run. If it does not find that file present, it will run the system default /etc/X11/xinit/xinitrc script instead. The startx script then does the same thing with the .xserverrc file, looking for it in the user's home directory and then running the default /etc/X11/xinit/xserverrc script if it cannot be found. Since many different X clients exist, the xinitrc files are very important. The xserverrc script is less important. It only sets the X server to connect to the X clients. Since the default X server is already configured with the /etc/X11/X link, Red Hat Linux does not install a default xserverrc.

The default xinitrc script then looks for user-defined files and default system files, including .Xresources, .Xmodmap, and .Xkbmap in the user's home directory and Xresources, Xmodmap, and Xkbmap in the /etc/X11 directory. The Xmodmap and Xkbmap files, if they exist, are used by the xmodmap utility to configure the keyboard. The Xresources files are read to assign specific preference values to particular applications.

After setting these options, the xinitrc script executes all scripts in /etc/X11/xinit/xinitrc.d. One important script in this directory is xinput, which configures settings such as the default language to use and the desktop environment to start (from /etc/sysconfig/desktop).

Next, the xinitrc script tries to execute .Xclients in the user's home directory, and turns to /etc/X11/init/Xclients if it cannot be found. The purpose of the Xclients file is to start the desktop environment or, possibly, just a basic window manager. The .Xclients script in the user's home directory starts the user-specified desktop environment or window manager in the .Xclients-default file. If .Xclients does not exist in the user's home directory, the standard /etc/X11/init/Xclients script attempts to start another desktop environment, trying GNOME first and then KDE. If a desktop environment cannot be found by this point, Xclients cycles through a list of window managers to find the one to start, after attempting the default window manager listed in the .wm_style file in the user's home directory.

By this point, the preferred X client applications should be started, along with the XFree86 server. If you need more details concerning starting X in runlevel 3, refer to the startx and xinit man pages and read through the scripts named above.

Runlevel 5: prefdm

Runlevel 5 uses a slightly different method to start X. When the system starts, no one is logged into the system by default. In order for a user to start a session, they must log in to the system. In runlevel 5, users authenticating themselves at the console use a display manager, a special X client that allows the user to submit their login name and password.

Depending on the desktop environments installed on your specific Red Hat Linux system, three different display managers are available to handle the user authentication. The xdm display manager is the original X authentication tool. xdm only allows you to log in and start an X session, nothing more. The gdm display manager, designed to work with the GNOME desktop environment, and kdm display manager, used with the KDE desktop environment, allow you to set the desktop environment, or session, you would like to use after authentication. Additionally, you can restart or halt the system from the login screen. The gdm display manager also allows you to configure the language you would like to use.

When the system enters runlevel 5, a line in the /etc/inittab file specifies that the prefdm script is executed in order to determine the preferred display manager to bring up for user authentication. The prefdm script uses the preferences stated in the /etc/sysconfig/desktop file to find the proper display manager. If no desktop environment is specified, prefdm cycles through the gdm, kdm, and xdm display managers to find one to use. Once one is found, prefdm launches it to handle the user login.

Each of the display managers look to the /etc/X11/xdm/Xsetup_0 file to set up the login screen. Once the user logs into the system, the /etc/X11/xdm/GiveConsole script runs to assign ownership of the console to the user. Then, the /etc/X11/xdm/Xsession script runs to accomplish many of the tasks normally done by the xinitrc script when starting X in runlevel 3, including setting system and user resources, as well as running the scripts in the /etc/X11/xinit/xinitrc.d directory.

The user can specify which desktop environment they want to utilize when they authenticate using the gdm or kdm display managers by selecting it from the Session menu. If the desktop environment is not specified in the display manager, the /etc/X11/xdm/Xsession script will check the .xsession and .Xclients files in the user's home directory to decide which desktop environment to load. As a last resort, the /etc/X11/xinit/Xclients file is used to select a desktop environment or window manager to use in the same way as runlevel 3.

When the user finishes an X session on the default display (:0) and logs out, the /etc/X11/xdm/TakeConsole script runs and reassigns ownership of the console to the root user. The original display manager, which continued running after the user logged in, takes control by spawning a new display manager. This restarts the XFree86 server, displays a new login window, and starts the entire process over again.

For more information about how display managers control user authentication, read the xdm man page.