34

I've read a lot of threads about gksu being removed from many Linux flavours, including Ubuntu 18.04. Many state that gksu is an abomination and no user should ever need to use anything like it. I have 2 examples where, as far as I know, I must have something that allows me to run as root (not just for editing files).

First, I have a need to start mysql only after an encrypted folder is opened on (GUI) command. This means having a script run by the encryption system that starts the mysql service. That has to be done as root, so I use gksu in that startup script which asks me for a password. How else can I start a service from a GUI system?

I also run Ubuntu system backups as root, otherwise many system files can't be backed up. So the desktop file uses gksu to start the backup.

pkexec looks complicated, needing policy files. Is that the only alternative, and if so, why is it any safer?

Other answers to similar questions don't seem to address individual issues that people have now that gksu is no more (or harder to find...). I think such questions from non-expert users would benefit from more detailed answers. I now have some, and so would like to answer my own question.

11
  • Does pkexec work for you instead of gksu? – Thomas Ward May 31 '18 at 15:31
  • 2
    You can also try to use sudo -H GUI-program and if Wayland there is this link, that might be useful, ... there are workarounds, if you have a GUI tool, that works well for you and needs elevated permissions. – sudodus May 31 '18 at 15:47
  • 3
    This is a controversial topic but, in a nut shell, running graphical applications as root is a security risk. Although you, and many others, may not agree with this, running graphical apps as root is discouraged by Ubuntu and many distros. Ubuntu is in process of writing alternate solutions, pkexec and other policies. You can file a bug report and the developers will work on a solution to programs they feel should do so such as package managers. This is complex as it involves X and Wayland. Alternately use non graphical apps or write your own. Encryption does not require a gui – Panther May 31 '18 at 16:09
  • 1
    @ sudodus Wayland's depreciated and no longer the default in 18.04 LTS. – K7AAY Jun 1 '18 at 23:48
  • 1
    @K7AAY, I run Ubuntu 18.04 LTS with Wayland in one computer. It is not the default, but it works for me. I would not say that it is deprecated. Maybe not mature yet ;-) – sudodus Jun 2 '18 at 5:10
35

I found this command

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

in this page

After running the alias command:

gksu /path/to/script.sh

It will ask you for a password like normal.

You will need to add the alias to the end of ~/.bashrc in order to keep the gksu command persistent on reboot.

1
  • 3
    Terrific, thank you so much. This practice of killing features without providing alternatives contributes to soo many hours of wasted time its not even funny. Thank you for fixing that, for me at least. – Pedro Rodrigues Nov 20 '20 at 12:10
10

I know there are said to be answers to this elsewhere, but I have not found any of them clear and simple enough for me to resolve this issue. So I dug around in many places and came up with this. I still don’t understand why the Linux community has decided to make life so hard for us simple desktop users (and I have tried to understand many explanations), but that’s life. I use the GUI for just about everything, only using the terminal window when necessary. As far as I can tell solutions using sudo options don’t help with this. So I have .desktop files and scripts that do all the jobs I use regularly.

There are two basic solutions.

1. Reinstall gksu

This solution may or may not last. Anti-gksu techies may find a way to stop it. But in the mean time, download 2 .deb files from libgksu (x64) and gksu (x64). Install libgksu2 and then gksu using gdebi or whichever installation tool you like to use. This worked for me.

2. Use pkexec

As a (hopefully) longer term solution, I managed to get pkexec to work for the tools I need.

  1. To start a service from a script. It turns out that neither gksu or pkexec is needed. Just start service xyz and it will ask for your password.

  2. To edit root files, or to open nautilus as root, see How To Run Gedit And Nautilus As Root With pkexec Instead Of gksu - Web Upd8. This provides two ‘polkit’ files for pkexec, that allow you to use a script containing pkexec gedit to edit a root file, and similarly for nautilus. The instructions are all on that web page. I’m now using ‘filemanager-actions’ to provide right-click actions to run gedit or nautilus as root.

  3. I run deja-dup as root for backups of the core system. I do this infrequently, excluding /home (for /home I do frequent backups that don’t need root access). To get this to work I took a copy of the file used in step 2 for gedit and edited it for deja-dup. I don’t really understand the contents, but it does work, both for backups and to restore files, using pkexec backup in a script initiated from a .desktop file. I added this new file to /usr/share/polkit-1/actions containing:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE policyconfig PUBLIC "-//freedesktop//DTD polkit Policy Configuration 1.0//EN" "http://www.freedesktop.org/software/polkit/policyconfig-1.dtd">
    
    <policyconfig>
      <icon_name>deja-dup</icon_name>
    
      <action id="org.gnome.DejaDup">
        <description gettext-domain="deja-dup">Backup</description>
        <message gettext-domain="deja-dup">Privileges are required to backup system files</message>
        <defaults>
        <allow_any>no</allow_any>
        <allow_inactive>auth_admin</allow_inactive>
        <allow_active>auth_admin</allow_active>
         </defaults>
        <annotate key="org.freedesktop.policykit.exec.path">/usr/bin/deja-dup</annotate>
        <annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
      </action>
    
    </policyconfig>
    

    Note that there is another deja-dup file in this directory, which claims to help with restoring files, but I couldn’t get it to work. This new file needs to have a unique name, such as org.gnome.DejaDupNew.policy.

