Transfer files to a Linux instance with scp, sftp, or rsync

更新时间:
复制 MD 格式

Choose scp, sftp, or rsync to transfer files between a local Linux or macOS system and an ECS Linux instance over SSH.

Choose a transfer tool

Select the tool that best fits your scenario.

  • scp: Quick, single-file transfers. Simple syntax.

  • sftp: FTP-like interactive session for browsing, deleting, and renaming files.

  • rsync: Directory synchronization, backups, or many small files. Incremental algorithm transfers only differences.

  • Unstable Networks: Use resumable transfer in rsync or sftp for large files.

Detailed technical comparison:

Detailed comparison of scp, sftp, and rsync

Feature

scp

sftp

rsync

Performance

Large files: Performs well.

Large number of small files: Inefficient, as each file can incur new connection overhead.

Large files: Performs well and supports pipelining.

Large number of small files: Better than scp, but not as efficient as rsync.

High overall efficiency. For subsequent syncs, it only transfers file differences, saving bandwidth and time.

Resumable Transfer

Not supported. Transfers must start from the beginning if interrupted.

Resumes single-file transfers with the reget (download) and reput (upload) commands.

Supported. The --partial or --append-verify options let you resume transfers for both files and directories.

Core Advantage

Simple syntax. Included by default in most systems, making it ideal for quick, one-off file transfers.

Offers an interactive session for managing files on the remote server with commands such as ls, rm, and mkdir.

Incremental synchronization. Supports advanced options such as filtering, deleting, speed limiting, and compression.

Resource Consumption

Low CPU and memory consumption.

An interactive session maintains a persistent SSH connection, resulting in medium resource consumption.

Consumes a certain amount of CPU and memory when comparing file differences, especially when scanning a large number of files.

Procedure

Before you begin

Complete these checks before transferring files.

  1. Retrieve the instance public IP address

    The instance must have public bandwidth enabled. Record the public IP address from the Instance list page in the ECS console.

  2. Configure security group rules

    These tools use SSH (port 22 by default). Ensure the security group allows inbound TCP traffic on this port from your local IP address.

    • Action: Allow

    • Protocol Type: Custom TCP

    • Port Range: 22/22 (or your custom SSH port)

    • Source: Set to your local public IP address for security.

      Get your IP by running curl ifconfig.me or curl ip.sb in your local terminal.
  3. Check the internal firewall

    The OS firewall (such as firewalld or ufw) may also block the connection.

    1. Log on to the instance and check the firewall status.

    2. If the firewall is active, ensure it allows SSH or port 22. To open a port or service, see the documentation for your OS.

Use scp to transfer files

Secure Copy Protocol (scp) copies files and directories between a local computer and a remote host.

Upload files or directories to an instance

Upload a file or folder to your instance. You will be prompted for a password.

# Upload a single file to the instance
sudo scp <local_file_path> <instance_username>@<public_ip_address>:<remote_directory_path>

# Upload a local directory to the instance
sudo scp -r <local_directory_path> <instance_username>@<public_ip_address>:<remote_directory_path>

Example:

Upload /opt/test.txt to /home/ecs-user/ on an instance with public IP 1xx.xxx.xxx.121:

sudo scp /opt/test.txt ecs-user@1xx.xxx.xxx.121:/home/ecs-user/

Download files or directories from an instance

Download a file from your instance. You will be prompted for a password.

Note:scp is inefficient for many small files. Archive them into a .tar.gz file before transferring, or use rsync.
# Download a single file to your local machine
sudo scp <instance_username>@<public_ip_address>:<remote_file_path> <local_directory_path>

# Download a directory from the instance to your local machine
sudo scp -r <instance_username>@<public_ip_address>:<remote_directory_path> <local_directory_path>

Example:

Download /home/ecs-user/test.txt from an instance with public IP 1xx.xxx.xxx.121 to the local /opt/ directory:

sudo scp ecs-user@1xx.xxx.xxx.121:/home/ecs-user/test.txt /opt/

Use sftp for interactive file transfers

SSH File Transfer Protocol (SFTP) starts an interactive session for file management on a remote server.

Connect to the instance

Establish an sftp connection. Once connected, the prompt changes to sftp>.

sudo sftp <instance_username>@<public_ip_address>

Common SFTP interactive commands

At the sftp> prompt, you can use the following commands:

  • Remote operations: ls (list remote directory), cd (change remote directory), pwd (show remote current path), mkdir (create remote directory), rm (delete remote file), rename (rename remote file).

  • Local operations: lls (list local directory), lcd (change local directory), lpwd (show local current path).

Upload files or directories to an instance

# Upload a single file
sftp> put <local_file_path> <remote_directory_path>

# Upload an entire directory
sftp> put -r <local_directory_path> <remote_directory_path>

Examples:

  • Upload /opt/test.txt to /home/ecs-user/ on the instance:

    sftp> put /opt/test.txt /home/ecs-user
  • To upload the local directory /opt/test/ to the /home/ecs-user/ directory on the instance:

    sftp> put -r /opt/test/ /home/ecs-user/

Download files or entire directories from an instance

# Download a single file
sftp> get <remote_file_path> <local_directory_path>

# Download an entire directory
sftp> get -r <remote_directory_path> <local_directory_path>

