How to access X11 Forwarding after becoming root

From DISI
Jump to navigation Jump to search

We're used to activating X11 forwarding when we SSH into a remote host. Everyone knows it by now:

ssh -X [or -Y] user@remotehost

Though sometimes, we have to become root and use a graphical program afterwards. You may know that after becoming root through sudo or su, X11 doesn't work anymore. You get an error that looks like this:

-sh-4.1$ sudo -i
[sudo] password for generic_user: 
[root@vav ~]# virt-manager
X11 connection rejected because of wrong authentication.
Unable to initialize GTK: could not open display

This is because root does not have the same .Xauthority contents as the user who originally SSH'd in. If you want to activate X11 forwarding, you'll have to copy the contents of the user's .Xauthority file into root's .Xauthority file.

[root@vav ~]# xauth add $(xauth -f ~generic_user/.Xauthority list | tail -1)