No alternative for this gksu usage

821 Views Asked by At

Ubuntu 18.04 removed the gksu package entirely from its repositories.

The rational behind this is that gksu is thought to be used only in this way:

gksu gedit /some/root/setting

But there are other usages of gksu as well. Gksu is an important program because unlike sudo or graphical programs like pkexec it prevents other programs from stealing focus.

So when you want to type your password using simple cli sudo, and some chat window pops up, what happens is that the password is entered into the chat window (if you don't look at the screen for a moment), and your root password is sent to you chatting buddy. It happened to me once and I don't want it to happen ever again.

This is why gksu is ideal security wise, and I use it often at command line level. So not to launch gedit, but to do command line stuff, e.g. apt-get update && apt-get upgrade or execute some bash scripts.

And no, pkexec is not an alternative to gksu, because 1) it does not execute scripts, only binaries and 2) it doesn't prevent other programs from stealing focus.

Secondly, you can also use gksu to ask for non-root passwords in a secure manner. For example:

ccrypt -K `gksu -p --message "Password:"` -d data.tar

So my question is: what program can be used as an alternative to gksu in these cases?

2

There are 2 best solutions below

0
On

This answer deals only with gksu gedit and nothing else.

Besides the disappearance of gksu with Ubuntu 17.10, I've had long standing problems where my regular user settings for font name, font size, tab stops, convert tabs to spaces, etc. were never carried over to sudo / gksudo and I'd always have to reset them.

Furthermore sudo gets no top-level menu for editing preferences.

The script I wrote not only allows gksu-like usage with gedit but also copies my user gedit preferences over to the sudo account.

You will need to change one line of code:

nohup gedit -g 1300x840+1+1220 $@ &>/dev/null &

This positions the window 1 pixel right and 1220 pixels down. You might prefer 1+1 instead of 1+1220.

The window size is set to 1300 pixels wide by 840 pixels high. You might prefer a smaller or larger size depending on Monitor resolution and HiDPI. So change 1330x840 to your preferences.

#!/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 &
    nohup gedit -g 1300x840+1+1220 $@ &>/dev/null &
#              Set the X geometry window size (WIDTHxHEIGHT+X+Y).

}

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

exit 0

Create the bash script somewhere in your path and mark it as executable using:

sudo chmod a+x /path/to-script/sgedit

Then to call it use:

sgedit <file-name>

instead of the old system of:

gksu gedit <file-name>

HTH

1
On

You can still obtain gksu as follows:

wget http://mirrors.kernel.org/ubuntu/pool/universe/libg/libgksu/libgksu2-0_2.0.13~pre1-9ubuntu2_amd64.deb

sudo apt install ./libgksu2-0_2.0.13~pre1-9ubuntu2_amd64.deb

wget http://mirrors.kernel.org/ubuntu/pool/universe/g/gksu/gksu_2.0.2-9ubuntu1_amd64.deb

sudo apt install ./gksu_2.0.2-9ubuntu1_amd64.deb