All Products
Search
Document Center

Elastic Compute Service:Use SFTP, SCP, or Rsync to transfer files to a Linux instance

Last Updated:Feb 19, 2025

This topic describes how to use file transfer tools, such as SSH File Transfer Protocol (SFTP), Secure Copy Protocol (SCP), and Rsync, to upload files to a Linux Elastic Compute Service (ECS) instance from an on-premises Linux or macOS computer or download files from a Linux ECS instance to an on-premises Linux or macOS computer when the instance is assigned a public IP address.

Tools

Item

SCP

SFTP

Rsync

Feature

Quickly uploads or downloads a single file or folder.

  • Interactively transfers a file or folder.

  • Supports file management.

  • Supports resumable file transfer.

Uses incremental synchronization. Only the content that differs from the content in the destination is transferred to the destination.

Interaction

Runs commands to perform one-time operations.

Supports interactive commands, similarly to the FTP client.

Runs commands or scripts to perform operations.

Scenario

  • Suitable for scenarios in which a small number of files or folders need to be transferred in a simple and quick manner.

  • Suitable for scenarios in which files or folders are temporarily transferred without the need to retain the transfer progress, such as configuration file updates.

  • Suitable for scenarios in which file management operations are required.

  • Suitable for scenarios in which the network is unstable and resumable file transfer is required.

  • Suitable for scenarios such as regular data backup and large-scale data synchronization.

  • Suitable for scenarios in which incremental transfer is required to save bandwidth and time.

Limits

On-premises operating system

Instance network configuration

File size

Transfer speed

Maximum number of files

Linux or macOS

The ECS instance is assigned a static public IP address (also called an auto-assigned or system-assigned public IP address) or associated with an elastic IP address (EIP).

Not limited

Not limited

Not limited

Preparations

  • Check the status of a Linux ECS instance. The operations described in this topic can be performed on a Linux ECS instance only if the instance is in the Running state.

    Check the status of a Linux ECS instance

    You can view the status of a Linux ECS instance on the Instance page in the ECS console. The following figure shows that a Linux ECS instance is in the Running state.

    For information about how to check the status of an ECS instance, see View instance information.

    image

  • Obtain the public IP address of the Linux ECS instance. The public IP address of the Linux ECS instance is required to connect to the instance and perform the operations described in this topic.

    Obtain the public IP address of the Linux ECS instance

    You can view the public IP address of the Linux ECS instance in the IP Address column on the Instance page in the ECS console.

    For information about how to check information about an ECS instance, see View instance information. For information about how to enable public bandwidth for an ECS instance to allow the instance to access the Internet, see Enable public bandwidth.

    image

  • Check the security groups of the Linux ECS instance. Before you use a tool described in this topic to upload files to the Linux ECS instance, check whether the port used by the tool is open in a security group of the instance. The default port is port 22.

    Check that a rule for opening the required port exists in a security group of the Linux ECS instance

    For information about how to add a security group rule, see Add a security group rule.

    Action

    Priority

    Protocol type

    Port range

    Authorization object

    Allow

    1

    Custom TCP

    The port used by the file transfer tool. Default value: 22.

    Specify the public IP address of your on-premises computer or the CIDR block to which the public IP address belongs.

    Warning

    If you specify 0.0.0.0/0, access on the specified port is allowed for all IPv4 addresses, which poses security risks. Proceed with caution.

    You can obtain the public IP address of your on-premises computer from https://cip.cc/.

    For example, if the public IP address of your on-premises computer is 118.xxx.xxx.xx8, add a rule to a security group of the Linux ECS instance, as shown in the following figure.

    image

Use SCP to transfer files

SCP is an SSH-based file transfer protocol that provides secure file transfer, copy, and management capabilities. The following section describes how to transfer files by using SCP.

  • Upload a file or folder

    If you want to upload a file or folder from your on-premises computer to the Linux ECS instance, run the following command on your on-premises computer. After you run the command, you may be prompted to enter a password.

    # Upload a single file to the Linux ECS instance.
    scp <File path on your on-premises computer> <Logon username of the Linux ECS instance>@<Public IP address of the Linux ECS instance>:<File path on the Linux ECS instance>
    
    # Upload an on-premises folder to the Linux ECS instance.
    scp -r <Folder path on your on-premises computer> <Logon username of the Linux ECS instance>@<Public IP address of the Linux ECS instance>:<Folder path on the Linux ECS instance>
  • Download a file or folder

    If you want to download a file or folder from the Linux ECS instance to your on-premises computer, run the following command on your on-premises computer. After you run the command, you may be prompted to enter a password.

    # Download a single file to your on-premises computer.
    scp <Logon username of the Linux ECS instance>@<Public IP address of the Linux ECS instance>:<File path on the Linux ECS instance> <File path on your on-premises computer>
    
    # Download a folder from the Linux ECS instance to your on-premises computer.
    scp -r <Logon username of the Linux ECS instance>@<Public IP address of the Linux ECS instance>:<Folder path on the Linux ECS instance> <Folder path on your on-premises computer>

