How do I ask password by GUI prompt while using sudo in script?
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:
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:
Note:
-
You can also use
gksudo
(GTK+ frontend for su and sudo) instead ofsudo
in the script which asks with GUI prompt (gksu
andgksudo
are obsolete and abandoned in 2019-2020): -
You can also use
pkexec
(polkit application) with some (for others it needs to be configure) applications/commands:
Related videos on Youtube
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, 2022Comments
-
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:
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 over 2 yearsA oneliner could do it:
echo $(zenity --password --title="Enter sudo password") | sudo -S [your command]
-
-
GTRONICK over 6 yearsWhen using pkexec, for example
pkexec leafpad
, it givesCannot open display:
after entering the password. Is there any aditional configuration needed? -
Pandya over 6 yearsTry
DISPLAY=:0 pkexec leafpad
-
GTRONICK over 6 yearsStill not working, the same message
Cannot open display:
appears -
scrat.squirrel about 4 years
gksu
andgksudo
is dead, abandoned in 2019 or before that.