0

I am running some scripts as part of pre/post deploy using DeployHQ, I need to be able to call the script on one line and for there to be no feedback from the terminal so I want to pass the password into the sudo command (instead of sudo asking for the password at the time of use)

I am trying to get sudo to work with stdin by doing something like;

echo my_password | sudo -S chown -R my_user:www-data /srv/www/my_site/public_html/

But what is getting returned is;

[sudo] password for my_user: 
 Sorry, try again.
[sudo] password for my_user: 
 sudo: 1 incorrect password attempt

The password in question is correct and when the command is ran without -S and the user is prompted for the password is works.

Could someone point out what I am doing wrong?

Just to clarify - I want there to still be a password, just one that I can pass in.

Toby
  • 630
  • 2
  • 7
  • 17
  • I think your problem is described here:http://serverfault.com/questions/510888/sudo-u-fails-with-env-u-no-such-file-or-directory –  Jun 04 '15 at 07:56
  • did you ever figure out what your problem was? I'm having the exact same issue and I've tried everything. – Nate-Wilkins Dec 24 '22 at 05:37

2 Answers2

1

from the sudo manual:

The -S (stdin) option causes sudo to read the password from the standard input instead of the terminal device. The password must be followed by a newline character. This means you should end password with \n

EDIT: I did a couple of test, and honestly in my test system your syntax works correctly. ( echo mypass | sudo -S .... )

2nd EDIT: Try with the following syntax: echo -e "mypass\n" | sudo -S command

0wn3r
  • 866
  • 1
  • 8
  • 21
  • Thanks for your answer, I hadn't noticed the newline character requirement before (mine just says `-S The -S (stdin) option causes sudo to read the password from the standard input instead of the terminal device.`) - I have tried this to no avail. – Toby Feb 12 '13 at 14:14
  • have you tried the syntax I posted with the -e parameter to echo? echo -e "mypass\n" | sudo -S command – 0wn3r Feb 12 '13 at 14:37
  • Yeah I did, unfortunately nothing. – Toby Feb 12 '13 at 19:08
0

I'm not sure what is wrong

However, if you want to run sudo without being prompted for a password, you can do so by editing the sudo configuration file. To do this use the command visudo

The default file has lots of examples so just search for NOPASSWD and you should find an example to get you started

Phil
  • 3,168
  • 1
  • 22
  • 29
  • I do want to require a password, just not be prompted - I will edit my question to reflect this. – Toby Feb 12 '13 at 12:55