X11 connection rejected because of wrong authentication

By | November 18, 2013

SSH does not allow the forwarding of X11 credentials onto a second user. For example, when connecting as user1 and you su – to another used (eg: user2), and you try to launch an X application, you will get the following error:

myhost ~ # virt-manager
X11 connection rejected because of wrong authentication.
Traceback (most recent call last):
File "/usr/share/virt-manager/virt-manager.py", line 383, in <module>
File "/usr/share/virt-manager/virt-manager.py", line 286, in main
raise gtk_error
RuntimeError: could not open display

To fix this problem, you need to do the following:

echo "export XAUTHORITY=/home/myUserNameHere/.Xauthority" >> ~/.bash_profile

Substitude myUserNameHere with your own. You will do this for the user that you use to log on to the system with SSH. Log out, then back in again, and now when you su – to the second user you should be able to launch your X11 application 🙂