6

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?

0

3 Answers 3

4

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
4

As root, run

echo $DISPLAY
xhost local:nextuser

Then

su - nextuser
export DISPLAY=<the value got from echo $DISPLAY above>
(or SET DISPLAY.. if export does not work)

And you are good to go. :)

1
  • xhost +si:localuser:nextuser did it for me. can be checked with xhost.
    – phil294
    May 26, 2017 at 3:59
2

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.

1
  • 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. Jul 27, 2012 at 13:05

Not the answer you're looking for? Browse other questions tagged .