How do I ask password by GUI prompt while using sudo in script?

39,111

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

Share:
39,111

Related videos on Youtube

Pandya
Author by

Pandya

I use PureOS on old Desktop Computer and Debian GNU/Linux on my Laptop. I want to switch to Guix in future! You may read my Computing History in this meta post :) I recommend visiting Philosophy of GNU Project.

Updated on September 18, 2022

Comments

  • Pandya
    Pandya over 1 year

    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?

    • Noam Manos
      Noam Manos over 2 years
      A oneliner could do it: echo $(zenity --password --title="Enter sudo password") | sudo -S [your command]
  • GTRONICK
    GTRONICK over 6 years
    When using pkexec, for example pkexec leafpad, it gives Cannot open display: after entering the password. Is there any aditional configuration needed?
  • Pandya
    Pandya over 6 years
    Try DISPLAY=:0 pkexec leafpad
  • GTRONICK
    GTRONICK over 6 years
    Still not working, the same message Cannot open display: appears
  • scrat.squirrel
    scrat.squirrel about 4 years
    gksu and gksudo is dead, abandoned in 2019 or before that.