How to run gui apps with root privileges on Ubuntu/Debian

By | August 29, 2023

In earlier days we used to have commands like kdesudo, kdesu, gksu that would easily allow us to run applications with root privileges. But these commands have been discontinued and totally removed from modern versions of ubuntu and debian many other distros.

It is believed that running a gui application as root poses security threats.

A detailed comment has been made over here by Emmanuele Bassi. The archlinux wiki page over here discusses some ways to get around the problem and run gui apps as root.

Using sudo

Here is a variation of the sudo command that you should use when launching gui apps specifically. It preserves the home directory and other environment variables from the invoking user account (and does not use the root user values)

sudo -EH gparted

pkexec

Pkexec is another command that allows running a program with a different user privileges. Typically the format is like this

pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY hardinfo

Another longer version could be

pkexec env DISPLAY=$DISPLAY WAYLAND_DISPLAY=$WAYLAND_DISPLAY XAUTHORITY=$XAUTHORITY XDG_SESSION_TYPE=$XDG_SESSION_TYPE XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR wireshark

KDE Applications: KDE applications need additional environment variable like KDE_SESSION_VERSION and KDE_FULL_SESSION to be passed on like this

pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY KDE_SESSION_VERSION=5 KDE_FULL_SESSION=true dolphin

or

pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY KDE_SESSION_VERSION=$KDE_SESSION_VERSION KDE_FULL_SESSION=true dolphin

Though it will report error messages like this

QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root'

You can add "XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR" to the above command to remove that error, but then another error will popup saying:

QStandardPaths: runtime directory '/run/user/1000' is not owned by UID 0, but a directory permissions 0700 owned by UID 1000 GID 1000

Conclusion

There are many applications that we genuinely need to run a root. These include Gparted, Wireshark and Synaptic package manager etc. Users need to understand that they should not launch any arbitrary program with root privileges, unless they know it comes from a trusted sources and is a reputed applications with not bad reviews.

Rest is upto the user to risk.

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

Leave a Reply

Your email address will not be published. Required fields are marked *