As root, I'm connecting to a remote host to execute a command. Only "standarduser" has the appropriate id-file and correct .ssh/config, so I'm switching the user first:

su standarduser -c 'ssh -x remotehost ./remotecommand'

The command works fine, but despite the fact that I used "-x" (disable X11-Forwarding) and having X11Forwards disabled in /etc/ssh/ssh_config, I still get the error message:

X11 connection rejected because of wrong authentication.

I'm not getting the error message when I'm logged in as "standarduser".

This is quite annoying as I would like to integrate the command in a cron job file. I understand that the error message refers to the wrong authentication of root's .XAuth file, but I'm not even trying to connect via X11.

Why is "ssh -x" not disabling the X11 connection and throwing the error message?

UPDATE: The message only shows when I'm logged in within a screen, when using the command stated above on the local machine itself (without screen), I don't get an error message, so this should be fine with cron, too.

I also started the same command with -v and surprisingly got the error message FIRST, even before the status information from SSH:

root@localhost:~# su standarduser -c 'ssh -x remotehost ./remotecommand'
X11 connection rejected because of wrong authentication.
OpenSSH_6.2p2 Ubuntu-6ubuntu0.1, OpenSSL 1.0.1e 11 Feb 2013

This led me to the problem itself, it is NOT the ssh which is throwing the error message, it's su:

root@localhost:~# su standarduser -c 'echo Hi'
X11 connection rejected because of wrong authentication.
Hi

Why do I only get this error within screen? How can I disable this error message?

  • Run the command again and add -v to the ssh options, then paste the output into your question. – Jenny D Jan 23 '14 at 9:50

Seems like your root lacks some X11 magic cookie in his .Xauthority, which your standarduser has. Here is how to fix this.

SHORT VERSION (thanks to @bmaupin for it)

standarduser@localhost:~$ xauth list | grep unix`echo $DISPLAY | cut -c10-12` > /tmp/xauth
standarduser@localhost:~$ sudo su
root@localhost:~$ xauth add `cat /tmp/xauth`

Attention: check the backticks! They cannot be replaced with quotes! You need sudo installed to proceed further the second command!

ORIGINAL LONG VERSION

To fix things, firstly detect which display number standarduser uses:

standarduser@localhost:~$ echo $DISPLAY
localhost:21.0

In this case it is 21.0. Secondly, display standarduser's list of cookies:

standarduser@localhost:~$ xauth list
localhost/unix:1  MIT-MAGIC-COOKIE-1  51a3801fd7776704575752f09015c61d
localhost/unix:21  MIT-MAGIC-COOKIE-1  0ba2913f8d9df0ee9eda295cad7b104f
localhost/unix:22  MIT-MAGIC-COOKIE-1  22ba6595c270f20f6315c53e27958dfe
localhost/unix:20  MIT-MAGIC-COOKIE-1  267f68b51726a8a381cfc10c91783a13

The cookie for the 21.0 display is the second in the list and ends with 104f.

The last thing to do is to add this particular cookie to the root's .Xauthority. Log in as root and do the following:

root@localhost:~$ xauth add localhost/unix:21  MIT-MAGIC-COOKIE-1  0ba2913f8d9df0ee9eda295cad7b104f

This is a plain and simple how to deal with the X11 connection rejected because of wrong authentication error while you run su as different users in bash script or screen. And, by the way, thanks to this guy for his brilliant post.

  • Interesting. I tried that, but that didn't work for me either. In my particular case, I can launch almost anything (another xterm, VirtualBox), but I cannot launch gedit (I get the same error). However, if I change to root, then I can launch gedit. I followed the instructions to the letter but nothing. Something else must be amiss. – luis.espinal Jun 26 '14 at 13:16
  • @luis.espinal hope someone suggest a solution to your particular problem. – TranslucentCloud Jun 30 '14 at 21:33
  • 1
    The long version worked like a charm, but the short version errored on centos with "xauth: (argv):1: bad "add" command line" – Sam Aug 20 '15 at 23:16
  • 1
    short version worked for me on centos 7 – tdc Oct 13 '15 at 20:19
  • 1
    on Ubuntu 18.04.1 short version works fine. – Simakis Panagiotis Sep 12 at 13:40

