16

I use Trisquel GNU/Linux with GNOME Flashback Desktop Environment. I need a GUI password prompt for user for executing command with sudo in script. Example consider following script:

zenity --question --text="Do you want to install this package?"
if [[ $? -eq 0 ]]; then sudo apt-get install package
else zenity --warning
fi

Which is to be executed following way (Run) i.e not inside terminal:

screen-shot

So, It is needed to ask for password in order to run command with sudo otherwise it fails to do a job.

Therefore, How do I ask password by GUI prompt?

24

You can ask password by means of GUI prompt with the help of -A, --askpass options for sudo.

From the sudo manpage:

-A, --askpass
                 Normally, if sudo requires a password, it will read it from the user's terminal.  If the -A
                 (askpass) option is specified, a (possibly graphical) helper program is executed to read the user's
                 password and output the password to the standard output.  If the SUDO_ASKPASS environment variable
                 is set, it specifies the path to the helper program.  Otherwise, if sudo.conf(5) contains a line
                 specifying the askpass program, that value will be used.  For example:

                     # Path to askpass helper program
                     Path askpass /usr/X11R6/bin/ssh-askpass

                 If no askpass program is available, sudo will exit with an error.

So, you can graphical helper program such as ssh-askpass which prompts a user for a pass-phrase using GNOME:

$ which ssh-askpass
/usr/bin/ssh-askpass

So, Add the following line to /etc/sudo.conf:

# Path to askpass helper program
Path askpass /usr/bin/ssh-askpass

And you will find GUI password prompt:

screen-shot1

You can also user other program like zenity for that. Example I use following:

$ cat /etc/sudo.conf
# Path to askpass helper program
Path askpass /usr/local/bin/zenity_passphrase

Where zenity_passphrase is a custom script set to be used directly as command:

$ cat $(which zenity_passphrase)
#!/bin/bash
zenity --password --title="sudo password prompt" --timeout=10

Which works like:

screen-shot2


Note:

  • You can also use gksudo (GTK+ frontend for su and sudo) instead of sudo in the script which asks with GUI prompt (gksu and gksudo are obsolete and abandoned in 2019-2020):

    screen-shot3

  • You can also use pkexec (polkit application) with some (for others it needs to be configure) applications/commands:

    screen-shot

| improve this answer | |
  • When using pkexec, for example pkexec leafpad, it gives Cannot open display: after entering the password. Is there any aditional configuration needed? – GTRONICK Dec 13 '17 at 3:13
  • Try DISPLAY=:0 pkexec leafpad – Pandya Dec 13 '17 at 3:19
  • Still not working, the same message Cannot open display: appears – GTRONICK Dec 14 '17 at 0:53
  • gksu and gksudo is dead, abandoned in 2019 or before that. – woohoo Apr 29 at 1:03

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.