Running GUI programs as root using sudo over SSH connection
If SSH connection is established to a remote machine using 'ssh -X' and then a GUI program is run using sudo such as 'sudo virt-manager' then error 'X11 connection rejected because of wrong authentication.' would be encountered.
For GUI to work two environment variables are very important:
- Specifies to which X server display should be sent, or where should the graphics be displayed.
- Used for authenticating while connecting to a Xserver. In case this environment variable is not set, by default ~/.Xauthority file is used. Note that this authentication is important else all other users on same system would simply be able to connect to local DISPLAY port and send graphic commands / messages.
When 'ssh -X' is used SSH sets DISPLAY environment appropriately and also creates a '.Xauthority' file in home folder of the user. This allows users to run GUI programs such as 'gedit &'. But when same is tried using sudo such as 'sudo gedit &' error is encountered as file /root/.Xauthority either does not exists or is incorrect. To solve this problem one can either copy .Xauthority file of user to /root using:
cp /home/<user>/.Xauthority /root/
or simply set XAUTHORITY environment variable as:
sudo XAUTHORITY=/home/<user>/.Xauthority gedit &
assuming home folders are created in /home.
The same would work if root shell is obtained using 'sudo su -'. After obtaining root shell use
and then all GUI programs run afterwards would work fine.