zh-CN:Xinitrc Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary end
~/.xinitrc file is a shell script read by
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
startx programs starts the X Window System and works as first client programs on systems that cannot start X directly from
/etc/init, or in environments that use multiple window systems.
One of the main functions of
~/.xinitrc is to dictate which client for the X Window System is invoked with the
/usr/bin/xinit program 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.
/etc/skel/ contains files and directories to provide sane defaults for newly created user accounts. (The name skel is derived from the word skeleton, because the files it contains form the basic structure for users' home directories.) The package will populate
/etc/skel with a framework
~/.xinitrcis a so-called 'dot' (.) file. Files in a *nix file system which are preceded with a dot (.) are 'hidden' and will not show up with a regular
lscommand, usually for the sake of keeping directories tidy. Dot files may be seen by running
ls -A. The 'rc' denotes Run Commands and simply indicates that it is a configuration file. Since it controls how a program runs, it is (although historically incorrect) also said to stand for "Run Control".
Copy the sample
/etc/skel/.xinitrc file to your home directory:
$ cp /etc/skel/.xinitrc ~
~/.xinitrc and uncomment the line that corresponds to your DE/WM. For example, if you want to test your basic X configuration (mouse, keyboard, graphics resolution), you can simply use xterm:
#!/bin/sh # # ~/.xinitrc # # Executed by startx (run your window manager from here) if [ -d /etc/X11/xinit/xinitrc.d ]; then for f in /etc/X11/xinit/xinitrc.d/*; do [ -x "$f" ] && . "$f" done unset f fi # exec gnome-session # exec startkde # exec startxfce4 # exec wmaker # exec icewm # exec blackbox # exec fluxbox # exec openbox-session # ...or the Window Manager of your choice exec xterm
execline is uncommented, or else only the first uncommented line will be run.
~/.xinitrc properly, it's time to run X. To run X as a non-root user, issue:
$ startx -- vt$XDG_VTNR
Your DE/WM of choice should now start up. You are now free to test your keyboard with its layout, moving your mouse around and of course enjoy the view.
Preserving the session
You need to make sure X will always start on the same tty where you logged in, to preserve your logind (or consolekit) session. This can be done either by include
exec /usr/bin/X -nolisten tcp vt$XDG_VTNR "$@" in
~/.xserverrc or using
startx -- vt$XDG_VTNR.
ck-launch-sessionwas used to start a new session instead of simply not breaking the old one. There is no equivalent to this hack with
logind, and a multi-seat aware display manager is required to run X on an arbitrary tty.
Following is a simple
~/.xinitrc file example, including some startup programs:
#!/bin/sh if [ -d /etc/X11/xinit/xinitrc.d ]; then for f in /etc/X11/xinit/xinitrc.d/*; do [ -x "$f" ] && . "$f" done unset f fi xrdb -merge ~/.Xresources # update x resources db xscreensaver -no-splash & # starts screensaver daemon xsetroot -cursor_name left_ptr & # sets the cursor icon sh ~/.fehbg & # sets the background image exec openbox-session # starts the window manager
exec is recommended as it replaces the current process with the process, so the script will stop running and X won't exit even if the process forks into the background.
When a display manager is not used, it is important to keep in mind that the life of the X session starts and ends with
~/.xinitrc. This means that once the script quits, X quits regardless of whether you still have running programs (including your window manager). Therefore it's important that the window manager quitting and X quitting should coincide. This is easily achieved by running the window manager as the last program in the script.
Note that in the first example above, programs such as
sh are run in the background (
& suffix added). Otherwise, the script would halt and wait for each program and daemons to exit before executing
openbox-session. Also note that
openbox-session is not backgrounded. This ensures that the script will not quit until openbox does.
The following sections explains how to configure
~/.xinitrc for Multiple WMs and DEs.
On the command line
If you have a working
~/.xinitrc, but just want to try other WM/DE you can run it by issuing
xinit followed by the path to the window manager:
Note that the full path is required. Optionally, you can pass options to the X server after appending
-- - e.g.:
xinit /usr/bin/enlightenment -- -br +bs -dpi 96
The following example
~/.xinitrc shows how to start a particular window manager with an argument:
#!/bin/sh # # ~/.xinitrc # # Executed by startx (run your window manager from here) if [ -d /etc/X11/xinit/xinitrc.d ]; then for f in /etc/X11/xinit/xinitrc.d/*; do [ -x "$f" ] && . "$f" done unset f fi if [[ $1 == "fluxbox" ]] then exec startfluxbox elif [[ $1 == "spectrwm" ]] then exec spectrwm else echo "Choose a window manager" fi
Using this example you can start fluxbox or spectrwm with the command
xinit fluxbox or
See Start X at Login.
Tips and tricks
Various desktop environment features and applications expect PolicyKit and to be active, and will produce errors if absent. A common error to notice is the inability to access shutdown or reboot prompts from desktop environment windows, and failure to mount USB devices, CD-ROMs etc. without root permissions. Most modern display managers will start PolicyKit automatically. When only using
~/.xinitrc with e.g. startx, you need to make sure that X starts on the same tty where you logged in.