I defined 2 user accounts:
- one with admin privilege (with
sudo
right) => lets call itadminuser
. - a 2nd one without any privilege => lets call it
normaluser
and I configure the autologin on this 2nd usernormaluser
.
So when I open a normaluser
session and want to run an application with admin privilege,
I open a terminal Ctrl+Alt+T and:
su adminuser
sudo anyapplication ...
It works fine, without having to quit the normaluser
session (no need to open a adminuser
session).
But what should I do if the application needs to run with a GUI (Graphic User Interface) ?
I though about that:
su adminuser
gksu anyapplication ...
but I get
** (gksu:9122): WARNING **: the connexion is closed
No protocol specified
No protocol specified
(gksu:9122): Gtk-WARNING **: cannot open display: :0.0
Terminology
In this answer:
normaluser
is a normal user who is not an administrator and cannot run commands asroot
withsudo
.admin
is an administrator who can run commands asroot
withsudo
. (Of course, any graphical commands should use a graphical frontend likegksu
/gksudo
, and notsudo
directly.)anyapplication
is the name of the graphical applicationnormaluser
wants to run asroot
.normaluser
knowsadmin
's password and has (presumably) been told s/he may use it for this purpose.The Problem
The cause of your problem, and the reason most of the other answers so far don't work (with the exception of Marty Fried's excellent answer), is:
gksu
can be configured to use eithersudo
orsu
as its backend. The default behavior ofgksu
in Ubuntu is to act as a frontend forsudo
, not forsu
. That is to say that, by default,gksu
andgksudo
behave exactly the same. See the manpage.normaluser
is not an administrator and thus cannot run commands asroot
withsudo
.sudo
prompts for the password of the user running it, not the password of the user they want to become. Not being able to use your password to perform actions as people who aren't you is what it means to not be an administrator.normaluser
, provided it is not a Guest account, can run commands as another user withsu
, putting in the other user's password. Butgksu
acts as a frontend forsudo
, notsu
.normaluser
cannot directly run any command asroot
, becausenormaluser
cannot usesudo
, and nobody can becomeroot
withsu
because there is noroot
password.The Solution
The solution requires writing a command that performs two authentication steps:
normaluser
must becomeadmin
to run a graphical command. To do this,normaluser
must rungksu
with the-w
flag to make it run in su-mode instead of the default sudo-mode, and the-u
flag to run the command asadmin
instead ofroot
.admin
must invokegksu
without the-w
flag to usesudo
to becomeroot
.Here's the command (yes, I have tested it ;-)):
You will be prompted for a password twice:
admin
's password, to letnormaluser
run a command asadmin
with thesu
backend.admin
's password, to letadmin
run a command asroot
with thesudo
backend.That's right. You enter
admin
's password twice.Miscellaneous notes:
gksu
withgksudo
to make it less confusing. In Ubuntu, they are equivalent. (You can also replace the firstgksu
withgksudo
, but that would be extremely counterintuitive and confusing.)-w
is the short form of--su-mode
.-S
is the short form of--sudo-mode
but neither has to be used because sudo-mode is the default.xclock
is a nice simple clock-window application.One way that will probably work is to use "sux" rather than "su" when you first switch to the admin user. sux fixes the problem of running x applications from the spoofed user. It is in the standard repo, and can be installed by entering
sudo apt-get install sux
at a commandline.Then, just use "sux" instead of "su" and it should work the way you expect.
Lets reuse the example of the application
xclock
:PAM can take care of it
This works for me on Ubuntu 16.04 (edit: it works too on 18.04 LTS):
put the line:
somewhere in:
and/or
and then doing "su -" or "sudo su -" I can use graphical apps as root.
pkexec
There is an ubiquitous alternative to kdesudo and gksu -
pkexec
which is frompolicykit-1
package that is required by lots of packages.For me worked this:
Here you have to replace the
yourcommand commandoption1 commandoption2
part with real command and it's argsIn Lubuntu, there's a tool called
lxqt-sudo
. It's in the official repositories. It works!lxqt-sudo/groovy
, now0.15.0-0ubuntu1 amd64
, is a Graphical Qt frontend for plainsudo
.https://packages.ubuntu.com/focal/lxqt-sudo
Instead of
I suggest you to try
gksu -u admin anyapplication
, where you do everything using thegksu
command itself. Also please make note that you have to enter the password of the user mentioned in the command, ie, in this case you have to enter admin's password.I would typically use the following logic in my scripts so that they will always request privilege escalation themselves using the appropriate method:
You can use
sudo
with GUI using the options-sE
, as follows:sudo -sE GUI_CMD
For example, if you want to run
nemo
as root:sudo -sE nemo
Here's the command to accomplish this.
Run it without running
su
first. You only need to run the above command from a normal user session and the application will be run as root.