An easier solution:

1.- ssh user@host

2.- $ sudo su

3.- # xauth merge /home/user/.Xauthority

That’s all

Of course $DISPLAY variable must be set.

  • 1
    This sounds promising but a bit incomplete. Would you mind to add info regarding how exactly to set $DISPLAY variable? I believe this small addition will cast additional voices to your answer. – TranslucentCloud May 6 '15 at 19:16
  • This worked for me, while @TranslucentCloud's answer did not. I originally encountered the problem while trying to run the Android SDK manager as root from the command line FYI. – scottyseus Oct 12 '15 at 14:06
  • 2
    This didn't work out of the box for me since xauth: file /root/.Xauthority does not exist – tdc Oct 13 '15 at 20:17
  • 2
    tdc, that's just a warning, not an error, xauth will create the file - if you run the command a second time you won't see it. – Vladimir Panteleev Jan 25 '16 at 8:53
  • Who needs to learn anything when you can just copy and paste! Thanks! Way easier. – Sirens Oct 24 '17 at 15:51

My needs were slightly different so I came up with a slightly different solution. I needed the ability to run an X11 app as another user (who is not root). Running CentOS, so I don't have the sweet gksudo tool the lucky dogs with ubuntu have which does the Xauth magic.

I really didn't want to break out some custom scripts just to login, switch user and run an app; that seems a bit superfluous to me.

Step One:

Allow $XAUTHORITY to be carried across sudo sessions.

Add this line under the rest of the env_keep statements in /etc/sudoers:

Defaults    env_keep += "DISPLAY XAUTHORIZATION XAUTHORITY"

Step Two:

Allow your target user the ability to read your .Xauthority (yeah I know, scream SECURITY! all you want). For those who just want the ability to run commands as root, this can be skipped.

The target user shares the same group as me so I just turn on group read permissions:

$ chmod g+r user ~/.Xauthority

Step Three:

CentOS doesn't populate the value of $XAUTHORITY by default. Add a line to your profile (mine is ~/.bash_profile):

export XAUTHORITY=$HOME/.Xauthority

That's it. No more tweaking. No writing .XML to get PolicyKit to work. No running scripts for each login. No having to sudo twice to copy the xauth. From here on out, you can simply:

$ sudo -u user xcalc

Works great with MobaXTerm.

  • This was just what I needed to solve the problem that I was having with getting VM console on CentOS 7. I actually only needed to do Step Three for my purpose (and, of course, ensure that X11Forwarding was set to yes in /etc/ssh/sshd_config). Thanks. – darklion Dec 16 '16 at 4:33
  • Awesome! This is the answer! Thanks @W Smith – tmow Jul 1 '17 at 19:52

You should fully switch to the target user, i.e. use "-" with su (su - standarduser ...). If not, root's X stuff will be dragged around in environment.

Because I frequently root on a root-squashed network file share, none of the above solutions worked for me. (xubuntu 14.04). I put together the following script, which works on my system. It may work on yours. Then again, it may not, but it's free to try...

Assumption is I've ssh'd using the -Y option.

#!/bin/bash  
if [[ $DISPLAY =~ localhost(:[[:digit:]]+) ]] ;then
    port=${BASH_REMATCH[1]}
else
    echo "Unexpected DISPLAY $DISPLAY"
    exit 1
fi
h=$(hostname)
cookie=$(xauth list|grep $h.*$port)
sudo -i xauth -i add $cookie
sudo -i $*

In my case, when I encountered that error, I had an encrypted user directory. After calling ecrypt-mount-private, it got rid of the error and allowed me to continue X11 forwarding.

To determine if your home folder is encrypted, you can try this (as per this answer): ls -A /home. If you see a .ecryptfs folder, then your home directory is probably encrypted, in which case you can try doing the command I put in the start of the answer.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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