how can i switch user using su command and adding password from file ? i want to make a script which will automaticly switch the user, having the user and the password ,instead of manually typing the password ? P.S. I don't have super user rights.

  • the only way of password less su , is to add the current user in the wheel group and then do sudo su <user> it ll not ask for password at all – Ijaz Ahmad Khan Jul 20 '16 at 11:32
  • 2
    @IjazKhan sudo su is a useless use of su. sudo swiches the user, the su serves no purpose. – Patrick Jul 20 '16 at 12:29

Typically programs like su require passwords to be entered on the terminal and can't be bypassed. The root user doesn't need to enter a password to switch to another, so it's common to see solutions like sudo su user.

However we might be able to cheat. If you have the expect command on your system then we can fake typing the password

#!/usr/bin/expect -f

set user [lindex $argv 0]
set password [lindex $argv 1]

spawn /bin/su $user

expect "Password:"
send "$password\r";


With this program you can do

./autosu username password


% autosu root imnottellingyou
spawn su root
[root@server /]# 

It's not normally a good idea to do this exact thing, though, 'cos the password will appear in your shell history when you type it on the command line. But if you have the password inside a script or data file then it doesn't matter too much! You should be able to modify this simple example to meet your needs.

If you don't have expect installed then it gets harder; ask your admin to install it, or compile it from source and run it from your directory!


The best option is use Sudo rulles
Safe passwords in text file is in top 10 wrong pratice IT. but if you want use you must rememebr parwd is not read from stdin but ftom tty so you can use expect(*1) but this is still wrong way . Beter option is use sudo or sshkey you can login to ssh account using public key to localhost address .

do not try su - username <<< password


    su username << - _EOF_ 

this only can run command in another sheell and give you output.

*1 https://stackoverflow.com/questions/29174337/trouble-logging-in-user-using-su-and-expect-script

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.