Setting up X11 for Xclient for non-root user

usually for non-root user we get error as

X11 connection rejected because of wrong authentication.

Solution to this is the MIT-MAGIC-COOKIE-1 is missing in your root user

e.g.

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

If you do not have Xauth installed , you can install it from yum

yum -y install xorg-x11-xauth

and reboot your machine

Check  if you have the .Xauthority file is created for  root and standarduser under /home/ directory .

check if you have files under /etc/X11/xinit/xinitrc.d/

 .Xauthority  file is missing then follow the below steps

Probably solved a long time ago but for completeness, when you ssh to a remote system, use:

ssh -X user@remote

You will get a complaint that the .Xauthority file did not exist and also find it was just created. This is dependent some settings in /etc/ssh/sshd_config:

X11Forwarding yes 
X11DisplayOffset 10 
X11UseLocalhost yes

Refer to http://ubuntuforums.org/showthread.php?t=1863739 where I was able to confirm my own experience with this errant error.