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.
I found this command
in this page
After running the alias command:
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.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.
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.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.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, usingpkexec backup
in a script initiated from a .desktop file. I added this new file to/usr/share/polkit-1/actions
containing: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…
Nautilus Admin (nautilus-admin) is a simple Python extension for the Nautilus file manager that adds some administrative actions to the right-click menu:
To install Nautilus Admin in all currently supported versions of Ubuntu open the terminal and type:
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:
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.I use a script called
sgedit
which inherits user preferences for font, tabs, and extensions. It usessudo -H gedit
instead ofgksu gedit
for stability in GUI environment. It prompts for a password.Have
sudo
inherit your user accountgedit
settingsIn 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 belowsgedit
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:sgedit filename1 filename2...
sudo -H
to preserve file ownership whilst getting root powers.gsettings set
on the differences only (reduces 174set
commands to a dozen or less. Next time it's run perhaps only one or two changes but often times none.Bash script
sgedit
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:
Note
~
is a shortcut for/home/YOURNAME
.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:Create new file
my-gksudo.sh
:file content:
Create new files gksudo and gksu with identical content:
and
file content for both:
Make gksudo and gksu executable:
And
Reboot the computer.
XTerm is probably one's best choice.
or
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.
Here's an alias which resembles gksu.
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.
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)
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
).Start
gdebi
in graphical mode (i.e.gdebi
's frontend). If you can't findgdebi
in your Applications menu (it's usually located in System), open a terminal emulator window (CtrlAltT usually does the trick), then typesudo gdebi-gtk &
and press Enter, type your password and press Enter again.At the
gdebi
window, click on File, then click on Open..., then open yourDownloads
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)
Start a terminal emulator window so you have access to the Linux shell ("command prompt"). Pressing CtrlAltT will likely do the trick.
Type
mkdir ~/gksu
in the emulator window and then press Enter. This will create agksu
folder in your home folder (e.g./home/yourname/gksu
).Type
cd ~/gksu
in the emulator window and then press Enter. This will cause~/gksu
(i.e. thegksu
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).Select and then copy the command below:
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
.Once the download is finished, select and then copy this other command:
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 tellsgdebi
to automatically solve package dependencies, thusgdebi
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.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:
This will prompt you for the password, and then pass it to sudo.
Here is the whole content of the desktop file: