People often ask me to teach them to use rsync. So instead of explaining to poeple over and over, I decided to make this guide which shows basic, light usage of rsync, basic options as well as some useful, more advanced things you can do with it.
First of all, what is rsync?
As its name suggests, rsync is a comand-line file sharing/syncing program. It runs on GNU+Linux, MacOS and *BSD. It is, dare I say, the most powerful yet the most simple file sharing program there is.
PrerequisitesFirst of all, you need
rsyncpackages installed on both systems in question. I will assume that you're a grown adult and know how to install packages on your computer. You also need to start
sshwith your init system - so on systemd that would be
systemctl start ssh.
Sending your first file
We will assume that you want to send a file from a system called thinkpad to a system called optiplex. Your user on thinkpad is joe and you want to send to user bill on optiplex on local network. A file you want to send is "helloworld.c" and it is located in your home directory. The local IP adress of thinkpad is 220.127.116.11 and IP adress of optiplex is 18.104.22.168An example command for a scenario like this would be:
rsync -v ~/helloworld.c firstname.lastname@example.org:~/
Tip: you can universally check what local ip adress you have on each system by executing
Now, writing IP adresses and users manually every time would get boring pretty soon. Let's configure SSH so that rsync knows what to default to. On thinkpad, create a file
mkdir -p /home/joe/.ssh/ touch /home/joe/.ssh/configNow, in this file we will put the basic configuration for optiplex. Rsync will automatically default to these settings.
/home/joe/.ssh/configNow we will demonstrate the same command from above, but now simplified using this config.
host optiplex HostName 22.214.171.124 #destination IP port 22 #SSH port. Default is always 22. User bill
rsync -v ~/helloworld.c optiplex:~/
Yup, that's it. Notice how you don't need to specify the user now at all, because it defaults to bill now that we set it in the config. If you wanted to send to different user, simply specify the user as you did in the first command.
Tip: If you don't have user specified in config file and you will not specify any user in the command, it will assume that the recipient's user is the same as yours. In our case it would default to joe, which is incorrect, since the recipient's user is bill.
Answering Noob questions
But jaocb... What is the "~/" in the command?
In Unix shell, the tidle symbol (~) symbolises the home directory. It is relative to each user. So if I was logged as bill, ~ would symbolise /home/bill, if you were logged as jake, it would symbolise /home/jake. Alternative to tilde would be the
$HOME global variable, but I think ~ just looks nicer.
Syncing with rsync
Let's assume we are in a similar scenario as in the first one, with the config file already set. Except, now we want to sync a website from the thinkpad to the optiplex. This could have multiple reasons, perhaps we were editing our personal website on the thinkpad, but we use the optiplex as a way to server the website to the internet. Whatever the reason is, it should work the same.
The root of your website on your thinkpad is located in
~/.local/usr/site, and on optiplex it is located in
/var/www/main An example command would look something like this, with individual options explained:
rsync --recursive --verbose --update --delete-after --progress ~/.local/usr/site/* optiplex:/var/www/main#--recursive (short -r) - basically sync directories and all its contents
#--verbose (short -v) - be more noisy and have more verbose output
#--update (short -u) - Updates files in the recieving directory if there are new ones. Useful for syncing.
#--delete-after - removes no-longer-existent files in the recipient's destination
#--progress - have a nice progress bar so you know how much data has been sent
You can put this command as an alias to your bashrc, or zshrc, or whatever shell you're using if you are going to use it frequently.
Sending without passwordSince rsync basically uses SSH for sending files, you can just do key authentification instead of password authentification. This is a much more secure way to connect to a server anyway, and I advise everyone to do it.
On thinkpad do
Leave it at the default location, and enter no passphrase. A file
~/.ssh/id_rsa will be created.
Now you can import this key into the server you want to send to, in this case optiplex.
The keys should be now sent. Now on the system optiplex, this is optional, but I highly advise to disable password authentification if you're using ssh and rsync over the internet. It is highly dangerous to leave SSH open to the internet with default port and with a easy to guess password.
If you now try to ssh or rsync to optiplex, you should be able to do it without a password.
I think this would be enough for a simple demonstration of the program. You can always find out more in