Scenario: Servers: A, B and C (All linux)


Now i need to send the file from Server A to B&C using SCP but i have to enter the password and File name only once.

Need to know how to achieve it.

Tried sshpass but seems its not installed and i cannot install it :(

sshpass:command not found

Used code:(Here need to enter password each time)


echo "Enter Package Name:\c"

read package



scp ${package1} User@ServerB:/tmp/

scp ${package1} User@ServerC:/tmp/
  • You could keep a directory on B&C constantly in sync using cron, then send the file to either B or C. – bitofagoob Apr 22 '17 at 9:27
  • Can you please suggest how to keep in sync using cron? – Arun kumar Apr 22 '17 at 9:35
  • 1
    Do you have to use scp? Do you have to type a password? There are many ways to do this, one of the better ways is without a password and using sftp. – Tigger Apr 22 '17 at 12:08
  • In answer to your question about how to keep in sync using cron: you could set up an nfs share on C and have it accessible on B. Then just use cron to periodically rsync the two directories. – bitofagoob Apr 22 '17 at 15:17

You can remove the "password" request like so. On the PC with the file (Server A), in a terminal type:

ssh-keygen -t rsa -b 2048
Enter file in which to save the key (/<user>/.ssh/id_rsa): [Hit Enter]
Enter passphrase (empty for no passphrase): [Hit Enter]
Enter same passphrase again: [Hit Enter]
Your identification has been saved in /<user>/.ssh/id_rsa.
Your public key has been saved in /<user>/.ssh/id_rsa.pub.

Then you need to copy /<user>/.ssh/id_rsa.pub to Server B and C. On Server B and C, do the following. NOTE: The mkdir -p ~/.ssh may not be needed.

mkdir -p ~/.ssh
cat id_rsa.pub >> ~/.ssh/authorized_keys
rm id_rsa.pub

Now you can connect from Server A to B and C without a password. For example, your script would now be as simple as (with a basic file check):


ask () {
    echo -n "Enter Package Name: "
    read package

    if [ ! -f "$package" ]
        echo "
${package} is not a file. Try again.
        upload "$package"

upload () {
    echo "put \"$1\" \"/tmp/$1\" > sftp.cmd
    sftp User@serverB < sftp.cmd
    sftp User@serverC < sftp.cmd
    rm -f sftp.cmd  

  • Thanks a lot for suggestion.....My bad we don't have access to generate keygen :(...... Is there any other way to pass password as prompt only once so that no need to enter again and again for each Server... – Arun kumar Apr 25 '17 at 3:25
  • Very surprised that ssh-keygen is not on Server A. This does not seem correct to me at all. Do you have a web server running PHP on the Server B & C? If so, you can always post the data to the server (over SSL / HTTPS) - again, no password needed. Would require curl, a web server on Server A or some other software to post the data to B & C. – Tigger Apr 25 '17 at 5:00
  • Actually..we dont have access to check that...let me go with scp script by entering password each time...thnx a lot for all your time and responses.... – Arun kumar Apr 26 '17 at 23:35

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.