Difference between revisions of "Xinitrc"

From ArchWiki
Jump to: navigation, search
m (Autostart X at login: formatting)
 
(131 intermediate revisions by 31 users not shown)
Line 1: Line 1:
 +
{{Lowercase title}}
 
[[Category:Desktop environments]]
 
[[Category:Desktop environments]]
[[Category:X Server]]
+
[[Category:X server]]
 
[[de:Xinitrc]]
 
[[de:Xinitrc]]
 
[[el:Xinitrc]]
 
[[el:Xinitrc]]
Line 7: Line 8:
 
[[it:Xinitrc]]
 
[[it:Xinitrc]]
 
[[ja:Xinitrc]]
 
[[ja:Xinitrc]]
 +
[[ru:Xinitrc]]
 
[[zh-CN:Xinitrc]]
 
[[zh-CN:Xinitrc]]
{{Article summary start}}
+
{{Related articles start}}
{{Article summary text|An overview of the primary configuration file for the xinit (startx) program.}}
+
{{Related|Display manager}}
{{Article summary heading|Related}}
+
{{Related|Xorg}}
{{Article summary wiki|Display Manager}}
+
{{Related|xprofile}}
{{Article summary wiki|Start X at Login}}
+
{{Related|Xresources}}
{{Article summary wiki|Xorg}}
+
{{Related articles end}}
{{Article summary wiki|xprofile}}
+
{{Article summary end}} {{Lowercase title}}
+
  
