How to make graphical applications work with sudo ?

If you use sudo, you may have noticed that, with Fedora Core 5, you just can’t exec a graphical application as root (for example) if you haven’t actually logged in as root in your X environment (you never do that ! Do you ?).

You have probably encountered this type of message :

user$ sudo kcalc
Xlib: connection to ":0.0" refused by server
Xlib: Invalid MIT-MAGIC-COOKIE-1 key
kcalc: cannot connect to X server :0

To be able to launch a graphical application as root, the XAUTHORITY environment variable must be copied to the shell launched by sudo.

Just run:

user$ sudo visudo

This will open the /etc/sudoers file.
After “_XKB_CHARSET”, just add “XAUTHORITY”. This should now look like :

Defaults    env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR \
                        LS_COLORS MAIL PS1 PS2 QTDIR USERNAME \
                        LANG LC_ADDRESS LC_CTYPE LC_COLLATE LC_IDENTIFICATION \
                        LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC \
                        LC_PAPER LC_TELEPHONE LC_TIME LC_ALL LANGUAGE LINGUAS \
                        _XKB_CHARSET XAUTHORITY"

Now, you can try:

user$ sudo kcalc

It’ll work !