sending files between linux computers

Sending files between Linux computers

In order to send files between computers, the easiest and safest way I have found so far is using rsync. It is good for this task because, as long as SSH keys are set up, files are transferred securely over SSH. In this setup, assume that you are on the sender computer and that the remote machine is the receiver.

make sure ssh works first

Before thinking about rsync, make sure the sender can SSH into the receiver. Test that with:

ssh cjm@1.2.3.4

Replace cjm with the username on the receiver, and replace 1.2.3.4 with the receiver's IP address or hostname.

If this is the first time this pair of computers has communicated, the command may not work yet. The receiver needs to know that it should trust the sender. The normal way to do this is to put the sender's public SSH key into the receiver's authorized_keys file.

create a key on the sender

On the sender computer, generate an SSH key if one does not already exist:

ssh-keygen

Press Enter to accept the default file location. Use a passphrase if desired, or press Enter twice for no passphrase.

Print the sender's public key:

cat ~/.ssh/id_ed25519.pub

register the sender on the receiver

The easiest way to install the sender's public key on the receiver is:

ssh-copy-id cjm@1.2.3.4

If ssh-copy-id is not available, manually add the sender's public key as a new line in this file on the receiver:

/home/cjm/.ssh/authorized_keys

After that, test SSH again:

ssh cjm@1.2.3.4

If this works, rsync has the connection path it needs.

install rsync

rsync should be installed on both the sender and the receiver.

sudo apt update
sudo apt install rsync

send files to the receiver

Copy the contents of a local folder on the sender to a folder on the receiver:

rsync -avz ./local-folder/ cjm@1.2.3.4:/home/cjm/remote-folder/

A common real shape is:

rsync -avz ./site/ cjm@1.2.3.4:/home/cjm/site/

copy files back from the receiver

Reverse the source and destination to pull files from the receiver back to the sender:

rsync -avz cjm@1.2.3.4:/home/cjm/remote-folder/ ./local-folder/

trailing slash

The trailing slash changes what gets copied.

rsync -avz folder/ cjm@1.2.3.4:/home/cjm/dest/

This copies the contents of folder into dest.

rsync -avz folder cjm@1.2.3.4:/home/cjm/dest/

This copies the folder directory itself into dest.

useful options

# archive mode, verbose, compressed
rsync -avz source/ dest/

# show what would happen without copying
rsync -avzn source/ dest/

# delete destination files that no longer exist in the source
rsync -avz --delete source/ dest/

Be careful with --delete. It is useful for mirroring directories, but it can remove files on the receiver.

custom ssh port

If the receiver's SSH server uses a non-default port, pass an SSH command with -e:

rsync -avz -e "ssh -p 2222" ./local-folder/ cjm@1.2.3.4:/home/cjm/remote-folder/

network access

The receiver must allow SSH connections. Usually that means port 22/tcp is open in the local firewall and in any VPS or cloud-provider firewall rules.

sudo ufw allow OpenSSH
sudo ufw status verbose

If ssh cjm@1.2.3.4 works from the sender, then rsync should work too.


edit this page