X and sudo

For some strange reason, ubuntu is really misbehaved when it comes to preserving environment variables. The normal fix to running X applications over sudo is to edit /etc/sudoers and make sure that DISPLAY and XAUTHORITY are in the preserved variables list. Log out, log in, it’ll work.

Ubuntu, for whatever stupid reason, absolutely refuses to preserve these variables. Granted here at work we run novell’s SuSE and I too run SuSE. When the Ubuntu guys (both of them) have a problem, it’s sometimes hard to troubleshoot especially when I have to say “It works for me”.

Here’s the deal. If you’re having problems making X applications work over sudo, either allow people to sudo bash to open a root shell they can work in or script the following:

xauth merge /home/USERNAME/.Xauthority ; /sbin/yast2

Obviously substitute username for the user having the problem and /sbin/yast2 is whatever you’re trying to run in X as root.

You guessed it, $USER doesn’t work because Ubuntu doesn’t export $XAUTHORITY or $DISPLAY either.