1

When I google “gksu gedit” one of the top entries (How to run gedit and nautilus as root) advises that gksu usage is frowned upon:

gksu hasn't been updated since 2009 and is not recommendedany more. In fact, Ubuntu no longer ships with gksu by default (though it may be installed for many of you, because some apps still depend on it) and it may even be completely removed at some point.

A link to AskUbuntu (Why is gksu no longer installed by default?) suggests pkexec should be used instead but doesn't provide clear and concise steps to do this.

How can I use pkexec within a new wrapper shell script called gsu that supports both gedit and nautilus?

I feel a wrapper script called gsu is most appropriate because my "muscle memory" is to type gksu all the time and I don't want to have to type pkexec instead.

Additionally I would like those pesky gtk warning messages to disappear. Also I would like the terminal prompt back right away instead of waiting for editor or file manager to end.

AU Duplicates: I've searched many Q&A but none are asking (or offering how to) give a complete solution for gsu wrapper script to call pkexec AND install all necessary policy kits AND get rid of pesky gtk warnings AND get terminal prompt back right away... All in one answer.

1 Answer 1

2

Before you can use pkexec with gedit and nautilus you need to copy the polkit rules to support them. This will automatically be done in Ubuntu 17.04 but in the mean time you need to wget them.

Nautilus Policy Kit

wget https://raw.githubusercontent.com/hotice/webupd8/master/org.gnome.nautilus.policy -O /tmp/org.gnome.nautilus.policy
sudo cp /tmp/org.gnome.nautilus.policy /usr/share/polkit-1/actions/

Gedit Policy Kit

wget https://raw.githubusercontent.com/hotice/webupd8/master/org.gnome.gedit.policy -O /tmp/org.gnome.gedit.policy
sudo cp /tmp/org.gnome.gedit.policy /usr/share/polkit-1/actions/

"gsu" bash script to replace "gksu"

Creating a bash script is one of two ways to call pxexec using the somewhat familiar term of gsu. Create this file in one of your paths:

#!/bin/bash

# Usage: gsu gedit file1 file2...
#  -OR-  gsu natuilus /dirname

# & is used to spawn process and get prompt back ASAP
# > /dev/null is used to send gtk warnings into dumpster

COMMAND=$1 # extract gedit or nautilus

pkexec "$COMMAND" "${@:2}" &> /dev/null&

Save the file and mark it as executable with chmod +x gsu

Now instead of typing gksu to edit grub configuration you can use:

gsu gedit /etc/default/grub

"gsu" as an alias of "pkexec" to replace "gksu"

Creating an alias is the second option to call pxexec using the somewhat familiar term of gsu. Open the file ~/.bashrc and search for alias. You will see this:

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

After the last line add this:

alias gsu='pkexec'

Save the file and exit.

6
  • 1
    1. Why not do sudo wget ... -O /usr/share... directly, instead of downloading to /tmp and then copying? 2. If those rules will be in 17.04, they will be available in Launchpad, straight from Ubuntu's sources. Why not download from there instead of copying from $RANDOM_GITHUB_REPO? 3. Why would you quote $1 in COMMAND="$1", and then not quote $COMMAND when calling pkexec? 4. Why not just pkexec "$@" in that script? 5. In fact, why not just do pkexec gedit ... directly instead of this script, now that the rules are installed?
    – muru
    Nov 10, 2016 at 12:03
  • @muru the wget instructions come directly from the first link story. I simply copy and pasted them into my answer because they worked. Tonight after work I'll research wget with your ideas. I picked gsu over pkexec because it's easier to remember coming off of gksu background. Also to get benefits of a wrapper script. Down the road I might modify it to log every file edited with date and time to /var/log/gedit history file or something similar. I'll reply to your other questions later after work. Nov 10, 2016 at 12:10
  • What benefits of a wrapper script? O.o Also: pkexec calls are logged, just like sudo commands are logged.
    – muru
    Nov 10, 2016 at 12:12
  • The gsu wrapper script redirects the pesky gtk warnings to /dev/null without having to setup root shares. The wrapper also returns the command prompt immediately after pkexec starts. I'm not sure what future enhancements will be put into the wrapper, ie backing up source file and running diff to new saved file, but the flexibility is there. Nov 10, 2016 at 12:41
  • Yes, but Muru's point is that you can do the same thing with a simple alias gksu='pkexec'. That won't redirect errors but you really don't want to redirect them. What if there's an actual error and not just gtk warnings?
    – terdon
    Nov 10, 2016 at 14:38

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.