This is rsync, the swiss-army knife of Linux file copying. It’s basically used if you want to download directories from one location to another, for example from your local prod server to a test server, or from idea in either case is the same – if you’re copying files and folders over to a remote server using rsync where you’re using key-based authentication, you need to input your public SSH key within that rsync command, similarly if you want to download remote folders from a remote server to your local prod server. Done effectively, the instructions are below.
1. Setup Public SSH Keys
First, create a password-less public SSH key on your local client system:
$ ssh-keygen -t rsa -C “your_email@example.com”
Generated public/private rsa key pair.
Enter file in which to save the key (/Users/huangjinsong/.ssh/id_rsa):
[press ENTER] Enter passphrase (empty for no passphrase): [type a passphrase]
Enter same passphrase again: [type passphrase again]
Your identification has been saved in /Users/huangjinsong/.ssh/id_rsa.
Your public key has been saved in /Users/huangjinsong/.ssh/id_rsa.pub.
The text that follows is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
ssh-keygen -f ~/.ssh/id_rsa -q -P ""
This command generates the SSH key pair without a passphrase, and then displays the public key:
cat ~/.ssh/id_rsa.pub
Your password has successfully been remote shelled To the target, it should look something like this: Copy the contents of the private key file. Now, log back into your remote server and add the cut-and-pasted SSH key to the file ~/.ssh/authorized_keys. If ~/.ssh doesn’t exist on the remote server, manually create it:
mkdir ~/.ssh
chmod 0700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 0644 ~/.ssh/authorized_keys
Now, your client system can authenticate with the remote server using the public key placed in the file called authorised_keys by the remote user.
2. Sync Files Using Rsync
Now that you have an SSH key in place, you can use rsync to mirror files between the two systems. Here is the command to sync a file from a source system to a destination system.
rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress /home/ubuntu/data.txt username@44.43.32.21:/home/ubuntu
Replace your username with your remote username, and adjust file paths and IP addresses as required:
~$ rsync -avz /home/graywinter/public_html/book1.darkside.com 192.168.1.100:/data/books
If all is well, you can check on the destination system that the file copy has completed.
Conclusion
Hopefully this article successfully walked you through the steps to use rsync with SSH keys for file transfer over SSH.
SSH keys are great because cloud-based systems, such as Amazon Web Services, are very commonly used now to host things, and one of the most commonly used security features is key-based authentication, so this should be very helpful. With password-less public keys in hand, you can also use this from scripts or cron jobs to automate all sorts of tasks. Keeping in mind that once the public key is copied over to the remote system, you can use it bidirectionally. What this means is that you can rsync files from your local system to a remote system or from a remote system to your local system. To rsync files from a remote system to your local system, you’d have to copy the public key of the remote server onto your local system and then on your local system you would authenticate the remote system with the key.