Examples:

  • To download the file /home/ecs-user/test.txt from the instance to your local /opt directory:

    sftp> get /home/ecs-user/test.txt /opt
  • To download the directory /home/ecs-user/test/ from the instance to your local /opt directory:

    sftp> get -r /home/ecs-user/test/ /opt

Resume a transfer

If a large file transfer is interrupted, you can use the reput (upload) and reget (download) commands to resume the transfer from where it left off.

# Resume an upload
sftp> reput <local_file_path> <remote_file_path>

# Resume a download
sftp> reget <remote_file_path> <local_file_path>
Note: SFTP resumable transfer is effective only for single files. If a directory transfer is interrupted, we recommend exiting SFTP and using rsync for efficient synchronization.

Disconnect the session

When you have finished your tasks, use the quit or bye command to exit the sftp session.

Use rsync for file and directory synchronization

rsync is ideal for large-scale, incremental, or repetitive transfers.

Install the tool

Ensure rsync is installed on both your local machine and the instance.

# On CentOS / Alibaba Cloud Linux
sudo yum install -y rsync

# On Debian / Ubuntu
sudo apt-get update && sudo apt-get install -y rsync

Common options

The standard usage of rsync often includes the -avz options:

  • -a (archive): Archive mode, equivalent to -rlptgoD. It synchronizes recursively and preserves all file attributes, such as permissions and timestamps.

  • -v (verbose): Displays detailed information about the transfer process.

  • -z (compress): To save bandwidth, it compresses data during transfer. However, on high-bandwidth links, CPU compression can become a bottleneck, and omitting -z might be faster.

Common options for production environments

  • --delete: Makes the destination directory an exact mirror of the source by deleting any files in the destination that do not exist in the source.

    Important

    Important: This is a high-risk operation. Always confirm your command is correct before using this flag, preferably by running with --dry-run first.

  • --exclude='PATTERN': Excludes files or directories that match the specified pattern, such as --exclude='*.log' or --exclude='node_modules/'.

  • --bwlimit=KBPS: Limits the transfer bandwidth in KB/s. This prevents rsync from consuming all available bandwidth and impacting other services.

  • --dry-run or -n: Simulates a run. Shows which files would be transferred without actually moving any data.

    Important

    Important: We strongly recommend using this option to review your command before running any operation that includes --delete.

  • --partial: Enables resumable transfers. If a transfer is interrupted, rsync keeps the partially transferred file, allowing the next run to continue from where it left off.

Upload or synchronize files and directories to an ECS instance

To upload files to an instance, run the following command from your local machine. You will be prompted to enter your password.

sudo rsync -avz -e ssh <local_path> <logon_username>@<public_ip_address>:<remote_path>

Examples:

  • To upload the file /opt/test.txt to the /home/ecs-user directory of an instance with the public IP address 1xx.xxx.xxx.121:

    sudo rsync -avz -e ssh /opt/test.txt ecs-user@1xx.xxx.xxx.121:/home/ecs-user
  • To synchronize the local directory /opt/test/ with the /home/ecs-user/test directory on the instance with public IP address 1xx.xxx.xxx.121:

    sudo rsync -avz -e ssh /opt/test/ ecs-user@1xx.xxx.xxx.121:/home/ecs-user/test

Download or synchronize files and directories from an instance

To download files from an instance to your local machine, run the following command locally. You will be prompted to enter your password.

sudo rsync -avz -e ssh <instance_username>@<public_ip_address>:<remote_path> <local_path>

Apply in production

  • Handle a large number of small files: archive first, then transfer

    Transferring a directory with thousands of small files is inefficient with any of these tools because of the connection and metadata overhead for each file. The best practice is to archive and compress the directory into a single file (for example, .tar.gz) on the source machine, transfer the archive, and then extract it on the destination machine.

  • Simplify commands with an SSH configuration file

    You can simplify connection and transfer commands by creating an alias for your ECS instance (for example, my-prod-server) in your local ~/.ssh/config file.

    # Add the following to ~/.ssh/config
    Host my-prod-server
        HostName 118.178.x.x
        User ecs-user
        Port 22
        IdentityFile ~/.ssh/id_rsa_aliyun
        ServerAliveInterval 60

    This simplifies your scp and rsync commands:

    # Original command:
    sudo scp -i ~/.ssh/id_rsa_aliyun local.txt ecs-user@118.178.x.x:/remote/
    # New command:
    sudo scp local.txt my-prod-server:/remote/
    
    # Original command:
    sudo rsync -avz -e "ssh -i ~/.ssh/id_rsa_aliyun" local_dir/ ecs-user@118.178.x.x:/remote_dir/
    # New command:
    sudo rsync -avz local_dir/ my-prod-server:/remote_dir/

FAQ

  • How do I transfer files using a specific port?

    • scp: To specify the port, use the -P option (uppercase): scp -P <port_number> ...

    • sftp: To specify the port, use the -P option (uppercase): sftp -P <port_number> ...

    • rsync: To specify the port for SSH, modify the -e option: rsync -avz -e "ssh -p <port_number>" ...

  • How do I specify a private key file when connecting to an instance with a key pair?

    • scp: To specify the private key file, use the -i option: scp -i <path_to_private_key> ...

    • sftp: To specify the private key file, use the -oIdentityFile option: sftp -oIdentityFile=<path_to_private_key> ...

    • rsync: To specify the private key for SSH, modify the -e option: rsync -e "ssh -i <path_to_private_key>" ...

References