Pscp – Transfer/Copy Files to Multiple Linux Servers Using Single Shell

Pscp utility allows you to transfer/copy files to multiple remote Linux servers using single terminal with one single command, this tool is a part of Pssh (Parallel SSH Tools), which provides parallel versions of OpenSSH and other similar tools such as:

  1. pscp – is utility for copying files in parallel to a number of hosts.
  2. prsync – is a utility for efficiently copying files to multiple hosts in parallel.
  3. pnuke – it helps to kills processes on multiple remote hosts in parallel.
  4. pslurp – it helps to copy files from multiple remote hosts to a central host in parallel.

When working in a network environment where there are multiple hosts on the network, a System Administrator may find these tools listed above very useful.

Copy Files and Directories to Multiple Linux Servers

Pscp – Copy Files to Multiple Linux Servers

In this article, we shall look at some useful examples of Pscp utility to transfer/copy files to multiple Linux hosts on a network.

To use the pscp tool, you need to install the PSSH utility on your Linux system, for installation of PSSH you can read this article.

  1. How to Install Pssh Tool to Execute Commands on Multiple Linux Servers

Almost all the different options used with these tools are the same except for few that are related to the specific functionality of a given utility.

How to Use Pscp to Transfer/Copy Files to Multiple Linux Servers

While using pscp you need to create a separate file that includes the number of Linux server IP address and SSH port number that you need to connect to the server.

Copy Files to Multiple Linux Servers

Let’s create a new file called “myscphosts.txt” and add the list of Linux hosts IP address and SSH port (default 22) number as shown.

192.168.0.3:22
192.168.0.9:22

Once you’ve added hosts to the file, it’s time to copy files from local machine to multiple Linux hosts under /tmp directory with the help of following command.

# pscp -h myscphosts.txt -l tecmint -Av wine-1.7.55.tar.bz2 /tmp/
OR
# pscp.pssh -h myscphosts.txt -l tecmint -Av wine-1.7.55.tar.bz2 /tmp/
Sample Output
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password: 
[1] 17:48:25 [SUCCESS] 192.168.0.3:22
[2] 17:48:35 [SUCCESS] 192.168.0.9:22

Explanation about the options used in the above command.

  1. -h switch used to read a hosts from a given file and location.
  2. -l switch reads a default username on all hosts that do not define a specific user.
  3. -A switch tells pscp ask for a password and send to ssh.
  4. -v switch is used to run pscp in verbose mode.

Copy Directories to Multiple Linux Servers

If you want to copy entire directory use -r option, which will recursively copy entire directories as shown.

# pscp -h myscphosts.txt -l tecmint -Av -r Android\ Games/ /tmp/
OR
# pscp.pssh -h myscphosts.txt -l tecmint -Av -r Android\ Games/ /tmp/
Sample Output
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password: 
[1] 17:48:25 [SUCCESS] 192.168.0.3:22
[2] 17:48:35 [SUCCESS] 192.168.0.9:22

You can view the manual entry page for the pscp or use pscp --help command to seek for help.

Conclusion

This tool is worth trying as if you control multiple Linux systems and already have SSH key-based passwordless login setup.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Ravi Saive

I am Ravi Saive, creator of TecMint. A Computer Geek and Linux Guru who loves to share tricks and tips on Internet. Most Of My Servers runs on Open Source Platform called Linux. Follow Me: Twitter, Facebook and Google+

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

12 Responses

  1. Ashwini R says:

    It didn’t work for me as well. I can get into the machine through same ip and port as I’ve inserted into hosts.txt file. Still i get the below messages:

    [[email protected] ~]# pscp -h myscphosts.txt root -Av LoadKafkaRN.jar /home/
    [1] 13:37:42 [FAILURE] 173.37.29.85:22 Exited with error code 1
    [2] 13:37:42 [FAILURE] 173.37.29.2:22 Exited with error code 1
    [3] 13:37:42 [FAILURE] 173.37.28.176:22 Exited with error code 1
    [4] 13:37:42 [FAILURE] 173.37.28.121:22 Exited with error code 1
    
  2. Sai Sudheer says:

    Hi,

    I have a shell script which will split the input dump to multiple files, and I want to know logic to transfer these files to multiple servers equally, as per above command source files will be transferred to all multiple servers.

    But my requirement is to avoid duplication, and copy files to destination servers equally. for exp: 16 input files to 4 servers– so that each server will get 4 files.

    my input dump changes time to time so that local files may become any numbers. Please help for solution.

  3. Ankit Tiwari says:

    Hi,
    I am following this tutorial to copy a file to multiple system but its giving error. The code i am using is
    pscp -h myhost.txt -l zabbix -Av show-image-1920×1080.jpg /home/zabbix/
    but it gives error
    [1] 11:18:50 [FAILURE] 192.168.0.244:22 Exited with error code 1

    • Ravi Saive says:

      @Ankit,

      Have you placed correct remote SSH host IP address and port number in the myscphosts.txt file? please confirm and add correct values and then try again..

  4. jHz says:

    Hi,

    I am trying to copy one file from 30 hosts to one central computer by following you article but no success.
    I am using pscp command for this purpose:

    pscp -h hosts.txt /camera/1.jpg /camera/1.jpg

    where camera directory has been created already in which 1.jpg exists. It always give me error:

    Exited with error code 1

    I have also tried pscp command to copy file from one host to server:

    pscp -H “192.168.0.101” /camera/1.jpg /camera/1.jpg

    but it also returned me with the same error.

    Any help will be much appreciated.
    Thanks in advance.

    • Ravi Saive says:

      @jHz,

      Have you created “myscphosts.txt” file on local machine with list of remote Linux server IP addresses and their respective SSH ports? and the correct command to copy the file from local to remote is:

      # pscp -h myscphosts.txt -l tecmint -Av wine-1.7.55.tar.bz2 /tmp/
      OR
      # pscp.pssh -h myscphosts.txt -l tecmint -Av wine-1.7.55.tar.bz2 /tmp/
      

      explanation about the options used in the above command.

      -h switch used to read a hosts from a given file and location.
      -l switch reads a default username on all hosts that do not define a specific user.
      -A switch tells pscp ask for a password and send to ssh.
      -v switch is used to run pscp in verbose mode.

      • jHz says:

        Yes I have created hosts ip file in which I saved all ips with port numbers. I am using the same command and syntax but it returns me with error: Exited with error code 1.

        • Ravi Saive says:

          @jHz,

          You were using wrong -H switch and also you didn’t mentioned about -l option used to specify username to connect to the server, I suggest you to run the commands which I given in the comments…and see.

  5. anil says:

    Above article is not working in ubuntu 12.04

    • Ravi Saive says:

      @Anil,

      Sorry for trouble, could you please tell us why it’s not working? did you get any error while installing? Please share with us your problem, so that we can help you out..

  6. Jalal Hajigholamali says:

    Hi,
    Thanks a lot
    Very nice and useful article
    Thanks again

Leave a Reply to Jalal Hajigholamali Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.