xinit

The ~/.xinitrc file is a shell script read by xinit and by its front-end startx. It is mainly used to execute desktop environments, window managers and other programs when starting the X server (e.g., starting daemons and setting environment variables). The xinit program starts the X Window System server and works as first client program on systems that are not using a display manager.

One of the main functions of ~/.xinitrc is to dictate which client for the X Window System is invoked with startx or xinit programs on a per-user basis. There exists numerous additional specifications and commands that may also be added to ~/.xinitrc as you further customize your system.

Installation

Install the xorg-xinit package, which provides both xinit, startx, and a default xinitrc configuration file.

Configuration

xserverrc

The xserverrc file is a shell script responsible for starting up the X server. Both startx and xinit execute ~/.xserverrc if it exists, startx will use /etc/X11/xinit/xserverrc otherwise.

In order to maintain an authenticated session with logind and to prevent bypassing the screen locker by switching terminals, Xorg has to be started on the same virtual terminal where the login occurred.[1] Therefore it is recommended to specify vt$XDG_VTNR in the ~/.xserverrc file:

~/.xserverrc

#!/bin/sh
exec /usr/bin/Xorg -nolisten tcp "$@" vt$XDG_VTNR

Alternatively, if you wish to have the X display on a separate console from the one where the server is invoked, you can do so by using the X server wrapper provided by /usr/lib/systemd/systemd-multi-seat-x. For convenience, xinit and startx can be set up to use this wrapper by modifying your ~/.xserverrc.

Note: To re-enable redirection of the output from X session into the Xorg log file, add the -keeptty option. See Xorg#Broken redirection for details.

xinitrc

If .xinitrc is present in a user's home directory, startx and xinit execute it. Otherwise startx will run the default /etc/X11/xinit/xinitrc.

Note: Xinit has its own default behaviour instead of executing the file. See xinit(1) for details.

This default xinitrc will start a basic environment with Twm, xorg-xclock and Xterm (assuming that the necessary packages are installed). Therefore, to start a different window manager or desktop environment, first create a copy of the default xinitrc in home directory:

$ cp /etc/X11/xinit/xinitrc ~/.xinitrc

Then edit the file and replace the default programs with desired commands. Remember that lines following a command using exec would be ignored. For example, to start xscreensaver in the background and then start openbox, use the following:

~/.xinitrc

xscreensaver &
exec openbox-session

Note: At the very least, ensure that the last if block in /etc/X11/xinit/xinitrc is present in your .xinitrc file to ensure that the scripts in /etc/X11/xinit/xinitrc.d are sourced.

Long-running programs started before the window manager, such as a screensaver and wallpaper application, must either fork themselves or be run in the background by appending an & sign. Otherwise, the script would halt and wait for each program to exit before executing the window manager or desktop environment. Note that some programs should instead not be forked, to avoid race bugs, as is the case of xrdb. Prepending exec will replace the script process with the window manager process, so that X does not exit even if this process forks to the background.

Usage

To now run Xorg as a regular user, issue:

$ startx

or

$ xinit -- :1

Note: xinit does not handle multiple displays when another X server is already started. For that you must specify the display by appending -- :display_number, where display_number is 1 or more.

You can replace the -eq 1 comparison with one like -le 3 (for vt1 to vt3) if you want to use graphical logins on more than one virtual terminal.

Alternative conditions to detect the virtual terminal include "$(tty)" = "/dev/tty1", which does not allow comparison with -le, and "$(fgconsole 2>/dev/null || echo -1)" -eq 1, which does not work in serial consoles.

If you would like to remain logged in when the X session ends, remove exec.

Starting applications without a window manager

It is possible to start only specific applications without a window manager, although most likely this is only useful with a single application shown in full-screen mode. For example:

~/.xinitrc

...
exec chromium

With this method you need to set each application window's geometry through its own configuration files, if possible at all.

Tip: This method can be useful to launch graphical games, especially on systems where excluding the memory or CPU usage of a window manager or desktop environment, and possible accessory applications, can help improve the game's execution performance.