This question already has an answer here:

I want to SSH into a remote machine but instead of typing the password, I want to redirect it from another file.

So I have a file password.txt that stores the password. File.sh is my bash file.

In File.sh

ssh -T user@

While executing the file, I did:

./File.sh < password.txt

But I was asked to enter password anyway.

How do I get the password to be input from the file?

marked as duplicate by Gilles ssh Sep 22 '16 at 15:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


SSH with 'password in a file' is commonly used as public key authentication. Create a key pair using ssh-keygen, upload the public key to the other host:

scp ./.ssh/id_rsa.pub user@

and place it as ~/.ssh/authorized_keys:

ssh user@
mkdir ~/.ssh
mv ~/id_rsa.pub ~/.ssh/authorized_keys

or, if an authorized_keys file already exists:

cat ~/id_rsa.pub >> ~/.ssh/authorized_keys

set the appropriate permissions (600 for the file, 700 for the directory):

chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh

and start a new ssh session.

  • Hello, I suggested an edit that appends to authorized_keys instead of overwriting it if it already exists. – Kos Sep 22 '16 at 10:18
  • 1
    This doesn't precisely answers the question imho, he asked for a password clearly stored in a txt – capitano666 Sep 22 '16 at 15:51

You can use sshpass to do that:

sshpass -f password.txt ssh -T user@
  • Does sshpass require superuser privileges? Because it says command not found for me. – Mahathi Vempati Sep 22 '16 at 16:15
  • 1
    No. It does not. But you have to install it before trying to run it. It is not clear what your OS is. – Jakuje Sep 22 '16 at 16:21
#!/usr/bin/expect -f
spawn ssh shw@hostname
expect -exact "shw@hostname's password: "
send -- "PASSWORD\r"
expect "$ "

Expect is a tool to automate the process.

  • Do all these commands need superuser privileges? Because this code wasn't executing. – Mahathi Vempati Sep 22 '16 at 16:16

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