I’m a network engineer, and I run Kubuntu on the desktop. Which means I often run applications as root: Wireshark, Ettercap, Zenmap, etc. I prefer a seamless desktop experience, meaning I like my GUI customisations to apply to root applications, not just those running as my own user. Secondly, I like to directly launch GUI apps from a root terminal.
- Allow root to display applications on the user’s desktop
- Retain environment variables when you execute sudo
- Keep the same GTK preferences as root
Normally only the logged-in user has the authority to run X applications. This is a good thing, and a useful security feature. Running commands as your user with sudo allows that application to use your X session, like so:
However, this doesn’t work:
(now as root) wireshark
We can work around this by modifying root’s ~/.Xauthority file. Either copy yours or link it into /root/, and you’re done:
ln -f -s /home/username/.Xauthority
If you have multiple users on the same machine, you can achieve the same goal by merging several files using the
OK, now root can make X apps display on tyler’s display, but he still has to set the DISPLAY variable or pass a -display argument. So let’s keep this variable when we login as root with “
Add the following somewhere in /etc/sudoers:
Defaults env_keep+="DISPLAY XAUTHORITY"
Or just for one user:
Defaults:username env_keep+="DISPLAY XAUTHORITY"
Now DISPLAY, and if you use it, XAUTHORITY, follow you if you use sudo. You can add any others to this list if you like.
Now, your root GTK apps run and display, but they look like something from Windows 95. This is because GTK has absolutely horrific default settings which are nearly always overridden by your window manager’s theme, and root doesn’t have your GTK preferences. That’s easy to fix:
ln -f -s /home/username/.gtkrc-2.0
And if you use KDE with the “Use my KDE style in GTK applications” (gtk-qt-engine) setting, you might also want:
ln -f -s /home/username/.gtk_qt_engine_rc
Finally, I just like a better sudo prompt. So I set this at the botton of /etc/bash.bashrc on every machine I manage:
export SUDO_PROMPT="[sudo] password for %u@%h: "
This is better than the sudo default (the totally ambiguous “Password: “) or the Ubuntu default (“[sudo] password for username: “) because it also lists the host you are currently on. If you login to as many machines as I do, confusing one sudo prompt for another is an easy way to ruin your day.
The GTK-QT-engine’s configuration file was changed in KDE4. Thus, regarding step (3), kde4 users must execute the following command:
sudo cp ~/.gtkrc-2.0-kde4 /root/.gtkrc-2.0
Comments feed for this article
Trackback link: https://www.tolaris.com/2008/10/02/running-root-apps-with-user-gui-preferences/trackback/