This is quite tricky I guess, or at least not desired very often.

What I am not after is:

Disabling password with

 user    ALL=(ALL) NOPASSWD:    ALL

Or a similar trick.

I know how to read sudo password from stdin, and this is working fine but you still get the prompt:

$ echo $PW | sudo -S whoami
[sudo] password for oz123: root

The man page shows I can use -p.

$ echo $PW | sudo -S -p "foo " whoami
foo root

$ echo $PW | sudo -S -p " " whoami

A single space works.

Passing an empty string does not seem to work ...

$ echo $PW | sudo -S -p "" whoami
[sudo] password for oz123: root

So the question is:

How do you define an empty string as a valid value for -p?

update - sudo version

$ sudo --version
Sudo version 1.8.21p2
Sudoers policy plugin version 1.8.21p2
Sudoers file grammar version 46
Sudoers I/O plugin version 1.8.21p2

(on ubuntu 18.04)

  • I wonder if a null \0 is allowed? – Zoredache May 18 '19 at 7:28
  • Nope, $ echo $PW | sudo -S -p '\0' whoami gives \0root – Oz123 May 18 '19 at 8:28
  • I have sudo 1.8.22 and can specify an empty string with -p (openSUSE 15.0). what version are you using? – Luuk May 18 '19 at 8:55
  • If you are also using -S you can redirect the prompt with ` 2>/dev/null` (along with any error messages). – meuh May 18 '19 at 9:11
  • 2
    What is the actual problem you're trying to solve? Why do you want the prompt to not be visible? – Jenny D May 18 '19 at 9:38

It seems that with newer versions of sudo this problem is solved:

On my gentoo laptop:

$ sudo -k
$ echo "$PW" | sudo -S -p "" whoami

$ sudo --version
Sudo version 1.8.25p1
Sudoers policy plugin version 1.8.25p1
Sudoers file grammar version 46
Sudoers I/O plugin version 1.8.25p1

On older Ubuntu machines you can simply redirect the prompt to /dev/null:

sudo -S 2>/dev/null whoami

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.