I am, for now, running without gksu. I’ll try to carry on…

2
  • The command to start the backup was pkexec deja-dup for me, after adding the file like you described. The dialog does let me choose the settings first, which is great. – wbloos Oct 3 '18 at 16:02
  • Putting a policy file in /usr/share/polkit-1/actions was indeed the way to create a desktop launcher with elevated pkexec permissions. Here is another example. – Serge Stroobandt Apr 23 '19 at 17:25
4

Nautilus Admin (nautilus-admin) is a simple Python extension for the Nautilus file manager that adds some administrative actions to the right-click menu:

  • Open as Administrator: opens a folder in a new Nautilus window running with administrator (root) privileges.
  • Edit as Administrator: opens a file in a Gedit window running with administrator (root) privileges.

To install Nautilus Admin in all currently supported versions of Ubuntu open the terminal and type:

sudo apt install nautilus-admin

I've tested all the alternatives to gksu in 18.04 for other applications besides Files and Gedit, and the one that seems to work the most consistently is:

sudo -H appname &>/dev/null

pkexec is the best replacement for gksu when it works because it provides higher security, but it is very inconsistent across different apps (for example it doesn't work with Gedit) and can cause crashing with some apps. sudo -i is unnecessarily difficult to manage because it elevates your privileges to root for an extended period of time when you only need to be root to run a single command.

0
3

I use a script called sgedit which inherits user preferences for font, tabs, and extensions. It uses sudo -H gedit instead of gksu gedit for stability in GUI environment. It prompts for a password.

Have sudo inherit your user account gedit settings

sgedit 80 column right slider.gif

In this example the user settings for font name, font size, tab stops, convert tabs to spaces, 80 column highlight, and right side thumbnail slider bar have been inherited by sudo.

With regular sudo -H gedit you cannot make nor save these configuration settings. With the script below sgedit the settings are inherited from your user account.

This script also addresses the "gksu is bad and not installed by default" and "pkexec is hard to setup" problems.


Background

I've been nagged by the same issue for years. This weekend's project was to write the sgedit script:

  • Call using sgedit filename1 filename2...
  • Gets user's gedit settings for tab stops, fonts, line-wrap, etc.
  • Elevates to sudo -H to preserve file ownership whilst getting root powers.
  • Requests password if last sudo has timed out.
  • Gets sudo's gedit settings
  • Compares differences between user and sudo gedit settings
  • Runs gsettings set on the differences only (reduces 174 set commands to a dozen or less. Next time it's run perhaps only one or two changes but often times none.
  • Calls gedit as a background task such that terminal prompt reappears immediately.

Bash script sgedit

#!/bin/bash

# NAME: sgedit
# PATH: /mnt/e/bin
# DESC: Run gedit as sudo using $USER preferences
# DATE: June 17, 2018.

# Must not prefix with sudo when calling script
if [[ $(id -u) == 0 ]]; then
    zenity --error --text "You cannot call this script using sudo. Aborting."
    exit 99
fi

# Get user preferences before elevating to sudo
gsettings list-recursively | grep -i gedit | grep -v history |
    grep -v docinfo |
    grep -v virtual-root | grep -v state.window > /tmp/gedit.gsettings

sudoFunc () {
    # Must be running as sudo
    if [[ $(id -u) != 0 ]]; then
        zenity --error --text "Sudo password authentication failed. Aborting."
        exit 99
    fi

    # Get sudo's gedit preferences
    gsettings list-recursively | grep -i gedit | grep -v history |
        grep -v docinfo |
        grep -v virtual-root | grep -v state.window > /tmp/gedit.gsettings.root
    diff /tmp/gedit.gsettings.root /tmp/gedit.gsettings | grep '>' > /tmp/gedit.gsettings.diff
    sed -i 's/>/gsettings set/g; s/uint32 //g' /tmp/gedit.gsettings.diff
    chmod +x /tmp/gedit.gsettings.diff
    bash -x /tmp/gedit.gsettings.diff  # Display override setting to terminal
    nohup gedit $@ &>/dev/null &
}

FUNC=$(declare -f sudoFunc)
sudo -H bash -c "$FUNC; sudoFunc $*;"

Housekeeping

Copy the bash script above to a new file called sgedit. I recommend placing it in your $HOME/bin directory, i.e. /home/YOURNAME/bin. You may have to create the directory first.

Mark the file as executable using:

chmod a+x ~/sgedit

Note ~ is a shortcut for /home/YOURNAME.

1

Ubuntu 18.04: Some installation programs and others do need gksudo or gksu to be available with the same name. To make these work:

Install gnome version of ssh-askpass. Without this the password dialog may be hidden behind another window:

sudo apt-get install ssh-askpass-gnome

Create new file my-gksudo.sh:

sudo -H gedit /etc/profile.d/my-gksudo.sh

file content:

export SUDO_ASKPASS=/usr/bin/ssh-askpass

Create new files gksudo and gksu with identical content:

sudo -H gedit /usr/bin/gksudo

and

sudo -H gedit /usr/bin/gksu

file content for both:

sudo -A $@

Make gksudo and gksu executable:

sudo chmod +x /usr/bin/gksudo

And

sudo chmod +x /usr/bin/gksu

Reboot the computer.

1
  • Here's a slight variant on your answer: create a file that looks like this: #!/bin/sh ** export SUDO_ASKPASS=/usr/bin/ssh-askpass ** sudo "$@". (Replace the spaces and "**" characters between the hi-lighted sections with new lines.) Save that file somewhere like /usr/local/bin/gksudo. This will allow you to use this gksudo-substitute only when you want to. IDK what your solution will do in a non-graphical TTY, but I don't know if really want to find out the hard way. Also, with this solution, you don't need a reboot. – TSJNachos117 Feb 2 '20 at 4:55
1

XTerm is probably one's best choice.

xterm -e "sudo -b gedit /etc/fstab && sleep 1"

or

xterm -e su -c "gedit /etc/fstab &"

The latter assumes the root account is enabled and has a password set.

-e tells XTerm to open running a single command and -c tells su to run a single command as root. In the first alternative, -b sends the application to the background after authenticating, which acts the same way in the second alternative, with &. This is required since XTerm would remain open if the application is in the foreground. The sleep 1 command is required because, for a unknown reason, the application would fail to run.

If the first alternative fails, use the following.

xterm -e sudo su -c "gedit /etc/fstab &"

Here's an alias which resembles gksu.

gksu(){
  xterm -e "sudo -b $* && sleep 1"
}

Paste it to .bashrc. Make sure to relog before using it.

One could also place it in a binary directory so the command becomes system-wide.

cd /usr/local/bin && sudo su -c "echo 'xterm -e \"sudo -b \$* && sleep 1\"' > gksu && chmod 0755 gksu && ln -s gksu gksudo" && cd ~
0

Just a marginal addition for those who want to use a gksu supplement in a desktop file to run a WINE program as admin. In my case Total Commander was the target program (I apologize to those from whom this provokes resistance). I wanted to run it as admin from the Unity Launcher.

Here is my solution:

Exec=sh -c "zenity --password --title=\"sudo password prompt\" --timeout=10 2>/d
ev/null | sudo -S -H wine \"/root/.wine/drive_c/totalcmd/TOTALCMD.EXE\""

This will prompt you for the password, and then pass it to sudo.

Here is the whole content of the desktop file:

[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Icon[en_US]=/home/zsolti/MyApps/.icons/Total_commander_Admin64.png
Name[en_US]=TotalCommanderAdmin
Exec=sh -c "zenity --password --title=\"sudo password prompt\" --timeout=10 2>/d
ev/null | sudo -S -H wine \"/root/.wine/drive_c/totalcmd/TOTALCMD.EXE\""
Comment[en_US]=Total Commander Admin
Name=TotalCommanderAdmin
Comment=Total Commander Admin
Icon=/home/zsolti/MyApps/.icons/Total_commander_Admin64.png
0

WHAT ABOUT INSTALLING THE (DEPRECATED) GKSU BACK?

For anyone wondering if it's still possible to install gksu, the answer is possibly yes: even though its DEB packages are deprecated since ~2015, by December 9, 2020, they were still available for download at Launchpad. The following solution worked on the 64-bit (AKA amd64) XUbuntu versions 18.04 (upgraded from 16.04) and 20.04 (fresh install) that I'm currently using.


IN A NUTSHELL

What I did was to download these 7 DEB packages:

...and then install them with the help of gdebi: a DEB package manager that not only installs DEB packages but also checks and solves (i.e. installs) their dependencies. It is mandatory to install the multiarch-support package first, then install the libgcrypt11 package, and so on (following that strict order from the top till the bottom of the list) until you end up installing the last package (i.e. the gksu one).


INSTALLATION EXPLAINED (GRAPHICAL METHOD)

  1. For each one of the 7 links from the list above, right click the link, then select Save link as... and then save the DEB package in your Downloads folder (e.g. /home/yourname/Downloads).

  2. Start gdebi in graphical mode (i.e. gdebi's frontend). If you can't find gdebi in your Applications menu (it's usually located in System), open a terminal emulator window (CtrlAltT usually does the trick), then type sudo gdebi-gtk & and press Enter, type your password and press Enter again.

  3. At the gdebi window, click on File, then click on Open..., then open your Downloads folder, select the first package to be installed (i.e. multiarch-support_2.17-93ubuntu4_amd64.deb), click on Open and then click on Install Package. Repeat these steps for the other packages (don't forget to follow the strict order of the list above, from top to bottom). The last package to be installed will therefore be gksu_2.0.2-9ubuntu1_amd64.deb.

As soon as you're done performing these 3 simple steps, gksu will be available at your Applications menu.


INSTALLATION EXPLAINED (TERMINAL / TEXT-MODE METHOD)

  1. Start a terminal emulator window so you have access to the Linux shell ("command prompt"). Pressing CtrlAltT will likely do the trick.

  2. Type mkdir ~/gksu in the emulator window and then press Enter. This will create a gksu folder in your home folder (e.g. /home/yourname/gksu).

  3. Type cd ~/gksu in the emulator window and then press Enter. This will cause ~/gksu (i.e. the gksu folder in your home folder) to become your working directory (i.e. the folder where your current shell session is active, i.e. the folder to which your user account is currently connected through the Linux shell emulator window).

  4. Select and then copy the command below:

    wget http://launchpadlibrarian.net/153554150/multiarch-support_2.17-93ubuntu4_amd64.deb ; wget http://launchpadlibrarian.net/147472576/libgcrypt11_1.5.0-3ubuntu3_amd64.deb ; wget http://launchpadlibrarian.net/141785150/libgnome-keyring-common_3.8.0-2_all.deb ; wget http://launchpadlibrarian.net/141785305/libgnome-keyring0_3.8.0-2_amd64.deb ; wget http://launchpadlibrarian.net/182650077/libgtop2-10_2.30.0.is.2.30.0-0ubuntu1_amd64.deb ; wget http://launchpadlibrarian.net/182741643/libgksu2-0_2.0.13~pre1-6ubuntu7_amd64.deb ; wget http://launchpadlibrarian.net/193391454/gksu_2.0.2-9ubuntu1_amd64.deb
    
  5. Use CtrlShiftV to paste the above command in the emulator window and then press Enter. The wget program will then download all these 7 required DEB packages and place them all in ~/gksu.

  6. Once the download is finished, select and then copy this other command:

    sudo gdebi -n multiarch-support_2.17-93ubuntu4_amd64.deb ; sudo gdebi -n libgcrypt11_1.5.0-3ubuntu3_amd64.deb ; sudo gdebi -n libgnome-keyring-common_3.8.0-2_all.deb ; sudo gdebi -n libgnome-keyring0_3.8.0-2_amd64.deb ; sudo gdebi -n libgtop2-10_2.30.0.is.2.30.0-0ubuntu1_amd64.deb ; sudo gdebi -n libgksu2-0_2.0.13~pre1-6ubuntu7_amd64.deb ; sudo gdebi -n gksu_2.0.2-9ubuntu1_amd64.deb
    
  7. Use CtrlShiftV to paste the above command in the emulator window and then press Enter, then type your password and press Enter again. For every instance of the gdebi command, it will install the corresponding DEB package. The -n parameter tells gdebi to automatically solve package dependencies, thus gdebi won't ask you questions: instead, it will automatically install the main package and will also automatically detect, download and install its occasional DEB package dependencies.

As soon as you're done performing these 7 steps, gksu will be available at your Applications menu.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.