How-To: copy files over the network and preserve file permissions and informations with ssh and rsync

1 minute read

When copying files over the network, the files informations can be modified.

When using cp, one can avoid this issue by using the -a which will do the copy in archive mode, meaning that it will keep the links, preserve mode, ownership and timestamps and the copy is recursive.

the solution to this over the network is rsync alongside with ssh.

With this method, we are going to use rsync over ssh. The transfert channel will be handled by ssh, while the file copy is handled by rsync.

As such, the client (the machine you are copying the files to) need to have a ssh client installed as well as the rsync client.

The server, need to run an ssh server.

Now suppose I want to copy the directory ~/directory from user foo on machine bar to my local machine and in the current directory, I will test it with:

$ rsync -avzn foo@bar:~/directory .

This will only output the action rsync would take without actually taking it. If you are satisfied with this, you can apply the actions by removing the -n option:

$ rsync -avz foo@bar:~/directory .