3

I'm trying to use the -S stdin arg on sudo.

I tried this:

echo 'mypass' | sudo -S cat /etc/shadow

And

echo 'mypass\n' | sudo -S cat /etc/shadow

As the man pages suggest to add a newline.

Both won't work. What is wrong?

3
  • 1
    By the way, this is a horrible security risk. Your password is now stored in plaintext in ~/.bash_history. – terdon Dec 8 '17 at 14:00
  • I'm aware of this. But I see no other method for 'no prompt' sudoing. – Mathias Maes Dec 8 '17 at 14:13
  • You can set it up to be passwordless (which is, obviously, not a good way to increase security) or you can write the password into a file only you can read and pass the file's contents to sudo. This is, again, not perfect, but t least it won't be in the history file. Alternatively, you run things as root in an sudo -i session. – terdon Dec 8 '17 at 14:39
4

echo command won't print newlines with the \n sequence. Use printf instead.

printf "mypassword\n" | sudo -S cat /etc/shadow
8
  • I just tested. It didn't work. But then I changed my password to something more simple, and it dit work. Odd. – Mathias Maes Dec 8 '17 at 13:56
  • Probably you have shell metachars in your password, consider wrapping your password in single quotes rather than double quotes to prevent parsing of metachars. – Mukesh Sai Kumar Dec 8 '17 at 13:57
  • The OP was already using single quotes. And the echo prints a newline by default, so there's no need for two. @MathiasMaes does your password contain the character '? That would break what you are attempting. – terdon Dec 8 '17 at 14:01
  • 1
    @MathiasMaes As you are providing input from two places (the pipe | and shell redirect <<), the input is taken from the second place, not the first. You may correct it by wrapping around brackets: (printf 'testpassword\n' | sudo -S chpasswd) << (echo 'magjefzuzkfnkbexqgxcjnkju:thisisatest') – Mukesh Sai Kumar Dec 8 '17 at 14:14
  • 1
    Am a newbie too, but I think you may add a space between those <'s like this: (commands) < <(echo newpassword) – Mukesh Sai Kumar Dec 8 '17 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.