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.

If you read this far, tweet to the author to show them you care. Tweet a thanks
Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

15 thoughts on “Pscp – Transfer/Copy Files to Multiple Linux Servers Using Single Shell”

  1. I believe if you have never logged into the target server from the source server before and it can’t establish authenticity of host and needs your yes/no for ECDSA fingerprint then the command will fail with error code 1.

    Reply
  2. Can we use pssh.pscp command to copy files from multiple servers in the list to one local server from where the pscp.pssh command is fired?

    This commands works perfectly

    # pscp.pssh -h myscphosts.txt -l tecmint -Av wine-1.7.55.tar.bz2 /tmp/
    
    Reply
  3. Hi bro your given command is perfect.

    I go through some comments. Some have an error “Exited with error code 1”.

    This is because they haven’t logged in that particular server from the system they have fired your command. Ask them to log in to the servers using “ssh”. This is one-time activity.

    After that, they will able to use your command and get the “SUCCESS” output.

    In the future they will use your command directly.

    Reply
  4. 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:

    [root@node1 ~]# 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
    
    Reply
  5. 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.

    Reply
  6. 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

    Reply
    • @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..

      Reply
  7. 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.

    Reply
    • @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.

      Reply
      • 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.

        Reply
        • @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.

          Reply
    • @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..

      Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.