Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I know the security risks, but I am performing this experiment/excercise on an old, seldom used *nix machine I have laying around the house. So doing this excerise will not bring any security risk to my machine. Having said that, please do not post any comments regarding security.

I also know I can change sudoers to disable password prompts, but I am not interested in that.

I have found a command:

echo "password" | sudo -S su

but which works very oddly. I type it in, and the domain stays the same; I don't change into root. Is there something else I'm missing or is there a better way to do this?

Things I've done:

  1. added a \n to the end of the password: password doesn't verify correctly.
  2. ommitting the su: prints out usage on how to use sudo correctly
share|improve this question
What is the output of printf '%d\n' "$UID" "$EUID" before and after doing this? –  Chris Down Sep 2 '12 at 19:53
~$ printf '%d\n' "$UID" "$EUID" => 1000 1000 –  Zchpyvr Sep 2 '12 at 20:18
Possible duplicate of stackoverflow.com/questions/233217/pass-password-to-su-sudo-ssh –  warl0ck Sep 6 '12 at 5:22
add comment

1 Answer

It's rather strange, but this is what works for me:

% echo -e "password\n" | sudo -S su
<myusername>'s password:HOSTNAME: Undefined variable.
% sudo -S su

So in the first step, the password is passed to sudo (using the -e flag to echo so that \n is really a newline), and in the second run, the authenticaion works without prompting for any password.

(The return in the second line is probably due to an improperly set up system...)

Edit: In that xterm session, I can call sudo -S <command> as often as I want... That's probably because I'm within the time limit in which the sudo password has to be entered only once.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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