I need to copy a directory to different servers, which are listed in a text file (hostfile).

I haven't found any way to do something like this with scp

scp -r --hostfile ~/directory to_all@servers:~/

I know how to do this for one server: scp -r directory one@server:~/, but not for many. The number of servers varies but I use the same user name for all of them.

  • 1
    Which part are you having trouble with? Copying a directory to another server, or applying this to a group of servers? Which part of each specifically? Have you tried anything? – Mark Smith Mar 29 '16 at 8:23
  • I have tried many many things but nothing to fit with scp :-/ – aurelien Mar 29 '16 at 8:30
  • Please edit your question and indicate what you've tried. And also please answer my previous questions. – Mark Smith Mar 29 '16 at 8:33
  • do not put -1 that is an interesting situation and no response seen on the web about that (no response that works) – aurelien Mar 29 '16 at 8:36
  • 1
    There's no need to restate the question in the comments. And the OP is seldom the right person to determine if a question is interesting. And there's not so much wrong with the question as there is with what you have tried, and how you react. – Henrik Mar 29 '16 at 8:40

The simplest way is with a shell for loop. For example:

for s in server1 server2 server3 ... serverN ; do 
    scp -r directory/ $s:~/

A minor variation is to list the servers in a file (e.g. server-list.txt), one per line, and do something like this:

for s in $(cat servers.txt) ; do 
    scp -r directory/ $s:~/

Yet another minor variation supports # comments (including commenting-out server hostnames) in the servers.txt file:

for s in $(sed -e 's/#.*//' servers.txt) ; do 
    scp -r directory/ $s:~/

There are countless other ways that the list of servers can be obtained or generated - e.g. run a mysql or psql query to extract the list from a database, fetch a text/plain file from a web server, fetch a json file from a web server and parse it with jq or python -mjson.tool, and many more. The basic method remains the same.

If you need to do this frequently with different files/directories and/or need to run shell commands in parallel on multiple servers at the same time, I'd strongly recommend installing the pdsh package and making use of the pdcp (parallel distributed copy) and pdsh (parallel distributed shell) commands. pdsh is packaged for most distros.

Search here for pdsh - there are several questions and answers that mention it.

while read server; do scp -r ~/directory to_all@$server: ; done < hostfile

assuming your remote user is really called to_all, and assuming you already have ssh keys set up (otherwise it would ask for a password for each of the servers).

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.