The {{ic|~/.xinitrc}} file is a shell script read by {{ic|xinit}} and {{ic|startx}}. It is mainly used to execute [[desktop environment]]s, [[window manager]]s and other programs when starting the X server (e.g., starting daemons and setting environment variables). The {{ic|xinit}} and {{ic|startx}} programs starts the [[Xorg|X Window System]] and works as first client programs on systems that cannot start X directly from {{ic|/etc/init}}, or in environments that use multiple window systems.
+
The {{ic|~/.xinitrc}} file is a shell script read by ''xinit'' and by its front-end ''startx''. It is mainly used to execute [[desktop environment]]s, [[window manager]]s 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 {{ic|~/.xinitrc}} is to dictate which client for the X Window System is invoked with the {{ic|/usr/bin/startx}} and/or {{ic|/usr/bin/xinit}} program on a per-user basis. There exists numerous additional specifications and commands that may also be added to {{ic|~/.xinitrc}} as you further customize your system.
+
One of the main functions of {{ic|~/.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 {{ic|~/.xinitrc}} as you further customize your system.
  
== Getting started ==
+
Most DMs also source the similar [[xprofile]] before xinit.
  
{{ic|/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 {{Pkg|xorg-xinit}} package will populate {{ic|/etc/skel}} with a framework {{ic|.xinitrc}} file.
+
== Installation ==
  
{{Note|{{ic|~/.xinitrc}} is 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 {{ic|ls}} command, usually for the sake of keeping directories tidy. Dot files may be seen by running {{ic|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".}}
+
[[Install]] the {{Pkg|xorg-xinit}} package, which provides both ''xinit''''startx'', and a default xinitrc configuration file.
  
Copy the sample {{ic|/etc/skel/.xinitrc}} file to your home directory:
+
== Configuration ==
$ cp /etc/skel/.xinitrc ~
+
  
Now, edit {{ic|~/.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]]:
+
If {{ic|.xinitrc}} is present in a user's home directory, ''startx'' and ''xinit'' execute it. Otherwise ''startx'' will run the default {{ic|/etc/X11/xinit/xinitrc}}.
 +
{{note|''Xinit'' has its own default behaviour instead of executing the file. See {{ic|man 1 xinit}} for details.}}
 +
This default xinitrc will start a basic environment with [[Twm]], {{Pkg|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 {{ic|xinitrc}} in home directory:
  
{{bc|
+
$ cp /etc/X11/xinit/xinitrc ~/.xinitrc
#!/bin/sh
+
#
+
# ~/.xinitrc
+
#
+
# Executed by startx (run your window manager from here)
+
  
if [ -d /etc/X11/xinit/xinitrc.d ]; then
+
The reason of doing this (instead of creating one from scratch) is to preserve some desired default behaviour in the original file, such as sourcing shell scripts from {{ic|/etc/X11/xinit/xinitrc.d}}. Scripts in this directory without {{ic|.sh}} extension are not sourced.
  for f in /etc/X11/xinit/xinitrc.d/*; do
+
 
    [ -x "$f" ] && . "$f"
+
Append desired commands and ''remove/comment the conflicting lines''. Remember, lines following {{ic|exec}} would be ignored. For example, to start [[Openbox#Standalone|openbox]]:
  done
+
 
  unset f
+
{{hc|~/.xinitrc|
 +
...
 +
 
 +
if [ -d /etc/X11/xinit/xinitrc.d ] ; then
 +
    for f in /etc/X11/xinit/xinitrc.d/'''?*.sh''' ; do
 +
        [ -x "$f" ] && . "$f"
 +
    done
 +
    unset f
 
fi
 
fi
  
# exec gnome-session
+
# twm &
# exec startkde
+
# xclock -geometry 50x50-1+1 &
# exec startxfce4
+
# xterm -geometry 80x50+494+51 &
# exec wmaker
+
# xterm -geometry 80x20+494-0 &
# exec icewm
+
# exec xterm -geometry 80x66+0+0 -name login
# exec blackbox
+
 
# exec fluxbox
+
## some applications that should be run in the background
# exec openbox-session
+
xscreensaver '''&'''
# ...or the Window Manager of your choice
+
xsetroot -cursor_name left_ptr '''&'''
exec xterm
+
 
 +
'''exec''' openbox-session
 
}}
 
}}
  
{{Note|Make sure to uncomment only one {{ic|exec}} line, since that will be the last command run from the script; all the following lines will just be ignored. Do NOT attempt to background your WM by appending a `&` to the line.}}
+
{{Note|At the very least, ensure that the ''if block'' in the example above is present in your {{ic|.xinitrc}} file to ensure that the scripts in {{ic|/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 {{ic|&}} 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 {{ic|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.
 +
 
 +
== Running ==
 +
 
 +
To now run Xorg as a regular user, issue:
  
After editing {{ic|~/.xinitrc}} properly, it's time to run X. To run X as a non-root user, issue:
 
 
  $ startx
 
  $ startx
$ xinit
 
$ xinit -- :1
 
  
{{Note|{{ic|xinit}} doesn't handle multiple session if you are already logged-in into some other vt. for that you have to specify session by appending {{ic|-- :''session_no''}}. If you are already running X then you should start with :1 or more.}}
+
or
  
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.
+
$ xinit -- :1 -nolisten tcp vt$XDG_VTNR
  
=== Making a DE/WM choice ===
+
Your window manager (or desktop environment) of choice should now start correctly.
  
If you aren't using any graphical login manager or don't want to, you might have to edit the {{ic|~/.xinitrc}} very frequently.
+
To quit X, run your window manager's exit function (assuming it has one). If it lacks such functionality, run:
This can be easly solved by simple few line case addition which will take the argument and load the desire DE/WM.
+
  
{{bc|
+
$ pkill -15 Xorg
#!/bin/sh
+
#
+
# ~/.xinitrc
+
#
+
# Executed by startx (run your window manager from here)
+
  
if [ -d /etc/X11/xinit/xinitrc.d ]; then
+
{{Note|''pkill'' will kill all running X instances. To specifically kill the window manager on the current VT, use:
        for f in /etc/X11/xinit/xinitrc.d/*; do
+
                [ -x "$f" ] && . "$f"
+
        done
+
        unset f
+
fi
+
  
# Here xfce is kept as default
+
{{bc|<nowiki>
case $1 in
+
WM_PID=$(xprop -id $(xprop -root _NET_SUPPORTING_WM_CHECK \
        gnome) exec gnome-session;;
+
| awk -F'#' '{ print $2 }') _NET_WM_PID \
        kde) exec startkde;;
+
| awk -F' = ' '{ print $2 }')
        xfce);;
+
 
        *) exec startxfce4;;
+
kill -15 $WM_PID</nowiki>}}
esac
+
 
 +
The program {{ic|xprop}} is provided by the package {{Pkg|xorg-xprop}} in the [[official repositories]].
 
}}
 
}}
  
After editing {{ic|~/.xinitrc}}, you can easily start desire DE/WM by passing argument.
+
{{Note|
$ xinit
+
* The above commands run [[Xorg]] on the same virtual terminal the user is logged in to. [http://blog.falconindy.com/articles/back-to-basics-with-x-and-systemd.html] This maintains an authenticated session with {{ic|logind}}, and prevents bypassing the screen locker by switching terminals.
$ xinit gnome
+
* You have to specify {{ic|vt$XDG_VTNR}} as command line option for ''xinit'' in order to [[General troubleshooting#Session permissions|preserve session permissions]].
$ xinit kde
+
* ''xinit'' does not handle multiple sessions when already logged-in into a different virtual terminal. For that you must specify the session by appending {{ic|-- :''session_no''}}. If X is already running, then you should start with :1 or more.
$ xinit xfce -- :1
+
* By default, due to permissions on console devices, the X display needs to be on the same tty where the login occurred. This is handled by the default {{ic|/etc/X11/xinit/xserverrc}}. See [[General troubleshooting#Session permissions]] for details.
 +
* 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 {{ic|/usr/lib/systemd/systemd-multi-seat-x}}. For convenience, ''startx'' can be set up to use this wrapper by modifying your {{ic|~/.xserverrc}}.
 +
* If you choose to use ''xinit'' instead of ''startx'', you are responsible for passing {{ic|-nolisten tcp}} and ensuring the session does not break by starting X on a different tty.
 +
* If X terminates with error message "SocketCreateListener() failed", you may need to delete socket files in {{ic|/tmp/.X11-unix}}. This may happen if you have previously run Xorg as root (e.g. to generate an {{ic|xorg.conf}}, as below).
 +
}}
  
=== Preserving the session ===
+
== Autostart X at login ==
  
X must always be run on the same tty where the login occurred, to preserve the logind session. This is handled by the default {{ic|/etc/X11/xinit/xserverrc}}. Also see [[General Troubleshooting#Session permissions]] for related issues.
+
{{Note|These solutions run X on the same tty used to login, which is required in order to maintain the login session.}}
  
{{Note|In the past {{ic|ck-launch-session}} was used to start a '''new''' session instead of simply not breaking the old one. There is no equivalent to this hack with {{ic|logind}}, and a multi-seat aware display manager is required to run X on an arbitrary tty.}}
+
For [[Bash]], add the following to the bottom of {{ic|~/.bash_profile}}. If the file does not exist, copy a skeleton version from {{ic|/etc/skel/.bash_profile}}.
  
== File examples==
+
For [[Zsh]], add it to {{ic|~/.zprofile}}.
  
Following is a simple {{ic|~/.xinitrc}} file example, including some startup programs:
+
{{bc|1=<nowiki>
{{hc|~/.xinitrc|<nowiki>
+
[[ -z $DISPLAY && $XDG_VTNR -eq 1 ]] && exec startx
#!/bin/sh
+
</nowiki>}}
  
if [ -d /etc/X11/xinit/xinitrc.d ]; then
+
{{Note|
        for f in /etc/X11/xinit/xinitrc.d/*; do
+
* You can replace the {{ic|-eq 1}} comparison with one like {{ic|-le 3}} (for vt1 to vt3) if you want to use graphical logins on more than one VT.
                [ -x "$f" ] && . "$f"
+
* X must always be run on the same tty where the login occurred, to preserve the logind session. This is handled by the default {{ic|/etc/X11/xinit/xserverrc}}.
        done
+
* {{ic|xinit}} may be faster than {{ic|startx}}, but needs additional parameter such as {{ic|-nolisten tcp}}.
        unset f
+
* If you would like to remain logged in when the X session ends, remove {{ic|exec}}.
fi
+
}}
  
xrdb -merge ~/.Xresources        # update x resources db
+
See also [[Fish#Start X at login]] and [[Systemd/User#Automatic login into Xorg without display manager]].
  
xscreensaver -no-splash &        # starts screensaver daemon
+
=== Automatic login to the virtual console ===
xsetroot -cursor_name left_ptr &  # sets the cursor icon
+
sh ~/.fehbg &                    # sets the background image
+
  
exec openbox-session              # starts the window manager
+
This method can be combined with [[automatic login to virtual console]].
</nowiki>}}
+
  
Prepending {{ic|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.
+
== Tips and tricks ==
  
== File configuration ==
+
=== Override xinitrc from command line ===
  
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 {{ic|~/.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.
+
If you have a working {{ic|~/.xinitrc}}, but just want to try other WM/DE, you can run it by issuing ''startx'' followed by the path to the window manager:
  
Note that in the first example above, programs such as {{ic|cairo-compmgr}}, {{ic|xscreensaver}}, {{ic|xsetroot}} and {{ic|sh}} are run in the background ({{ic|&}} suffix added). Otherwise, the script would halt and wait for each program and daemons to exit before executing {{ic|openbox-session}}. Also note that {{ic|openbox-session}} is not backgrounded. This ensures that the script will not quit until openbox does.
+
$ startx /full/path/to/window-manager
  
The following sections explains how to configure {{ic|~/.xinitrc}} for multiple WMs and DEs.
+
If the window manager takes arguments, they need to be enquoted to be recognized as part of the first parameter of ''startx'':
  
=== On the command line ===
+
$ startx "/full/path/to/window-manager --key value"
  
If you have a working {{ic|~/.xinitrc}}, but just want to try other WM/DE you can run it by issuing {{ic|xinit}} followed by the path to the window manager:
+
Note that the full path is '''required'''. Optionally, you can also override {{ic|/etc/X11/xinit/xserverrc}} file (which stores the default X server options) with custom options by appending them after {{ic|--}}, e.g.:
  
  $ xinit /full/path/to/window-manager
+
  $ startx /usr/bin/enlightenment -- -nolisten tcp -br +bs -dpi 96 vt$XDG_VTNR
  
Note that the full path is '''required'''. Optionally, you can pass options to the X server after appending {{ic|--}} - e.g.:
+
or
  
  $ xinit /usr/bin/enlightenment -- -br +bs -dpi 96
+
  $ xinit /usr/bin/enlightenment -- -nolisten tcp -br +bs -dpi 96 vt$XDG_VTNR
  
The following example {{ic|~/.xinitrc}} shows how to start a particular window manager with an argument:
+
See also {{ic|man startx}}.
 +
 
 +
{{Tip|This can be used even to start a regular GUI programs but without any of the window manager features. See also [[#Starting applications without a window manager]] and [[Running program in separate X display]].}}
 +
 
 +
=== Making a DE/WM choice ===
 +
 
 +
If you are frequently switching between different DEs/WMs, it is recommended to either use a [[Display manager]] or add code to {{ic|.xinitrc}}. The code described next consists of a simple few lines, which will take the argument and load the desired desktop environment or window manager.
 +
 
 +
The following example {{ic|~/.xinitrc}} shows how to start a particular DE/WM with an argument:
  
 
{{hc|~/.xinitrc|<nowiki>
 
{{hc|~/.xinitrc|<nowiki>
#!/bin/sh
+
...
#
+
# ~/.xinitrc
+
#
+
# Executed by startx (run your window manager from here)
+
  
if [ -d /etc/X11/xinit/xinitrc.d ]; then
+
# Here Xfce is kept as default
        for f in /etc/X11/xinit/xinitrc.d/*; do
+
session=${1:-xfce}
                [ -x "$f" ] && . "$f"
+
        done
+
        unset f
+
fi
+
  
if [[ $1 == "fluxbox" ]]; then
+
case $session in
        exec startfluxbox
+
    awesome          ) exec awesome;;
elif [[ $1 == "spectrwm" ]]; then
+
    bspwm            ) exec bspwm;;
        exec spectrwm
+
    catwm            ) exec catwm;;
else
+
    cinnamon          ) exec cinnamon-session;;
        echo "Choose a window manager"
+
    dwm              ) exec dwm;;
fi
+
    enlightenment    ) exec enlightenment_start;;
 +
    ede              ) exec startede;;
 +
    fluxbox          ) exec startfluxbox;;
 +
    gnome            ) exec gnome-session;;
 +
    gnome-classic    ) exec gnome-session --session=gnome-classic;;
 +
    i3|i3wm          ) exec i3;;
 +
    icewm            ) exec icewm-session;;
 +
    jwm              ) exec jwm;;
 +
    kde              ) exec startkde;;
 +
    mate              ) exec mate-session;;
 +
    monster|monsterwm ) exec monsterwm;;
 +
    notion            ) exec notion;;
 +
    openbox          ) exec openbox-session;;
 +
    unity            ) exec unity;;
 +
    xfce|xfce4        ) exec startxfce4;;
 +
    xmonad            ) exec xmonad;;
 +
    # No known session, try to run it as command
 +
    *) exec $1;;
 +
esac
 
</nowiki>}}
 
</nowiki>}}
  
Using this example you can start fluxbox or spectrwm with the command {{ic|xinit fluxbox}} or {{ic|xinit spectrwm}}.
+
Then copy the {{ic|/etc/X11/xinit/xserverrc}} file to your home directory:
 +
 
 +
$ cp /etc/X11/xinit/xserverrc ~/.xserverrc
 +
 
 +
After that, you can easily start a particular DE/WM by passing an argument, e.g.:
 +
 
 +
$ xinit
 +
$ xinit gnome
 +
$ xinit kde
 +
$ xinit wmaker
 +
 
 +
or
 +
 
 +
$ startx
 +
$ startx ~/.xinitrc gnome
 +
$ startx ~/.xinitrc kde
 +
$ startx ~/.xinitrc wmaker
 +
 
 +
=== 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:
 +
 
 +
{{hc|~/.xinitrc|
 +
...
 +
 
 +
exec chromium
 +
}}
 +
 
 +
With this method you need to set each application window's geometry through its own configuration files, if possible at all.
  
=== At startup ===
+
{{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.}}
  
See [[Start X at Login]].
+
See also [[Display manager#Starting applications without a window manager]].

Latest revision as of 12:06, 19 May 2016

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.

Most DMs also source the similar xprofile before xinit.

Installation

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

Configuration

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 man 1 xinit 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

The reason of doing this (instead of creating one from scratch) is to preserve some desired default behaviour in the original file, such as sourcing shell scripts from /etc/X11/xinit/xinitrc.d. Scripts in this directory without .sh extension are not sourced.

Append desired commands and remove/comment the conflicting lines. Remember, lines following exec would be ignored. For example, to start openbox:

~/.xinitrc
...

if [ -d /etc/X11/xinit/xinitrc.d ] ; then
    for f in /etc/X11/xinit/xinitrc.d/?*.sh ; do
        [ -x "$f" ] && . "$f"
    done
    unset f
fi

# twm &
# xclock -geometry 50x50-1+1 &
# xterm -geometry 80x50+494+51 &
# xterm -geometry 80x20+494-0 &
# exec xterm -geometry 80x66+0+0 -name login

## some applications that should be run in the background
xscreensaver &
xsetroot -cursor_name left_ptr &

exec openbox-session
Note: At the very least, ensure that the if block in the example above 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.

Running

To now run Xorg as a regular user, issue:

$ startx

or

$ xinit -- :1 -nolisten tcp vt$XDG_VTNR

Your window manager (or desktop environment) of choice should now start correctly.

To quit X, run your window manager's exit function (assuming it has one). If it lacks such functionality, run:

$ pkill -15 Xorg
Note: pkill will kill all running X instances. To specifically kill the window manager on the current VT, use:
WM_PID=$(xprop -id $(xprop -root _NET_SUPPORTING_WM_CHECK \
| awk -F'#' '{ print $2 }') _NET_WM_PID \
| awk -F' = ' '{ print $2 }')

kill -15 $WM_PID

The program xprop is provided by the package xorg-xprop in the official repositories.

Note:
  • The above commands run Xorg on the same virtual terminal the user is logged in to. [1] This maintains an authenticated session with logind, and prevents bypassing the screen locker by switching terminals.
  • You have to specify vt$XDG_VTNR as command line option for xinit in order to preserve session permissions.
  • xinit does not handle multiple sessions when already logged-in into a different virtual terminal. For that you must specify the session by appending -- :session_no. If X is already running, then you should start with :1 or more.
  • By default, due to permissions on console devices, the X display needs to be on the same tty where the login occurred. This is handled by the default /etc/X11/xinit/xserverrc. See General troubleshooting#Session permissions for details.
  • 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, startx can be set up to use this wrapper by modifying your ~/.xserverrc.
  • If you choose to use xinit instead of startx, you are responsible for passing -nolisten tcp and ensuring the session does not break by starting X on a different tty.
  • If X terminates with error message "SocketCreateListener() failed", you may need to delete socket files in /tmp/.X11-unix. This may happen if you have previously run Xorg as root (e.g. to generate an xorg.conf, as below).

Autostart X at login

Note: These solutions run X on the same tty used to login, which is required in order to maintain the login session.

For Bash, add the following to the bottom of ~/.bash_profile. If the file does not exist, copy a skeleton version from /etc/skel/.bash_profile.

For Zsh, add it to ~/.zprofile.

[[ -z $DISPLAY && $XDG_VTNR -eq 1 ]] && exec startx
Note:
  • 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 VT.
  • X must always be run on the same tty where the login occurred, to preserve the logind session. This is handled by the default /etc/X11/xinit/xserverrc.
  • xinit may be faster than startx, but needs additional parameter such as -nolisten tcp.
  • If you would like to remain logged in when the X session ends, remove exec.

See also Fish#Start X at login and Systemd/User#Automatic login into Xorg without display manager.

Automatic login to the virtual console

This method can be combined with automatic login to virtual console.

Tips and tricks

Override xinitrc from command line

If you have a working ~/.xinitrc, but just want to try other WM/DE, you can run it by issuing startx followed by the path to the window manager:

$ startx /full/path/to/window-manager

If the window manager takes arguments, they need to be enquoted to be recognized as part of the first parameter of startx:

$ startx "/full/path/to/window-manager --key value"

Note that the full path is required. Optionally, you can also override /etc/X11/xinit/xserverrc file (which stores the default X server options) with custom options by appending them after --, e.g.:

$ startx /usr/bin/enlightenment -- -nolisten tcp -br +bs -dpi 96 vt$XDG_VTNR

or

$ xinit /usr/bin/enlightenment -- -nolisten tcp -br +bs -dpi 96 vt$XDG_VTNR

See also man startx.

Tip: This can be used even to start a regular GUI programs but without any of the window manager features. See also #Starting applications without a window manager and Running program in separate X display.

Making a DE/WM choice

If you are frequently switching between different DEs/WMs, it is recommended to either use a Display manager or add code to .xinitrc. The code described next consists of a simple few lines, which will take the argument and load the desired desktop environment or window manager.

The following example ~/.xinitrc shows how to start a particular DE/WM with an argument:

~/.xinitrc
...

# Here Xfce is kept as default
session=${1:-xfce}

case $session in
    awesome           ) exec awesome;;
    bspwm             ) exec bspwm;;
    catwm             ) exec catwm;;
    cinnamon          ) exec cinnamon-session;;
    dwm               ) exec dwm;;
    enlightenment     ) exec enlightenment_start;;
    ede               ) exec startede;;
    fluxbox           ) exec startfluxbox;;
    gnome             ) exec gnome-session;;
    gnome-classic     ) exec gnome-session --session=gnome-classic;;
    i3|i3wm           ) exec i3;;
    icewm             ) exec icewm-session;;
    jwm               ) exec jwm;;
    kde               ) exec startkde;;
    mate              ) exec mate-session;;
    monster|monsterwm ) exec monsterwm;;
    notion            ) exec notion;;
    openbox           ) exec openbox-session;;
    unity             ) exec unity;;
    xfce|xfce4        ) exec startxfce4;;
    xmonad            ) exec xmonad;;
    # No known session, try to run it as command
    *) exec $1;;
esac

Then copy the /etc/X11/xinit/xserverrc file to your home directory:

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

After that, you can easily start a particular DE/WM by passing an argument, e.g.:

$ xinit
$ xinit gnome
$ xinit kde
$ xinit wmaker

or

$ startx
$ startx ~/.xinitrc gnome
$ startx ~/.xinitrc kde
$ startx ~/.xinitrc wmaker

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.

See also Display manager#Starting applications without a window manager.