I know the security risks, but I am performing this experiment/exercise on an old, seldom used *nix machine I have laying around the house. So doing this exercise 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. omitting the su: prints out usage on how to use sudo correctly

3 Answers 3


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.


I used the following

echo password | sudo -S su - user_to_switch_to

then run

sudo -S su - user_to_switch_to

to switch to the user you want without manually entering in the password.

(N.B. replace password and user_to_switch_to with your pwd and user)


I have the nagging feeling that you are trying to run an interactive shell form su (and friends) while doing i/o redirection. I don't think that will work namely echo passwd| will give you an eof (end of file) after the password. I think you are going to need something like expect or a dial script utility. expect has a pass-through mode which can be invoked at the end of the script. this is not needed if your command is not interactive like xterm (which although fully interactive does not need standard input. also watch out for some password being read from standard error which makes things interesting to say the least. (they do that so that i/o redirection is less likely to interfere with your password prompt)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .