23

Let's say I have 2 user accounts user1 and user2. When I login as user1, and then switch to user2 using su, I can execute command-line programs, but GUI programs fail.

Example:

user1@laptop:~$ su - user2
user2@laptop:~$ leafpad ~/somefile.txt
No protocol specified
leafpad: Cannot open display: 

So how can I run a GUI application?

  • One of the main reasons, I've found, that this fails is because $XAUTHORITY is still set to user1's ~/.Xauthority, which the program, I guess, will try to read, and it fails because that file typically has mode 0600 (-rw-------), meaning it's unavailable for reading by anyone in the "other" group, which includes user2. Meaning if you chmod o+r ~/.Xauthority (as user1), you will have hacked your way around this problem. I wrote a script that demonstrates this. – Braden Best Feb 3 '18 at 7:29
32

su vs. su -

When becoming another user you generally want to use su - user2. The dash will force user2's .bash_profile to get sourced.

xhost

Additionally you'll need to grant users access to your display. This is governed by X. You can use the command xhost + to allow other users permission to display GUI's to user1's desktop.

NOTE: When running xhost + you'll want to run this while still in a shell that belongs to user1.

$DISPLAY

When you become user2 you may need to set the environment variable $DISPLAY.

$ export DISPLAY=:0.0
  • 1
    xhost +user2 still gives me this error - xhost: bad hostname "user2". I googled some, and it seems I need to do xhost +user2@laptop or xhost +user2@localhost, not sure which. Then it says xhost +user2@localhost being added to access control list. – sashoalm Jan 11 '14 at 10:10
  • 1
    But even after adding the user with xhost, and specifying export DISPLAY=:0.0, running leafpad still gives me No protocol specified leafpad: Cannot open display:, and fails to run. I found this link at linuxquestions.org/questions/linux-newbie-8/…, which says that there are some magic cookies and xauth. Have you tested that those things work on your computer btw? Maybe something's different with my configuration? I'm on Debian+LXDE. – sashoalm Jan 11 '14 at 10:15
  • 1
    Thanks, xhost + works, and nothing else seems to be needed (no need to set $DISPLAY). Can you update your answer, and I'll accept it? – sashoalm Jan 11 '14 at 22:27
  • 4
    Oh, found something. On Fedora 21 running xhost gives a list in the format SI:localuser:USERNAME, so xhost SI:localuser:user2 should work. Oh and the display of the user can be found using w. – Wilf Apr 21 '15 at 11:01
  • 3
    xhost + will allow any user on any host that can connect to your x-server to access your screen. xhost +SI:localuser:user2 works for me on Debian. – robartsd Mar 17 '17 at 4:46
5

You need to share the authentication token from the user1 (assuming ~is home of user1):

cat ~/.Xauthority | sudo -u user2 -i tee .Xauthority > /dev/null
  • This is the only answer that worked for me (Ubuntu 14). – sudo Sep 8 '17 at 1:28
  • This solution works nicely from within the remote machine (= X Win client) while the xhost solutions in other answers have to be executed on the local machine (= X Win server). – Jpsy Nov 1 '17 at 15:55
4

You could use X11 forwarding:

ssh -XY otheruser@localhost your-gui-program-name-here
  • This is a brilliant solution. The simplest I 've read so far. Far more people are familiar with ssh than with x11 configuration. – Alexis Panagiotopoulos Nov 19 '18 at 15:13
1

You may try the sux command:

sux user2

sux will handle the $DISPLAY stuff for you. You may need to install it with:

sudo apt-get install sux

under Debian/Ubuntu.

  • 2
    sux is no longer shipped by Debian or Ubuntu. The best alternative I could find is adding xhost SI:localuser:root (or whatever user) to ~/.xprofile to allow it permanently or to use runuser – stefanct May 21 '16 at 21:34
1

You can start app from another user. I will start the gimp app from user2, while being logged in (GUI) with user 1:

$ xhost +
$ sudo su user2

(enter pass)

$ gimp

Enjoy :)

  • 1
    This is the same as the four-year-old accepted answer. – G-Man Dec 17 '17 at 22:55
  • can you tell a quick better way? – Antoni Stavrev Jan 1 '18 at 1:57
  • I've always used this method, but it's no longer working to me with Debian and Xfce. (correction: it does work, but I have to export DISPLAY first, as the accepted answer says) – giusti Aug 20 '18 at 0:38
  • after you end your session it will be good to disable it by $ xhost - – Antoni Stavrev Oct 18 '18 at 21:59

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.