I'm trying to open an xterm as another user :
$ su - michel
$ xterm
No protocol specified
xterm: Xt error: Can't open display: :0
$ exit
$ sudo su - michel
$ xterm
No protocol specified
xterm: Xt error: Can't open display: :0
I'm trying to open an xterm as another user :
$ su - michel
$ xterm
No protocol specified
xterm: Xt error: Can't open display: :0
$ exit
$ sudo su - michel
$ xterm
No protocol specified
xterm: Xt error: Can't open display: :0
It doesn't make sense to use sudo
followed by su
if you can use su
itself.
The problem is that the other used isn't allowed to use the $DISPLAY
. You don't want arbitrary users to connect to your display. If you really want to allow other users to connect to your display, you must also give them access to your $XAUTHORITY
file. Usually your $XAUTHORITY
file doesn't contain information for more than one display, so you could just make the file content accessible to the target user or copy the file.
You can also extract the information and merge it into the target user's $XAUTHORITY
file. The comment from @mosvy suggests ssh
, but su
also works.
xauth extract - $DISPLAY | su sebastien -c 'xauth merge - <&7' 7<&0 </dev/tty
When you do the su
, it discards the DISPLAY
environment variable (which is used to tell the terminal where to display its window). If you set DISPLAY
, you will run into a different problem (essentially permissions) which has been asked more than once, e.g.,. X “Can't open display: :0” while DISPLAY variable is correct (noting that one was incorrectly marked as a duplicate for yet another related issue).
I finally came across this command and now it works :
$ xhost local:$USER
non-network local connections being added to access control list
$ sudo su - michel -c "xterm -e 'sleep 1'"
$
I tried literally every solution from all similar questions. None of them works. However, a solution I found here (Written in Chinese) solved.
You just need to delete ~/.Xauthority
and restart the shell.
Everything works great after relogin.
DISPLAY
and XAUTHORITY
– Chris Davies
Apr 28 '21 at 07:19
xauth extract - $DISPLAY | ssh other_user@localhost xauth merge -
– Sep 22 '18 at 00:54ssh
, I'm usingsu
to log in as another, otherwise I'll have to be root to be able to installsshd
– SebMa Sep 22 '18 at 10:38ssh
you just have to make the content available to the other user. I added an example to do that withsu
. – RalfFriedl Sep 22 '18 at 10:55su: must be run from a terminal
– Sep 22 '18 at 10:57