6.3.4 How to copy files from server to server using SSH

Main banner

Moving files between servers is an integral part of the work of system administrators and developers. Let’s take a closer look at three methods for copying files between servers using SSH protocols. Let’s look at the examples of the SCP, Midnight Commander (MC) and Rsync utilities.

​ To get started, you need to connect to the server using the SSH protocol. ​

Using the SCP utility

SCP (Secure Copy Protocol) is a protocol and command utility for securely copying files between computers via SSH (Secure Shell). It provides secure data transmission and encryption during transmission. Using the utility, you can copy both from a remote server and to the remote server itself.

Example: copying information from a local machine to a remote server

To copy information from the local machine to a remote server, use a command like:

Command syntax:
  1. scp /path/to/local/file user@remote_server:/path/on/remote/server/

Example of a ready-made command:
  1. scp /var/www/user_name/data user@s1.thehost.com.ua:/var/www/user_name/data/
  • /var/www/user_name/data: path to the local and remote server directory
  • user: user who should have ssh access
  • s1.thehost.com.ua: address/ip of the remote server, in your case the address will be different.
Example: copying information from a remote server to a local computer

To copy information from a remote server to a local computer, use a command like:

Command syntax:
  1. scp user@remote_server:/path/on/remote/server/local_file /path/on/local/machine/
Example of a ready-made command:
  1. scp user@s1.thehost.com.ua:/var/www/user_name/data /var/www/user_name/data/

Both of these examples use the SCP utility to securely copy files over SSH. It is important to ensure that you have the correct permissions and access to files and directories on both sides of the operation. ​

Using the MC utility

Midnight Commander (MC) is a text file manager for the command line on Unix-like operating systems. It provides a convenient interface for navigating the file system, copying, moving, deleting files, and also supports archiving and working with remote servers via the FTP and SFTP protocols. Midnight Commander is an effective tool for managing text files. ​ The utility is not always preinstalled. You can always install the utility using the command:

For Ubuntu/Debian
  1. apt-get install mc
For Centos/AlmaLinux/RockyLinux:
  1. yum install mc

Example: using Midnight Commander

1. Open terminal.

2. Launch Midnight Commander by entering the command:

user@hosting.server:
  1. mc

3. In the Right section, select Shell link. MC interface4. In the window that appears, enter the address of the remote server in the following format:

  1. sh://user@remote_server/path/on/remote/server/
  • user: user of the remote server
  • remote_server: address/ip of the remote server
  • /path/on/remote/server/: path to the desired directory

5. When connecting for the first time, you need to confirm the security key by entering yes in the console.

  1. Are you sure you want to continue connecting (yes/no)?

6. Now you have access to the files and directories of the remote server in the right panel.

7. Use standard MC commands such as F5 (copy), F6 (move), F8 (delete) to manage files and directories on the remote server.

8. To exit MC, press F10 or Esc and then 0 (zero).

Using the Rsync utility

Rsync (Remote Sync) is a utility for synchronizing files and directories between local and remote systems. It ensures efficient data transfer by sending only modified parts of files and minimizing the amount of information transferred. Rsync is often used to backup, update remote files, and synchronize data between computers over a network. The utility provides flexible configuration options and is a powerful tool for working effectively with data. ​ To use the utility, it must be installed on both servers. If it is not installed on some device, you can install it with the command:

For Ubuntu/Debian
  1. apt-get install rsync
For Centos/AlmaLinux/RockyLinux:
  1. yum install rsync
Example: copying from local server to remote server
Command syntax:
  1. rsync -avr -e ssh --progress /path/to/local/directory/ user@remote_server:/path/on/remote/server/
Example of a ready-made command:
  1. rsync -avr -e ssh --progress /var/www/user_name/data/ user@s1.thehost.com.ua:/var/www/user_name/data/

The command may include additional keys when forming the command: ​

  • -a, --archive: archive mode, enables recursive copying, saving access rights, timestamps and other file attributes.
  • -v, --verbose: verbose output, displays information about the copying process.
  • -z, --compress: compress data during transmission.
  • -h, --human-readable: Human-readable output format.
  • -e, --rsh: specify an alternative command for transferring data (for example, SSH).
  • -u, --update: copy only newer files.
  • -r, --recursive: recursively copy directories.
  • -n, --dry-run: A “dry run” operation that does not make any real changes.
  • --progress: display information about the progress of data transfer.
  • --bwlimit=1000: the switch limits the data transfer rate to 1000 kilobytes per second. Can be configured if you need to limit the transfer speed.
Example: copying from a remote server to a local computer
Command syntax:
  1. rsync -avr -e ssh --progress user@remote_server:/path/on/remote/server/local_directory/
Example of a ready-made command:
  1. rsync -avr -e ssh --progress user@s1.thehost.com.ua:/var/www/user_name/data/ var/www/user_name/data/

These examples will help you master using commands to copy files between servers. Choose a method depending on your preferences and the requirements of your project.