Examples

  • To upload the /opt/test.txt file from your on-premises computer to the /home/ecs-user/ path of the Linux ECS instance whose public IP address is 1xx.xxx.xxx.121, run the following command:

    scp /opt/test.txt ecs-user@1xx.xxx.xxx.121:/home/ecs-user/
  • To download the /home/ecs-user/test.txt file from the Linux ECS instance whose public IP address is 1xx.xxx.xxx.121 to the /opt/ path of your on-premises computer, run the following command:

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

Specify the SSH port when you use SCP

By default, SCP uses SSH port 22. If you changed the SSH port to a port other than port 22, specify the new SSH port by using the -P parameter when you run the scp command. The following sample code provides an example of the scp command:

# Upload a single file to the Linux ECS instance and specify the SSH port.
scp -P <SSH port> <File path on your on-premises computer> <Logon username of the Linux ECS instance>@<Public IP address of the Linux ECS instance>:<File path on the Linux ECS instance>

Specify the private key file when you use SCP (key pair-based authentication)

If you bound an SSH key pair to the Linux ECS instance, specify the path of the private key file by using the -i parameter when you connect to the instance. Sample command:

scp -i <Path of the private key file> <Path on your on-premises computer> <Logon username of the Linux ECS instance>@<Public IP address of the Linux ECS instance>:<Path on the Linux ECS instance>

Use SFTP to transfer files

SFTP is an interactive file transfer tool based on the SSH protocol. SFTP supports various features such as file management and resumable file transfer. The following section describes how to transfer files by using SFTP.

Step 1: Connect to the Linux ECS instance

On your on-premises terminal, run the following command to connect to the Linux ECS instance:

sftp <Logon username of the Linux ECS instance>@<Public IP address of the Linux ECS instance>

The following figure shows that you are connected to the Linux ECS instance.

image

Example

To connect to the Linux ECS instance whose public IP address is 1xx.xxx.xxx.121 as the ecs-user user, run the following command:

sftp ecs-user@1xx.xxx.xxx.121

Specify the SFTP port when you use SFTP to connect to the Linux ECS instance

If you changed the SFTP port on the Linux ECS instance, specify the new SFTP port by using the -P parameter when you connect to the instance. Sample command:

sftp -P <SFTP port> <Logon username of the Linux ECS instance>@<Public IP address of the Linux ECS instance>

Specify the private key file when you use SFTP to connect to the Linux ECS instance (key pair-based authentication)

If you bound an SSH key pair to the Linux ECS instance, specify the path of the private key file by using the -oIdentityFile parameter when you connect to the instance. Sample command:

sftp -oIdentityFile=<Path of the private key file> <Logon username of the Linux ECS instance>@<Public IP address of the Linux ECS instance>

Step 2: Upload or download files

  • Upload a single file or a folder from your on-premises computer to the Linux ECS instance

    # Upload a single file.
    sftp> put <File path on your on-premises computer> <File path on the Linux ECS instance>
    
    # Upload a folder.
    sftp> put -r <Folder path on your on-premises computer> <Folder path on the Linux ECS instance>
  • Download a single file or a folder from the Linux ECS instance to your on-premises computer

    # Download a single file.
    sftp> get <File path on the Linux ECS instance> <File path on your on-premises computer>
    
    # Download a folder.
    sftp> get -r <Folder path on the Linux ECS instance> <Folder path on your on-premises computer>

Examples

  • Upload the /opt/test.txt file from your on-premises computer to the /home/ecs-user/ path of the Linux ECS instance.

    sftp> put /opt/test.txt /home/ecs-user
  • Upload the /opt/test/ folder from your on-premises computer to the /home/ecs-user/ path of the Linux ECS instance.

    sftp> put -r /opt/test/ /home/ecs-user/
  • Download the /home/ecs-user/test.txt file from the Linux ECS instance to the /opt path of your on-premises computer.

    sftp> get /home/ecs-user/test.txt /opt
  • Download the /home/ecs-user/test/ folder from the Linux ECS instance to the /opt path of your on-premises computer.

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

