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
SebMa
  • 2,149

4 Answers4

1

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
RalfFriedl
  • 8,981
  • I want another user to access my display, how do I do that ? – SebMa Sep 21 '18 at 20:06
  • @SebMa it's in the xauth(1) manpage xauth extract - $DISPLAY | ssh other_user@localhost xauth merge - –  Sep 22 '18 at 00:54
  • @mosvy I'm not using ssh, I'm using su to log in as another, otherwise I'll have to be root to be able to install sshd – SebMa Sep 22 '18 at 10:38
  • You don't need ssh you just have to make the content available to the other user. I added an example to do that with su. – RalfFriedl Sep 22 '18 at 10:55
  • 1
    @RalfFriedl su: must be run from a terminal –  Sep 22 '18 at 10:57
1

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).

Thomas Dickey
  • 76,765
0

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'"
$
SebMa
  • 2,149
0

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.

FargolK
  • 1,667
  • Once you login again the missing values are reset. Look at the other answers to this question to see how to handle DISPLAY and XAUTHORITY – Chris Davies Apr 28 '21 at 07:19