[TOP TIP] How to transfer files/directories between servers

Avatar forum:IfThenElse 6 months, 1 week ago

There are tons of ways to transfer files and directories between two systems. Here are two typical methods to help you achieve your life's goals…

METHOD 1 - direct server to server

Lets pretend that we have two systems, serverA and serverB. We want to transfer the /home/user directory from serverA to serverB. This can be achieved by generating a temporary ssh key on serverB and by using the generated public key in serverA.

(on serverB)

first we generate a temporary ssh key, the following command will generate a new key under ~/.ssh named tempkey (tempkey, of type ed25519.

ssh-keygen ssh-keygen -t ed25519 -f ~/.ssh/tempkey

(on serverA)

next, we need to send the generated public key to serverA, this can be done either via plain copy/paste or by using ssh-copy-id. In any case, the idea is to copy our serverB ~/.ssh/ to the serverA ~/.ssh/authorized_keys. If this is done for the root user, then that will give serverB full access to serverA.

~/.ssh/ copy/paste to ~/.ssh/authorized_keys

(on serverB)

now that our serverB has access to serverA, we may issue our copy command, for example:

by using scp

scp -C -i ~/.ssh/tempkey -p -r user@serverA /home/user

or by using rsync

rsync -e "ssh" --safe-links -tarxlzhP user@serverA /home/user

METHOD 2 - indirect server to server via a local system

Lets pretend that we have three systems, a desktop, serverA and serverB. We want to transfer the /home/user directory from serverA to serverB but without allowing the two servers to interact directly. This can be achieved by using sshfs on the desktop, which is provided by the fuse-sshfs rpm package.

The flow of data looks like this: serverA (sshfs)-> desktop (rsync over ssh)-> serverB

In essence, we mount serverA to a local directory and use rsync to copy files/directories to serverB, so here is a quick and dirty little script that does that for you:


# rsync 2 remove hosts

if [ $# -ne 2 ]; then
        echo 1>&2 Usage: rsync2 [user@]host:[dir] [user@]host:[dir]
        exit 127

if [ ! -f /usr/bin/sshfs ]; then
        echo "sshfs: command not found."
        echo "The required 'fuse-sshfs' package is not installed."
        exit 127

# remove stale tmp directory
rm -rf /tmp/sshfstmp 2>/dev/null

# create temporary directory
mkdir /tmp/sshfstmp
chmod go-wrx /tmp/sshfstmp

# mount sshfs
sshfs "$1" /tmp/sshfstmp

# rsync
rsync --safe-links -tarxlzhP /tmp/sshfstmp/ "$2"

# unmount
fusermount -u /tmp/sshfstmp

# remove tmp directory
rm -rf /tmp/sshfstmp 2>/dev/null

Save it as ~/bin/rsync2 and use it like this:

rsync2 user@serverA:/home/user user@serverB:/home/user

Be warned that this method is considerably slower but has its uses when you don't want the two servers to access one another.

0 Replies


Please enter an answer

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct