Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

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

(Update title)

share|improve this question
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

2 Answers 2

Seems like your root lacks some of X11 magic cookie in his .Xauthority, which your standarduser have. To fix things, firstly detect which display number standarduser uses:

standarduser@localhost:~$ echo $DISPLAY

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.

share|improve this answer
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

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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