Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

As we know that, password must be inputted while switching user [Username: test, Password: test]

[user1@hosts ~] $ su test
[test@hosts ~]$ 

Now I want to create a shell script (example: to switch user without entering password.

My shell script is:

su - test
test   # The password of user test

But it still needs password entering.

How can the shell script accept the password? Thanks in advance!!

share|improve this question
Have a look at this answer, in this case sudo is the tool you need. – Shadok Jan 28 '13 at 11:38
@Shadok That requires you to enter a password at some point. I guess the OP needs expect. – slhck Jan 28 '13 at 11:39
Thanks for @Shadok, but I don't think the answer is what I want. I just want to switch user by script, but execute command by another user. – Marslo Jan 28 '13 at 12:16
Hi @slhck , I have finished my problem by using ssh. However, I think expect is a good idea. I don't use that command before, but I will have a try. Thanks!! – Marslo Jan 28 '13 at 12:46
up vote 1 down vote accepted

Actually, I found another method to switch the user without password (Even if the new user needs password authentication).

That is using ssh

Alias file in user1:

alias tt="ssh test@"

And cat the user1's to test's authorized_keys: user1:

  • user1: create ssh public key:

    $ ssh-keygen -t rsa

    press yes if asking...

  • upload the user1's public key to test's directory (this step needs input the password):

    $scp test@

  • add the into .ssh/authorized_keys

    $ su - test


    $ cd $ cat >> .ssh/authorized_keys

    $ chmod 644 .ssh/authorized_keys

Then switch user by using tt

$ tt
Last login: Mon Jan 28 20:27:34 2013 from localhost
[test@host ~]$
share|improve this answer

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.