9
votes

I have added a user like this:

$ adduser --system --home /no/home --no-create-home --group --disabled-password --disabled-login testuser

Added a user to a group:

$ adduser testuser testgroup

added lines to sudoers (visudo):

testuser    ALL=(ALL) NOPASSWD: ALL
%testgroup         ALL=(ALL:ALL) NOPASSWD: ALL

When I try to run the bash script with the following content:

#!/bin/sh
sudo -u testuser /usr/bin/php /usr/local/bin/script.php

But when I run this script, I get the error in the log:

sudo: no tty present and no askpass program specified

Edit: requiretty is not in the sudoers file.

3
Aren't sudo permissions about the user/group you are changing from not the user you are changing to? So aren't those lines letting the testuser user and the testgroup group run any command (as anyone) without a password? Don't you need to give permission to the user running the script to run commands as the testuser for what you want? Assuming that's what you meant to allow?Etan Reisner
@EtanReisner I have tried it too, but still getting the same error.Tim Mishutin
You tried what too? Running sudo as the testuser? What specifically did you run/try? What user is running the original script?Etan Reisner
@EtanReisner You were right. I had to add NOPASSWD for the client's group. Now it works correctly. Thank you.Tim Mishutin

3 Answers

8
votes

sudo permissions are about the user/group you are changing from not the user you are changing to.

So are those permission lines are letting the testuser user and the testgroup group run any command (as anyone) without a password.

You need to give permission to the user running the script to run commands as the testuser user for what you want.

Assuming that's what you meant to allow that is.

4
votes

That error occurs when your sudoers file specifies requiretty. From the sudoers manpage:

   requiretty      If set, sudo will only run when the user is 
                   logged in to a real tty.  When this flag is set, 
                   sudo can only be run from a login session and not 
                   via other means such as cron(8) or cgi-bin scripts.  
                   This flag is off by default.

To fix your error, remove requiretty from your sudoers file.

0
votes

I fixed it by login to the server and append the following lines to the ssh-server configuration:

> vim /etc/ssh/sshd_config

Match User <your user name>
    PermitTTY yes

So I don't need the -t options permanently.