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
rsyncorsftp.
The following table provides a more detailed technical comparison:
Procedure
Before you begin
Complete the following preparations and checks before transferring files.
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.
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.meorcurl ip.sbin your local terminal.
Check the instance's internal firewall
The firewall within the instance's operating system (such as
firewalldorufw) might also block the connection.Log on to the instance and check the firewall status.
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:scpis 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 usingrsyncinstead.
# 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>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.txtto the/home/ecs-user/directory on the instance:sftp> put /opt/test.txt /home/ecs-userTo 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.txtfrom the instance to your local/optdirectory:sftp> get /home/ecs-user/test.txt /optTo download the directory
/home/ecs-user/test/from the instance to your local/optdirectory: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 rsyncCommon 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-zmight be faster.
Common options for production environments
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.txtto the/home/ecs-userdirectory of an instance with the public IP address1xx.xxx.xxx.121:sudo rsync -avz -e ssh /opt/test.txt ecs-user@1xx.xxx.xxx.121:/home/ecs-userTo synchronize the local directory
/opt/test/with the/home/ecs-user/testdirectory on the instance with public IP address1xx.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/configfile.# 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 60This simplifies your
scpandrsynccommands:# 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
-Poption (uppercase):scp -P <port_number> ...sftp: To specify the port, use the
-Poption (uppercase):sftp -P <port_number> ...rsync: To specify the port for SSH, modify the
-eoption: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
-ioption:scp -i <path_to_private_key> ...sftp: To specify the private key file, use the
-oIdentityFileoption:sftp -oIdentityFile=<path_to_private_key> ...rsync: To specify the private key for SSH, modify the
-eoption:rsync -e "ssh -i <path_to_private_key>" ...
References
To back up important files that you upload to an ECS instance, see Create a snapshot.
If you need to upload files to a Windows instance, you can use other file transfer methods. See Select a file transfer method.
To transfer files from Windows to a Linux instance, use a tool such as WinSCP. See Use WinSCP to upload a file from an on-premises Windows computer to a Linux instance.