4

I have passwordless access to a server. My user account has added in sudoers file for NOPASSWD usage for some services/daemons in /etc/init.d/.

I want to write a script to start or stop a daemon according to some test results. I had set a cron to run this script at a specific time. But it returns error "sudo: no tty present and no askpass program specified".

I can use the " sudo /etc/init.d/service name start/stop without password in terminal.I do not have root password.

I have double checked /etc/sudoers file. There is no "requiretty". I am using Ubuntu-10.04 , 2.6.32-24-generic.

Following are the active lines in sudoers:

Defaults        env_reset
root    ALL=(ALL) ALL
bijo ALL=NOPASSWD: /etc/init.d/apache2
%sudo ALL=(ALL) ALL
%admin ALL=(ALL) ALL
1
  • are you calling the script on the server or from your remote machine? May 19, 2011 at 8:29

4 Answers 4

7

If you are running the script as the same user as the one that has no password access then it should work correctly, i would make sure that the script is running as the correct user, I would also test the script by manually calling it as the right user.

You may also need to check that the following line doesn't exist in the sudoers file or that you have a further line which states your user doesn't require a TTY

Defaults requiretty

if that line is present you can either comment it out (not a good idea) its probably better to add the following in

Defaults:<USERNAME> !requiretty

where USERNAME is your user

4
  • Thank you for your reply. Sudoers file does not contain Defaults requiretty. I am using cron to run the script. If run the script manually, it works fine...
    – Bijo
    May 19, 2011 at 8:32
  • One more similar question: I have sudoers entry bijo ALL=NOPASSWD: /etc/init.d/apache2 and set a cron as */2 * * * * sudo /etc/init.d/apache2 start >>testingcrn.out 2>&1 Want to start/stop apache using cron. It return error "sudo: no tty present and no askpass program specified" in the output file testingcrn.out. Any Idea??
    – Bijo
    May 19, 2011 at 8:48
  • 1
    Are you sure about the requiretty line not being present ? I think its default on all major distros.
    – Sirex
    May 19, 2011 at 9:17
  • 1
    The only possibility, if it's not requiretty, is that your defined sudo line is not getting hit when it's called by cron due to a different PATH. Quick way to test, though; just set ALL=(ALL) NOPASSWD: ALL for the user running the crontab and see if the error still happens. If it does, your issue is requiretty; if it doesn't, your issue is probably PATH.
    – BMDan
    May 20, 2011 at 2:39
1

I think you need to use ssh -t

7
  • only if he is doing the command remotely from another server right? May 19, 2011 at 8:23
  • oops, i assumed its from a remote server, some how can't down vote my own answer May 19, 2011 at 8:25
  • he didnt specify what way he is triggering so it may still be valid May 19, 2011 at 8:26
  • Thank you for your reply. Where I can use it? I do not have root password. Plz help......
    – Bijo
    May 19, 2011 at 8:28
  • Bijo are you calling the script via cron on the server itself or from your local machine? May 19, 2011 at 8:30
1

That complaint message comes from part of the code which is asking for the password, so is complaining because without a tty, it can't disable echo. You can prove to yourself that you're in this code-path by setting the visiblepw option in sudoers, which will avoid that warning and let you see the password prompt.

So ultimately, something is wrong with the NOPASSWD: control.

When multiple rules in sudoers match, the last match is used, not the most specific. So user bijo is in %sudo or %admin; thus the NOPASSWD: control is not applied.

Did your testing at the command-line happen before you logged out/in after adding yourself to one of those groups?

0

Add Defaults !requiretty line to the bottom of the file. This will explicitly turn off requiretty for everything.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .