Kindly support DOTSLASHLINUX on Patreon to keep the website up and running while remaining ads free.

Starting Xorg Server using xinit

xinit
Firas Khalil Khana | 29/03/2017

In this article I’ll show you how to correctly setup xinit to start your xorg server. I’ll also demonstrate how you can auto startx once logged in a tty.


1- Installation


Gentoo Linux:

emerge --sync && emerge -av x11-apps/xinit


Void Linux:

xbps-install -Su && xbps-install -S xinit


Arch Linux:

pacman -Syu xorg-xinit

2- xserverrc


In order to correctly setup xinit, you need to copy the global xserverrc to your home directory and name it as .xserverrc:

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


Now, in order to maintain an authenticated session and prevent xauth from complaining about a missing .serverauth file every time you startx, you may need to modify you .xserverrc file. If your .xserverrc file looks like this:

#!/bin/sh
if [ -z "$XDG_VTNR" ]; then
  exec /usr/bin/X -nolisten tcp "$@"
else
  exec /usr/bin/X -nolisten tcp "$@" vt$XDG_VTNR
fi


Then you don’t need to worry about an unautheticated X session, however if it were like this:

#!/bin/sh
exec /usr/bin/X -nolisten tcp "$@"


Then you need to add vt$XDG_VTNR to the end of the command. This states that Xorg has to be started on the same virtual terminal where the login occurred.

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


If the problem persists (xauth keeps complaining about missing file /home/user/serverauth), you can edit the startx script with your favorite editor:

vim /usr/bin/startx


Then change enable_xauth=1 to enable_xauth=0 (line 29 here):

#!/bin/sh

#
# This is just a sample implementation of a slightly less primitive
# interface than xinit. It looks for user .xinitrc and .xserverrc
# files, then system xinitrc and xserverrc files, else lets xinit choose
# its default. The system xinitrc should probably do things like check
# for .Xresources files and merge them in, start up a window manager,
# and pop a clock and several xterms.
#
# Site administrators are STRONGLY urged to write nicer versions.
#

unset DBUS_SESSION_BUS_ADDRESS
unset SESSION_MANAGER
userclientrc=$HOME/.xinitrc
sysclientrc=/etc/X11/xinit/xinitrc

userserverrc=$HOME/.xserverrc
sysserverrc=/etc/X11/xinit/xserverrc
defaultclient=xterm
defaultserver=/usr/bin/X
defaultclientargs=""
defaultserverargs=""
defaultdisplay=":0"
clientargs=""
serverargs=""
vtarg=""
enable_xauth=0 # Change this from 1 to 0
...

3- xinitrc


As we did with xserverrc, we need to copy the global xinitrc to our home directory and name it as .xinitrc:

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


The global xinitrc configuration file (which will be executed if you forgot to make your own in your home directory) has the following:

#!/bin/sh

userresources=$HOME/.Xresources
usermodmap=$HOME/.Xmodmap
sysresources=/etc/X11/xinit/.Xresources
sysmodmap=/etc/X11/xinit/.Xmodmap

# merge in defaults and keymaps

if [ -f $sysresources ]; then







    xrdb -merge $sysresources

fi

if [ -f $sysmodmap ]; then
    xmodmap $sysmodmap
fi

if [ -f "$userresources" ]; then







    xrdb -merge "$userresources"

fi

if [ -f "$usermodmap" ]; then
    xmodmap "$usermodmap"
fi

# start some nice programs

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


Notice lines 51 to 55, these are the only lines that you should edit! These lines indicate that twm, xclock and 3 xterm instances will be started (if installed obviously) once an Xorg session starts.

Since these are very outdated versions of what we intend to use, delete lines 51 to 55 and replace them with your favored session. In our case we love dwm and openbox delete lines 51 to 55 and add this for dwm:

exec dwm


or this for openbox:

exec openbox-session


Please be noted that the program(s) that you intend to start must be installed!


4- startx


Once you’ve done configuring xinit, you can now start your X session simply by running:

startx

(Optional) Auto startx at Login

- BASH

If you’re using BASH you can automate the process of startx after logging in by adding this line to your ~/.bash_profile (create this file if it doesn’t exist):

if [[ -z $DISPLAY ]] && [[ $(tty) = /dev/tty1 ]]; then exec startx; fi

- Other Shells

If you’re using dash or even plain sh, then you may want a more portable version of the BASH-exclusive code above, and you’ll need to add it to your ~/.profile (create this file if it doesn’t exist):

if [ -z $DISPLAY ] && [ $(tty) = /dev/tty1 ]; then exec startx; fi

4 Comments

oldgaro

15/12/2017


Gentoo: Auto startx at login

[[ -t 0 && $(tty) == /dev/tty1 && ! $DISPLAY ]] && exec startx

DOTSLASHLINUX

15/12/2017


@oldgaro, what an active son of a tux you are :)

Thanks for your positive feedback and your awesome replies in several articles ;P

random

06/01/2018


Or If you are not a bash user. You can put this in /etc/profile: if [[ “$(tty)” == ‘/dev/tty1’ ]]; then exec startx fi

DOTSLASHLINUX

06/01/2018


@random, thanks for stopping by and for your suggestion.

I don’t think that what you suggested though would work for all non-bash users as you’ve used the bashism “[[“, perhaps if it were “[” then it would work. Also adding it to a ~/.profile would be the wiser thing to do.

Either way, article has been updated to include an auto-startx method for non-BASH users.

Comments undergo validation, spam detection and approval before being posted here, so kindly wait at least 1-2 hours before resubmitting your comments. Thanks for understanding.

Leave A Comment