All Products
Search
Document Center

Elastic Compute Service:Transfer files to Linux instances using scp, sftp, or rsync

Last Updated:Sep 18, 2025

Use scp, sftp, and rsync command-line tools to transfer files from a local Linux or macOS system to a Linux instance of Elastic Compute Service (ECS). This guide helps you choose the right tool for your needs, from simple file uploads to large-scale data synchronization, and provides best practices to ensure efficient, reliable, and secure transfers.

Choose a transfer tool

Before you start, select the most suitable tool based on your specific scenario.

  • scp: For quick, single-file transfers. It's simple and easy to learn.

  • sftp: It provides an FTP-like interactive session for file management, such as browsing, deleting, and renaming. 

  • rsync: For synchronizing directories, backups, or many small files. It is highly efficient with its incremental algorithm.

  • Unstable Networks: For large files, use the resumable transfer features in rsync or sftp.

The following table provides a more 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 the following preparations and checks before transferring files.

  1. Retrieve the public IP address of the instance

    The instance must be assigned a public IP address with public bandwidth enabled to receive files from your local computer. After enabling it, find and record the public IP address of the target instance from the Instance list page in the ECS console. All subsequent commands use this IP address.

  2. Configure security group rules

    These tools transfer files over the SSH protocol, which uses port 22 by default. Ensure the instance's 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: For security, we recommend you set this to your local public IP address.

      You can get your IP by running curl ifconfig.me or curl ip.sb in your local terminal.
  3. Check the instance's internal firewall

    The firewall within the instance's operating system (such as firewalld or ufw) might also block the connection.

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

    2. If the firewall is active, ensure it allows the SSH service or port 22. To open a specific port or service, refer to the relevant documentation for your operating system.

Use scp to transfer files

The Secure Copy Protocol (scp) is a straightforward command for copying files and directories between a local computer and a remote host.

Upload files or directories to an instance

To upload a file or folder to your instance, run the following command from your local terminal. You will be prompted to enter your 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:

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

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

Download files or directories from an instance

To download a file from your instance to your local machine, run the following command from your local terminal. You will be prompted to enter your password.

Note:scp is inefficient when transferring a large number of small files. We recommend archiving the files into a single compressed file (such as .tar.gz) before transferring, or using rsync instead.
# 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:

To download the file /home/ecs-user/test.txt from the instance with public IP address 1xx.xxx.xxx.121 to your local /opt/ directory, run the following command:

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

Use sftp for interactive file transfers

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

Connect to the instance

To establish an sftp connection, run the following command in your local terminal. Once connected, the terminal 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 entire 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:

  • To upload the local file /opt/test.txt to the /home/ecs-user/ directory 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 efficient file and directory synchronization

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

Install the tool

Ensure rsync is installed on both your local machine and the ECS 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