I just noticed something peculiar about how sudo handles the .Xauthority file:
sudo xauth add $(xauth list | tail -1) user@server: sudo xauth info Authority file: /root/.xauthYZ21Nt File new: no File locked: no Number of entries: 1 Changes honored: yes Changes made: no Current input: (argv):1 user@server: sudo xauth info Authority file: /root/.xauth3BFy5d File new: no File locked: no Number of entries: 1 Changes honored: yes Changes made: no Current input: (argv):1 user@server: sudo xauth list server/unix:10 MIT-MAGIC-COOKIE-1 c922ab48defdf43b1092dffb86c06eed user@server: sudo ls -la /root | grep auth -rw-r--r-- 1 root root 0 Nov 9 14:40 .Xauthority -rw------- 1 root root 57 Nov 9 15:23 .xauthsrxzxl user@server: pkexec xauth info Authority file: /root/.Xauthority File new: no File locked: no Number of entries: 0 Changes honored: yes Changes made: no Current input: (argv):1 So, $XAUTHORITY value is different in every new sudo, and it points to a temporary file which disappears when sudo quits. Because of this, the last command (which uses pkexec instead of sudo and expects it to be in /root/.Xauthority) fails to see the cookie. For instance, sudo gedit runs fine, but pkexec env DISPLAY=$DISPLAY gedit fails.
Why is it done in such a complicated way, where is the data stored, and more importantly, how can I access .Xauthority data with pkexec?