14

After logging in as another user how do I run GUI programs?

thufir@doge:~$ 
thufir@doge:~$ sudo -u hawat -i
hawat@doge:~$ 
hawat@doge:~$ whoami
hawat
hawat@doge:~$ 
hawat@doge:~$ pwd
/home/hawat
hawat@doge:~$ 
hawat@doge:~$ echo $HOME
/home/hawat
hawat@doge:~$ 
hawat@doge:~$ firefox
No protocol specified
Failed to connect to Mir: Failed to connect to server socket: No such file or directory
Unable to init server: Could not connect: Connection refused
Error: cannot open display: :0
hawat@doge:~$ 
12

export DISPLAY=:0 worked for me. Make sure you execute the command on the connecting system, not on the system you're connecting to!

  • 2
    On the machine I was using to connect, not the machine you are connecting to. – user2938093 Feb 7 '17 at 18:45
  • ahhh, this is on the same box. diff accounts. – Thufir Apr 1 '17 at 20:46
  • Did not have any effect on my computer – ComputerScientist Aug 21 '18 at 14:50
6

This answer works on my Ubuntu 16.04 and Mir (with slight adaptations):

Run x command as another user using xauth

Wrapper bash script:

For convenience, I created a wrapper bash script, that you might want to download to /usr/local/bin/xsudo and mark executable (chmod +x /usr/local/bin/xsudo).

Then simply run:

xsudo hawat firefox

Potential Alternatives:

  • As a far simpler alternative, you might look into pam_xauth (s. this answer) However this doesn't work for me for some reason. (I still get the error about firefox not being able to connect to the display at :0)
  • In a lot of forums you might see the command xhost +local: or similar, which will make gksudo -u hawat firefox work without any wrapper script. However be aware that this opens a security loophole: Any active user on your local host will be able to access your keyboard, mouse, screen etc.
  • There used to be sux, which has been discontinued for some reason - you might still be able to download and use it though. As far as I can tell, it did something similar to my script only a lot more. Unfortunately I couldn't find out, why this is no longer maintained. (Maybe because we should now use pam_xauth instead - s. first alternative)
1

export DISPLAY=:0 works fine but this is a temporary solution since you need to type this each time when you open the terminal.

Permanent Solution : The problem might be due to env_keep variable in /etc/sudoers. Add the following line at the end of /etc/sudoers using gedit or using sudo visudo

Defaults env_keep+="DISPLAY"

Note : Keep in mind to always use "+=" while adding to env_keep. In case if there are any other env_keep with "=" change them to "+=" because using "=" will override other values.

  • FYI: keep in mind you aren't supposed to edit the sudoers file directly, but by using the visudo wrapper, which provides some sanity checking before writing a new sudoers file. – Gabriel Fair yesterday
1

After spending way too much time on this, I discovered that with Gnome on Ubuntu 16.04, the correct value is DISPLAY=:1, not DISPLAY=:0 as it has been from time immemorial.

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.