DEV Community

logix2
logix2

Posted on • Updated on

Another Gksu Alternative For Ubuntu (Xorg)

Back when Debian and Ubuntu removed the gksu package, which was used to allow elevating your permissions when running graphical applications, the recommendations were:

  • for application developers to use PolicyKit and only use elevated privileges for specific actions
  • for users to take advantage of the gvfs admin backend, by using the admin:// prefix

Using admin:// has an issue on Ubuntu though - it asks for your password twice the first time you use it during a session. This does not happen in Fedora, but it happens in both Ubuntu 18.04 and 19.04.

So what's a good alternative for gksu to use on Ubuntu under its default Xorg session, which doesn't ask for the password twice? An alternative that doesn't depend on the Linux distribution or desktop environment you use (you do need PolicyKit installed), is the following command, which works with any graphical application (GUI) that you want to run with elevated permissions / as root:

pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS`

The command exports your current $DISPLAY, $XAUTHORITY and $DBUS_SESSION_BUS_ADDRESS environment variables, allowing you to run any GUI application as root (with no need of having PolicyKit files for those apps), and without asking for the password twice, like admin:// does on Ubuntu.

To make it easier to use, since the command is quite long, you can create an alias for this command, called gksu.

To make this gksu alias for the pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS command permanent, add the following to your ~/.bashrc file:

alias gksu='pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS'

You can add it to ~/.bashrc by issuing the following command (run this command only once since it adds this line every time you run it):

echo "alias gksu='pkexec env DISPLAY=\$DISPLAY XAUTHORITY=\$XAUTHORITY DBUS_SESSION_BUS_ADDRESS=\$DBUS_SESSION_BUS_ADDRESS'" >> ~/.bashrc

After this, source ~/.bashrc:

. ~/.bashrc

Now you can run this alias exactly like you'd use the old gksu command.

For example, you can use it to run Gedit (replace with any other GUI text editor you wish) with elevated privileges and edit the /etc/default/grub file:

gksu gedit /etc/default/grub

Another example - run Nautilus (or some other file manager) and open the system themes folder:

gksu nautilus /usr/share/themes

For more articles I've written, check out LinuxUprising.com, a Linux & open source blog.

Top comments (0)