Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

This question already has an answer here:

In my terminal, I did

 $su nextuser

and logged in to that account. Now, when I try to open a text file in the home directory of that user by

$gedit test.c

I get

No protocol specified
Cannot open display: 
Run 'gedit --help' to see a full list of available command line options.

How can I use the graphical text editor to display files on the new user's account?

share|improve this question

marked as duplicate by Sneetsher, BuZZ-dEE, waltinator, Jorge Castro, Eric Carvalho Jun 11 at 14:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers 3

Use the -p option with su to preserve environment variables including DISPLAY; alternately set it explicitly with export or prefix it on a per command basis.

share|improve this answer
    
I tried that. But then the new user's home folder is the old user's one itself. As in, when you type just cd, the terminal takes me to the old user's home folder. I'm talking in case of using -p. I did not understand what your other suggestions meant. –  harisibrahimkv Jul 27 '12 at 13:05

Running Graphical Commands from Another User's Shell

The sux Install sux command (see man sux) takes care of this for you, and takes the same syntax as su.

sux - nextuser
gedit test.c

This will automatically ensure the .Xauthority is correct, so you don't have to run any commands to manually authorize anything.

Graphical applications tend to write configuration files in your home directory, and if you run them as another user while preserving $HOME, they'll attempt to write configuration files as that user, in your home directory. This will result in failing to write the configuration, or the configuration getting messed up for the original user (or some combination of the two). That's why it's advisable to use the - flag with sux (or su) when running graphical applications. (For sudo, you can instead use graphical frontends like gksu/gksudo and kdesudo.)

Running Graphical Commands Directly as Another User

You can use gksu in su-mode (with the -w flag) to run the command as another user (with the -u flag):

gksu -w -u nextuser gedit test.c

However, you might still have to run xhost local:nextuser first (as you detailed in your quite good answer) for this to work. It's less likely to need that if you use sudo-mode, but then you can only do it if you're an administrator (and you'll be authenticating with your password rather than nextuser's password, as sudo rather than su is the backend):

gksu -u nextuer gedit test.c

Running the Terminal (Not Just the Shell) as Another User

You may just want to run the graphical Terminal program (that the shell runs "inside") as the other user. Once it's running, graphical programs launched from it will work seamlessly as well.

You can use the same methods described above for running gedit as the other user.

gksu -w -u nextuser gnome-terminal
gksu -u nextuser gnome-terminal
share|improve this answer
up vote 1 down vote accepted

As root, run

xhost local:nextuser

Then

su - nextuser

And you are good to go. :)

share|improve this answer

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