X11 connection rejected because of wrong authentication when using sudo
I have a headless server and a desktop client, both running XUbuntu 20.04.
I log in to the server using 'ssh -X servername'
Graphical applications work fine from the server as a normal user, and display on the desktop machine.
But, when I 'sudo graphical-app' (eg 'eog', or 'update-manager'), I get the following error:
X11 connection rejected because of wrong authentication. Unable to init server: Could not connect: Connection refusedSimilarly, 'sudo emacs' responds: [sudo] password for cmb:
X11 connection rejected because of wrong authentication. Display localhost:10.0 unavailable, simulating -nw(and falls back to running emacs in the terminal).
On earlier versions of XUbuntu (back to 12.04, through to 18.04 / 18.10 / 19.04) I was able to run graphical applications on the server through 'sudo' and they displayed correctly on the client. Something has changed! Any ideas as to what, and how to work round it?
11 Answer
Found an answer here:
For whatever reason, Xauthority isn't being passed over on sudo. But... sudo xauth merge ~/.Xauthority then sudo graphical-app works fine.
I think this is unrelated to ssh, just to Xauth and sudo.
1