Sudoers NOPASSWD: sudo: no tty present and no pass program specified

I added this user:

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

User added to group:

 $ adduser testuser testgroup 

Added lines for sudoers (visudo):

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

When I try to run a bash script with the following contents:

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

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

 sudo: no tty present and no askpass program specified 

Edit: requiretty not in the sudoers file.

+9
linux bash tty sudo
source share
3 answers

sudo Permissions apply to the user / group with which you are changing, from the user you are changing.

Thus, these permission lines allow the user testuser and the group testgroup to run any command (like anyone else) without a password.

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

Assuming you wanted to do this.

+8
source share

This error occurs when the sudoers file indicates 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 the sudoers file.

+4
source share

I fixed this by logging into the server and adding the following lines to the ssh server configuration:

 > vim /etc/ssh/sshd_config Match User <your user name> PermitTTY yes 

Therefore, I do not need the -t options permanently.

0
source share

All Articles