68

What is the purpose of the .xsession file in the home folder? What should be put in there? The desktop environments don't use that file and for the X startup from the tty there is .xinitrc.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Mirzhan Irkegulov
  • 8,248
  • 11
  • 45
  • 54

4 Answers4

76

If you log in in text mode then start a GUI session with xinit or with the wrapper script startx, then xinit does the following things:

  • Start an X server (typically through the script /etc/X11/xinit/xserverrc).
  • Usually run some scripts in /etc/X11 (typically /etc/X11/xinit/xinitrc), depending on how it's set up.
  • Run ~/.xinitrc, if it exists. If it doesn't exist, run a default client (traditionally xterm).
  • Once ~/.xinitrc terminates, kill the X server.

If you log in in graphical mode on an X display manager (xdm, gdm, kdm, wdm, lightdm, …), traditionally, what is executed after you log in is some scripts in /etc/X11 then ~/.xsession.

~/.xsession has the role of ~/.profile and ~/.xinitrc combined: it's supposed to perform the initial startup of your session (e.g. define environment variables), then launch programs specific to the GUI (usually at least window manager).

Nowadays, most X display managers give you a choice of a session. Choosing a particular session launched a specific desktop environment, session manager, window manager. What is executed then is only that DE/SM/WM and whatever programs it chooses to start based on whatever configuration files it chooses to read. Many environments provide a “custom session” that reads the traditional ~/.xsession.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • 3
    I don't know what is this "many environments", but at least kdm silently ignores the «~/.xsession» file. – Hi-Angel Feb 18 '15 at 19:18
  • 2
    @Hi-Angel The `.xsession` file is only read if you select a particular session type. I know that for Gdm, the upstream developers don't want to include one that runs `.xsession` by default, and leave it up to each distribution, and many distributions don't bother or don't realize the need. I don't know what the kdm situation is. – Gilles 'SO- stop being evil' Feb 18 '15 at 20:12
  • Well, you know, I [found in man kdm](http://www.linuxcertif.com/man/1/kdm/) that kdm have an own file `/etc/kde4/kdm/Xsession` to be ran as user just after login. They're lie — the file *(which is present btw)* never being touched by kdm, I even did removed it at all, and that didn't changed anything. – Hi-Angel Feb 19 '15 at 04:58
  • 2
    @Hi-Angel What Gilles means is that at your graphical login screen you'll often have a dropdown menu listing various environments. If you choose "KDE," that will start and it will ignore `.xsession`, as you described. But if, instead of "KDE," you choose "Custom session," `.xsession` will be run _instead of_ KDE. I use this, for example, because I run just `xfce4-panel` and FVWM 2 as my "desktop"; if KDE were installed on a system I happened to be using, it would be completely ignored. – cjs Jul 26 '17 at 02:10
  • Xsession may setup paths. Since it's often run from startx typed at a command line, frequently all paths are already set up. – RichieHH Dec 09 '20 at 18:04
  • I need to setxkbmap and found `/etc/gdm3/Xsession` will change it. And I have to set it at the very end to make it work. – WesternGun Apr 11 '21 at 15:50
18

X session startup can be complicated these days. A good place to start is the script /etc/X11/Xsession to see how things will get executed.

On my Debian/Ubuntu systems, the script /etc/X11/Xsession sets the variable USERXSESSION=$HOME/.xsession. Later, the script /etc/X11/Xsession.d/50x11-common_determine-startup looks for this script and uses it as the primary session, if allowed by system policy in /etc/X11/Xsession.options, and if no other session was requested by the display manager.

Jim Paris
  • 14,137
  • 5
  • 36
  • 35
  • 1
    Complicated is an understatement. Combined with sys specific handling of /etc/ profile etc it's a downright mess. – RichieHH Dec 09 '20 at 18:05
  • And there have been a lot of changes in the 8 years since this answer was posted (such as systemd adoption). – Jim Paris Dec 10 '20 at 21:35
  • @JimParis My `/etc/X11/Xsession` sets the variable `USERXSESSION=$HOME/.xsession`, used by `/etc/X11/Xsession.d/50x11-common_determine-startup`. My `/etc/X11/Xsession.options` has `allow-user-xsession`. But still my `${HOME}/.xsession` doesn't run as I did `echo "Hello Nikhil" > /home/nikhil/Desktop/a.txt` inside `${HOME}/.xsession` but this file is not created when I log in after logging out! Can you please help? I am using Ubuntu 20.04.2. I want to start firefox using systemd unit and I am refering https://superuser.com/questions/759759/writing-a-service-that-depends-on-xorg/1128905#1128905 – Porcupine Aug 11 '21 at 12:47
9

I strongly recommend you to take a look in the book X Window system administrator's guide : for X version 11. Chapter 2 explains the purpose of .xsession and the whole configuration process for a sample setup. Also it's a nice refresher for the whole x-window related terminology (and i hope you don't get spooked :) ). You can get the book from

http://archive.org/details/xwindowsystemadm08muimiss

Although it's rather outdated you can find a lot of information of the underlying concepts that didn't change until today and it is very legible. Moreover it is really funny to take a look at what was top notch at those days.

user1146332
  • 2,214
  • 13
  • 14
7

.xsession is the traditional startscript for the X11 environment. Nowadays with sessionmanagers like kdm it is not that much of a use anymore, but on a traditionaly set up system. This is what is run after starting X11 with startx.

Nikodemus RIP
  • 300
  • 1
  • 5