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 of sudo
in the script which asks with GUI prompt (gksu
and gksudo
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:
Best Answer
Zenity is a good tool for that.
That assigns to variable
user_input
whatever the user types in the GUI window, unless the user presses cancel, in which case the exit code is not zero.Gxmessage is an alternative, with very similar syntax.
More information in
man zenity
andman gxmessage
.