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?
As root, run
Then
And you are good to go. :)
Running Graphical Commands from Another User's Shell
The sux command (see
man sux
) takes care of this for you, and takes the same syntax assu
.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 withsux
(orsu
) when running graphical applications. (Forsudo
, you can instead use graphical frontends likegksu
/gksudo
andkdesudo
.)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):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 thannextuser
's password, assudo
rather thansu
is the backend):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.Use the
-p
option with su to preserve environment variables includingDISPLAY
; alternately set it explicitly withexport
or prefix it on a per command basis.