Step 3: Resume the file transfer process when the process is interrupted

If the transfer of a large file is interrupted, replace the get and put commands in Step 2 with the reget and reput commands to continue the file transfer.

# Resume the upload process.
sftp> reput <Path on your on-premises computer> <Path on the Linux ECS instance>

# Resume the download process.
sftp> reget <Path on the Linux ECS instance> <Path on your on-premises computer>

Step 4: Disconnect from the Linux ECS instance

After the transfer task is completed, run the quit or bye command to exit SFTP.

sftp> quit
sftp> bye

Use Rsync to transfer files

Rsync is a file synchronization and transfer tool for Linux and Unix operating systems. You can use Rsync to upload on-premises files to a Linux ECS instance or download files from a Linux ECS instance to your on-premises computer. Rsync also supports the folder synchronization feature. To install and use Rsync, perform the following steps:

Step 1: Install Rsync

Install Rsync on your on-premises computer and the Linux ECS instance. The installation method varies based on the operating system.

Alibaba Cloud Linux 2 and Alibaba Cloud Linux 3

sudo yum install -y rsync 

CentOS 6, CentOS 7, and CentOS 8

sudo yum install -y rsync 

Ubuntu and Debian

sudo apt install -y rsync 

Step 2: Use Rsync to upload or download files

  • Upload a single file

    If you want to upload a file from your on-premises computer to the Linux ECS instance, run the following command on your on-premises computer. After you run the command, you may be prompted to enter a password.

    rsync -avz -e ssh <Path of the file or folder on your on-premises computer> <Logon username of the Linux ECS instance>@<Public IP address of the Linux ECS instance>:<Path of the file or folder on the Linux ECS instance>
  • Download a single file

    If you want to download a file from the Linux ECS instance to your on-premises computer, run the following command on your on-premises computer. After you run the command, you may be prompted to enter a password.

    rsync -avz -e ssh <Logon username of the Linux ECS instance>@<Public IP address of the Linux ECS instance>:<Path of the file or folder on the Linux ECS instance> <Path of the file or folder on your on-premises computer>
  • Synchronize (or upload) a folder from your on-premises computer to the Linux ECS instance

    If you want to upload (or synchronize) a folder from your on-premises computer to the Linux ECS instance, run the following command on your on-premises computer. After you run the command, you may be prompted to enter a password.

    rsync -avz -e ssh <Folder path on your on-premises computer> <Logon username of the Linux ECS instance>@<Public IP address of the Linux ECS instance>:<Folder path on the Linux ECS instance>
  • Synchronize (or download) a folder from the Linux ECS instance to your on-premises computer

    If you want to download (or synchronize) a folder from the Linux ECS instance to your on-premises computer, run the following command on your on-premises computer. After you run the command, you may be prompted to enter a password.

    rsync -avz -e ssh <Logon username of the Linux ECS instance>@<Public IP address of the Linux ECS instance>:<Folder path on the Linux ECS instance> <Folder path on your on-premises computer>

Examples

  • To upload the /opt/test.txt file from your on-premises computer to the /home/ecs-user path of the Linux ECS instance whose public IP address is 1xx.xxx.xxx.121, run the following command:

    rsync -avz -e ssh /opt/test.txt ecs-user@1xx.xxx.xxx.121:/home/ecs-user
  • To synchronize the /opt/test folder from your on-premises computer to the /home/ecs-user/test path of the Linux ECS instance whose public IP address is 1xx.xxx.xxx.121, run the following command:

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

Specify a port when you use Rsync

To specify the SSH port, you can use the -e parameter. Sample command:

rsync -avz -e "ssh -p <SSH port>" <Path on your on-premises computer> <Logon username of the Linux ECS instance>@<Public IP address of the Linux ECS instance>:<Path on the Linux ECS instance>

Specify the private key file when you use Rsync (key pair-based authentication)

If you bound an SSH key pair to the Linux ECS instance, you can use the key pair to authenticate your identity. To specify the path of the private key file, you can use the -e parameter. Sample command:

rsync -avz -e "ssh -i <Path of the private key file>" <Path on your on-premises computer> <Logon username of the Linux ECS instance>@<Public IP address of the Linux ECS instance>:<Path on the Linux ECS instance>

References