DEV Community

Cover image for How to use rsync to copy files from one Linux system to another Linux system?
abbazs
abbazs

Posted on • Edited on

4 2

How to use rsync to copy files from one Linux system to another Linux system?

Introduction

Transferring files between Linux systems can be efficiently handled using rsync. This tutorial demonstrates how to use rsync for copying files and directories between systems, both from local to remote and from remote to local, with or without using a certificate file.

Prerequisites

  • Both source and destination systems should have rsync and ssh installed.
  • Access credentials (username and password or SSH key) for the destination system.

Using rsync with a Certificate File

Copying a File from Local to Remote

To copy a single file from your local machine to a remote server using a certificate file:

rsync -avz -e "ssh -i ~/servers_ppk_files/pem_file.pem -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress ./file_to_be_copied.tar.gz username@remote_server_ip:~/file_to_be_copied.tar.gz
Enter fullscreen mode Exit fullscreen mode

Copying a Directory from Local to Remote

To copy an entire directory from your local machine to a remote server using a certificate file:

rsync -avz -e "ssh -i ~/servers_ppk_files/pem_file.pem -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress ./folder_to_be_copied username@remote_server_ip:~/folder_to_be_copied
Enter fullscreen mode Exit fullscreen mode

Copying a File from Remote to Local

To copy a single file from a remote server to your local machine using a certificate file:

rsync -avz -e "ssh -i ~/servers_ppk_files/pem_file.pem -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress username@remote_server_ip:~/file_to_be_copied.tar.gz ./file_to_be_copied.tar.gz
Enter fullscreen mode Exit fullscreen mode

Copying a Directory from Remote to Local

To copy an entire directory from a remote server to your local machine using a certificate file:

rsync -avz -e "ssh -i ~/servers_ppk_files/pem_file.pem -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress username@remote_server_ip:~/folder_to_be_copied ./folder_to_be_copied
Enter fullscreen mode Exit fullscreen mode

Using rsync Without a Certificate File

Copying a File from Local to Remote

To copy a single file from your local machine to a remote server without using a certificate file:

rsync -avz --progress ./file_to_be_copied.tar.gz username@remote_server_ip:~/file_to_be_copied.tar.gz
Enter fullscreen mode Exit fullscreen mode

Copying a Directory from Local to Remote

To copy an entire directory from your local machine to a remote server without using a certificate file:

rsync -avz --progress ./folder_to_be_copied username@remote_server_ip:~/folder_to_be_copied
Enter fullscreen mode Exit fullscreen mode

Copying a File from Remote to Local

To copy a single file from a remote server to your local machine without using a certificate file:

rsync -avz --progress username@remote_server_ip:~/file_to_be_copied.tar.gz ./file_to_be_copied.tar.gz
Enter fullscreen mode Exit fullscreen mode

Copying a Directory from Remote to Local

To copy an entire directory from a remote server to your local machine without using a certificate file:

rsync -avz --progress username@remote_server_ip:~/folder_to_be_copied ./folder_to_be_copied
Enter fullscreen mode Exit fullscreen mode

Dealing with Older Servers

Older servers may not support the latest SSH algorithms, which can cause issues when attempting to connect. To resolve this, you can specify the algorithms manually using the following options:

  • -o HostKeyAlgorithms=+ssh-rsa: This option adds the ssh-rsa algorithm to the list of allowed host key algorithms. Host keys are used by SSH to authenticate the server to the client. If the server only supports older algorithms, specifying this option ensures compatibility.
  • -o PubkeyAcceptedKeyTypes=+ssh-rsa: This option adds the ssh-rsa algorithm to the list of accepted public key types for authentication. It ensures that the client can use RSA keys for authenticating to the server.

Copying a File from Local to Remote

To copy a single file from your local machine to an older remote server:

rsync -avz -e "ssh -o HostKeyAlgorithms=+ssh-rsa -o PubkeyAcceptedKeyTypes=+ssh-rsa" --progress ./file_to_be_copied.tar.gz username@remote_server_ip:~/file_to_be_copied.tar.gz
Enter fullscreen mode Exit fullscreen mode

Copying a Directory from Local to Remote

To copy an entire directory from your local machine to an older remote server:

rsync -avz -e "ssh -o HostKeyAlgorithms=+ssh-rsa -o PubkeyAcceptedKeyTypes=+ssh-rsa" --progress ./folder_to_be_copied username@remote_server_ip:~/folder_to_be_copied
Enter fullscreen mode Exit fullscreen mode

Copying a File from Remote to Local

To copy a single file from an older remote server to your local machine:

rsync -avz -e "ssh -o HostKeyAlgorithms=+ssh-rsa -o PubkeyAcceptedKeyTypes=+ssh-rsa" --progress username@remote_server_ip:~/file_to_be_copied.tar.gz ./file_to_be_copied.tar.gz
Enter fullscreen mode Exit fullscreen mode

Copying a Directory from Remote to Local

To copy an entire directory from an older remote server to your local machine:

rsync -avz -e "ssh -o HostKeyAlgorithms=+ssh-rsa -o PubkeyAcceptedKeyTypes=+ssh-rsa" --progress username@remote_server_ip:~/folder_to_be_copied ./folder_to_be_copied
Enter fullscreen mode Exit fullscreen mode

Options details

  • -a: Archive mode; equals -rlptgoD (no -H,-A,-X)
  • -v: Verbose mode; increases the level of detail in the output
  • -z: Compress file data during the transfer
  • -e: Specify the remote shell to use
  • --progress: Show progress during the transfer
  • ssh -i ~/servers_ppk_files/pem_file.pem: Use the specified SSH private key for authentication
  • -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null: Bypass SSH host key checking
  • -o HostKeyAlgorithms=+ssh-rsa -o PubkeyAcceptedKeyTypes=+ssh-rsa: Specify SSH algorithms for older servers to ensure compatibility

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay