How to securely transfer files between Linux servers over the network

In this tutorial we will cover the steps needed to transfer files with rsync over SSH in CentOS 6.4, Debian or Ubuntu platform.

  • Requirements
  • CentOS 6.4, Debian or Ubuntu installed on your computer/server
  • SSH access (Command line access to the server)
  • Appropriate privileges
  • Basic skills for working on a Linux environment

Rsync is a great tool that allows you to copy and synchronize data between servers. The command can be used over SSH which encrypts the connection. Therefore, It is secure method to transfer the data over the network. It also provides large amount of options which can be used such as archive mode, backup mode, data compression during the transfer etc.

Install rsync

If the command is not included by default inside the server configuration we can easily add it using the default package manager:

CentOS 6.4:


Check SSH configuration

In order to make sure that you will be able to transfer files from/to the remote server using rsync over SSH you have to make sure it connecting correctly:

I assume you did it well.

Transfer data using rsync

Copy a file from local server to remote one:

In the above example we will copy a whole directory with its files called my_backup_web_media from the local machine to the remote one and will place all inside the folder /home/remoteuser/www/mywebsite/public/media.

If the remote server is configured to work with non-default SSH port (other than 22) we can specify that inside the -e option: ssh -p 2222

Copy a file from remote server into a local folder:

In the above example we will copy a directory called /media from the remote server inside a local folder called /my_backup_web_media.

Synchronize local folder on remote server:

Use of “/” at the end of path:

If “/” is placed at the end of the source folder, rsync will copy one the content of the folder.
When not using “/” at the end of source folder, rsync will copy the folder itself and the content of the folder.

If “/” is placed at the end of the destination folder, rsync will paste the data directly inside the folder.
When not using “/” at the end of destination folder, rsync will create a folder with that name and paste the data inside that folder.

common rsync options:

  • –delete – delete files that don’t exist on sender (system)
  • -v – verbose (-vv will provide more detailed information)
  • -e “ssh options” – specify the ssh as remote shell
  • -a – archive mode – it preserves permissions (owners, groups), times, symbolic links, and devices
  • -r – recurse into directories
  • -z – compress file data during transfer
  • –exclude ‘foldername’ – excludes the corresponding folder from transfer
  • -P – show progress during transfer

Hoàng Tựa

Yêu thích lập trình web và tạo ra những thứ đẹp đẽ.

Leave a Reply

Your email address will not be published. Required fields are marked *