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/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
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

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.

share|improve this answer

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)

share|improve this answer

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)

share|improve this answer

Your Answer

 
discard

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.