0

So, I have a script that needs to install a software but the script needs to be run as root.

I have created a desktop icon on the GUI (using Ubuntu desktop) where ideally, the user will click this icon, it will prompt for password and then run the script as root.

So far the script does what is intended if ran as root, but not as the user. Script looks like this:

 #!/bin/bash -l

zenity --question --text "This script will reboot after installation is complete" --title="Warning\!" 2> /dev/null
if [ $? = 0 ]; then
  install dependencies 
  dpkg -i package.deb
  etc
fi

reboot

However, if I click on the icon to run the script, it loads and it doesn't do anything, then I check the logs and realize that it fails: sudo: no tty present and no askpass program specified I know I can modified the sudoers files but I do not want to do this as I need to keep this user independent.

Is there a way to force when running the script to ask the user to become root? or to force the GUI to ask for a password? what is the best way to get this done? I need users to run this program but I need to run the program as root and sudo is not doing it for whatever reason.

user3311890
  • 181
  • 2
  • 7
  • The question I posted as a duplicate doesn't exactly match yours, but the answer does. You *do* need to edit the sudoers file, but you don't need to make the changes user-specific. – Jenny D Jun 28 '17 at 06:32

1 Answers1

1

The error message you received tells you what you need to do:

sudo: no tty present and no askpass program specified

You need to configure an askpass program in order to make it work. If you haven't installed one you first need to install one. It looks like ssh-askpass should work.

sudo apt-get install ssh-askpass

Next you need to configure sudo to use it. That can be done through sudo.conf or an environment variable. Since you are writing a script I guess you want to use an environment variable. I tested this command:

SUDO_ASKPASS=/usr/lib/ssh/x11-ssh-askpass sudo -A ls

The first part sets the environment variable. The -A argument forces sudo to always use the GUI to ask for a password, even if it is running in a terminal.

kasperd
  • 29,894
  • 16
  • 72
  • 122