This page will teach you how to design your own personalized X (graphical desktop) sessions by editing shell scripts. Desktop environments like GNOME often have their own session managers. These session managers allow you to specify which additional programs will be loaded at the beginning of your session by providing a graphical interface. However, knowing how to create X session scripts can grant you greater flexibility in customizing your desktop environment whether you use GNOME, KDE, XFCE, Openbox or other lesser-known window managers like Blackbox or FVWM. Furthermore, being able to create X session scripts allows you to run a graphical desktop even if the graphical login manager, GDM, is broken or not installed.

Anatomy of a simple X session script

The simplest possible X session script consists of two commands, as shown in the example below:

 #!/usr/bin/env bash
 exec gnome-session

The first line is common to all Linux shell scripts; it specifies the shell used to process the script. In the case of the example shown above, the bash (Bourne Again Shell) has been specified as the shell.

The second line specifies the application that drives the X session. This is sometimes called the "magic process", as the X session will end when this application stops running. The "magic process" in the case of the above example is "gnome-session", which will launch the GNOME desktop environment.

How to create a X session script

You do not have to have root privileges to create your own X session script.

  1. Open a terminal by opening the Applications menu, select Accessories, and then click Terminal.

  2. To begin editing your script, type the following command at the prompt:

gedit ~/.xinitrc

You should now have an empty text editor window on your screen. Any changes you make will be saved to the file .xinitrc, which will be hidden in your home directory. (Note: In Linux, all files that begin with a period are treated by the system as hidden files.)

  1. Specify the shell on the first line:

 #!/usr/bin/env bash
  1. Type in any commands you want to run before launching your desktop environment or window manager. Use one command per line, and place a & after each command. Placing an ampersand (&) after each command tells the shell to run the command in the background and move immediately to the next command. Not having commands run in the background will lead to your X session script getting stuck on the first command listed in the file, so that all you see is an empty desktop. For example, if you always want to have a terminal open when you first login, include a line that says:

gnome-terminal &
  1. On the last line, type in the word exec and then the name of the window manager or desktop environment you want to use as shown in the example below:

# for GNOME, use this command
exec gnome-session
# for KDE, us this command instead. 
exec startkde
# Note that if you have two "exec" lines, X will only process the first, and ignore all others. Also, any line
# that begins with a # will be treated by the shell as a comment and ignored. Use this to your advantage
# by placing notes in your script or disabling commands that you do not currently wish to run.
  1. When you are satisfied, save your file and close the text editor.
  2. Now make your X session script executable. To do this, type the following command into your terminal:

chmod +x ~/.xinitrc
  1. Now that you have made your X session script executable, you are ready to try it out. To do so, save all documents you might have open, and close all applications. Log out of GNOME. Press CTRL+ALT+F1 to access a text console, and login. Once you have logged in, you have to temporarily disable your graphical login. To do this, enter the command

sudo /etc/init.d/gdm stop
  1. Once you have done this, you will be ready to test your X session script. To test it, enter the following command:

  1. If you have not mispelled the names of any commands, then you should have a graphical desktop. If not, then you will have to correct your script. The shell will tell give you the line number if a command fails to make fixing your script easier. To fix your script, you can either edit your file with nano:

nano -w
  • Or you can restart the graphical login manager with the following command, login, and perform steps 1 and 2:

sudo /etc/init.d/gdm start
  • Otherwise, log out of the graphical desktop you brought up with the startx command.

  • Your login script, .xinitrc, works with startx, but graphical login managers like GDM do not look for .xinitrc. Instead, they look for a file named .xsession in your home directory. To make GDM run your .xinitrc script, you have to link it to .xsession with the following command:

ln -s ~/.xinitrc ~/.xsession
  • Using the above command creates a symbolic link named .xsession that points to .xinitrc. If you are familiar with Windows, it may help to think of a symbolic link (or symlink), as a kind of non-graphical shortcut.

  • Once you have linked .xsession to .xinitrc, you are ready to make your graphical login manager run your custom environment instead of a default session. To do, begin by restarting the graphical login manager:

sudo /etc/init.d/gdm start
  1. Enter your user name, and then click on the Sessions button. The appearance of the Sessions button depends on whether you are using a themed greeter or not, and which greeter theme you are using.

  2. From the Sessions menu, select Default System Session. This will make GDM run your X session script instead of one of the predefined sessions.

  3. Enter your password.
  4. Once you have entered your password, GDM will notify you that you have chosen a session different from the default and ask you if you want to use the session you've chosen once, or make it the default. If you make your custom session the default, you can select a different session at your next login.

Sample ~/.xinitrc File

The following is the .xinitrc file that the author of this HOWTO, Stormy Eyes, uses at home. It makes use of apps not part of the default install, like xcompmgr for compositing effects and feh for setting wallpapers. If you have any questions about the example .xinitrc file, please ask them in the forum.

 #!/usr/bin/env bash

xcompmgr -fF -I-.002 -O-.003 -D1 &
devilspie &
sudo killall -9 esd
sh ./.fehbg &
xrdb -merge .Xdefaults &

export OOO_FORCE_DESKTOP=gnome
export LANG="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
export LANGUAGE="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"

gnome-settings-daemon &
gnome-volume-manager &
conky &

exec openbox

LightDM configuration

Ubuntu 12.04 uses LightDM by default.

Create a new file /usr/share/xsessions/custom.desktop with:

[Desktop Entry]

You should now have a new session option during login, Xsession will load the user's ~/.xsession file

Questions and Comments

If you have any questions or comments pertaining to this tutorial, please post them on the Ubuntu Forum. Thank you.

CategoryDocumentation CategoryCleanup

CustomXSession (last edited 2012-07-14 18:02:39 by monkeypet)