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
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
if that line is present you can either comment it out (not a good idea) its probably better to add the following in
where USERNAME is your user
I think you need to use
ssh -t
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 userbijo
is in %sudo or %admin; thus theNOPASSWD:
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?
Add
Defaults !requiretty
line to the bottom of the file. This will explicitly turn off requiretty for everything.