Ubuntu – Unable to run gedit as a different user

editorgedit

I login as user 'foo' and if I am able to run gedit and edit files in my Ubuntu 12.04 machine with a DISPLAY setting as :0

If I "su – bar" and then try to edit files, I get a no protocol specified error and it doesn't open the necessary files. What coule be the problem here?

Best Answer

gedit is a graphical program, so just as you would not run it as root with straight sudo, you shouldn't run it as another user with straight sudo or straight su.

Instead, if you (i.e., foo) have the power to run programs as another user with sudo, this is probably the easiest way for footo run gedit as bar:

xhost +local:bar
gksu -u bar gedit

If you don't have the power to run programs as another user with sudo but you can su to bar, then this is the easiest way:

gksu -w -u bar gedit

The -w flag (equivalent to --su-mode) makes gksu (which is a frontend for both sudo and su) use su instead of sudo.

  • You shouldn't need to run any corresponding xhost command to make gksu -w ... work.

If you want a graphical terminal instance (gnome-terminal) from which you can run anything as bar including graphical applications, you should consider just running a new instance of gnome-terminal as bar (which you can do the same way as running gedit, detailed above).