Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

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

Seems like your root lacks some of 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 step!


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
Works great! I shortened it a little: 1. xauth list|grep unix`echo $DISPLAY|cut -c10-12` > /tmp/xauth 2. sudo... 3. xauth add `cat /tmp/xauth` – bmaupin Jun 29 '15 at 19:44
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
short version worked for me on centos 7 – tdc Oct 13 '15 at 20:19

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
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. – Scott Scooter Weidenkopf Oct 12 '15 at 14:06
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
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 at 8:53

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.

share|improve this answer

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:


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.

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.