How do I restrict a user to a specific desktop environment?

I'm running a more or less standard installation of Ubuntu Precise 12.04 and I am quite satisfied with doing everydays task with Unity desktop environment.

When it comes to playing, however, I feel much more comfortable with LXDE (background: in it's current state, Unity archieves much lower framerates than e.g. Unity 2D or LXDE - it's a known bug and being worked on).

For that purpose I created a gamer user profile and now I want to restrict it to use LXDE desktop environment only.

To elaborate this further: when asked to login after boot I want only LXDE to appear as an option for the gamer user profile at the login-screen while my standard user profile should still have the option to choose which desktop environment to use.

Thanks in advance for any insight that may be of help!


Okay, from my understanding of LightDM (the login manager) it does not seem supported to restrict the desktop environments for a specific user. It is an all or nothing deal. By default, when a user chooses a desktop environment, that will become their default desktop environment. When they log in next time, unless they choose another one, they will be logged in to their default.

You can, however, make it so the user cannot change this. This user will still be able to select a different desktop environment, but if they do, it will not change their default. I believe this is the safest way to do this, if you are not worried that the person will change it. All you have to do is edit their .dmrc file (located in their home directory, i.e. /home/user). This is where their default session is stored. Change the Session variable in that file to whatever you want, i.e. ubuntu is the default Unity DE, ubuntu-2d is Unity 2D, and LXDE is LXDE. To make it so this will not be changed if they log in with another DE, we can simply make it immutable (unchangeable):

sudo chattr +i /home/user/.dmrc

Now no one will be able to change that file (root and other programs included) unless you remove the immutable flag:

sudo chattr -i /home/user/.dmrc

If you absolutely DO NOT want this user to ever log in with another DE, I did figure out a really hacky way (i.e. do this at your own risk) to accomplish this. I was kind of bored and this seemed like a fun thing to try. All of the different DEs that LightDM can see are kept at /usr/share/xsessions as .desktop files. FYI, you can delete any of these you don't want showing up at the login screen (but that will remove it for ALL users). If you open any one of these, they are fairly straightforward. They are basic desktop launcher files, just like all of the shortcuts on your desktop. Each one contains an Exec line, which is what gets executed when you choose your DE at the login screen (i.e. which DE gets launched). We are going to take advantage of this and check which user is logging in. If the user is our restricted user, we will force them to use a certain DE.

First, for each .desktop file in /usr/share/xsessions/ you will need to create a script in /usr/local/bin. For my example, I will be showing how to do this for ubuntu.desktop, which is the Unity DE, and using LXDE as the DE we are forcing our restricted user (restricteduser) to use. Looking at LXDE.desktop, we can see that the Exec command is /usr/bin/startlxde and there is no TryExec command. When you open ubuntu.desktop we see that the Exec command is gnome-session --session=ubuntu and the TryExec command is unity. The TryExec command is just what it sounds like. After running the Exec command, it will try running the TryExec command, but if it fails, it won't crash.

Now, taking those commands we got from the .desktop files we can create our scripts. We will put them in /usr/share/bin. We will need to make two, one for the Exec and one for the TryExec. We'll do something like this:

ubuntude.sh

#!/bin/bash

if [ `/usr/bin/whoami` = "restricteduser" ] ; then
   /usr/bin/startlxde
else
   gnome-session --session=ubuntu
fi

ubuntudetry.sh

#!/bin/bash

if [ `/usr/bin/whoami` != "restricteduser" ] ; then
   unity
fi

We are just testing whether the person logging in is our restricted user, and firing up the appropriate DE. Now, we need to make sure they are executable:

sudo chmod +x /usr/local/bin/ubuntude.sh
sudo chmod +x /usr/local/bin/ubuntudetry.sh

We now need to change our ubuntu.desktop so that it will call our scripts, instead of the actual DEs. Comment out the Exec and TryExec lines in the file, and replace them with this:

Exec=/usr/local/bin/ubuntude.sh
TryExec=/usr/local/bin/ubuntudetry.sh

Just repeat the process for any other DEs you may have. If there is no TryExec for a DE, then there is no need to create a detry.sh script for it either, as LXDE does not use a TryExec. Obviously this is a bit hacky, and you should make sure that you back up any files before editing them, but it will work if you absolutely need to make sure that a user is logging in to the correct DE.


on Debian Squeeze and Wheezy you can also add an .xsessionrc with a single line

export STARTUP=XXX

(where XXX ist your forced session-script see Exec-Line above) in the user homedirectory to force a certain session type regardless of what the user has chosen.

Should also work on Ubuntu.

(Take a look into /etc/X11/Xsession.d/40x11-common_xsessionrc and 50x11-common_determine-startup to see, why this works)