SCP (Secure Copy) is a useful utility that allows you to securely copy files & folders across two locations, on same or different machines. It allows you to transfer files between your local & remote machine, or even between two remote machines using your local machine. SCP encrypts data during transfer so it is protected from malicious attackers and bots. Many even use SCP commands in their shell scripts or create cron jobs to regularly transfer files for their systems. In this article, we will look at SCP command in Linux to copy file from one server to another.
SCP command in Linux to copy file from one server to another
Here is the syntax of SCP command.
scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2
In the above command,
- user – username for source and destination hosts
- SRC_HOST – IP address of source machine
- DEST_HOST – IP address of destination machine
- file1, file2 – source and destination file paths
Please note, you may use absolute or relative paths for file locations. If you are using remote machines as source or destination host then you need to specify their IP address & username. For local machines you don’t need to mention these details.
Here are some commonly used options for SCP
-P– Specify remote host ssh port
-p– Preserve file modification and access times
-q– silent mode. no messages are displayed during file transfer
-C– Compresses data before transfer
-r– Copy directories recursively
Also, remember that in order to transfer files you need to have read permission on source host and write permission destination host. If you don’t specify colon (:) separating username and host IP address, SCP will assume it to be a local machine.
If you need to transfer files to/from remote machine, you need to provide SCP and SSH key or password for authentication on remote machines.
Now we will look at common use cases for file transfer.
Copy Local File to a Remote System with the SCP Command
Let us say you want to copy local file /home/ubuntu/data.txt to /home/project on 184.108.40.206 with user test_user, then here is the scp command for it.
$ sudo scp /home/ubuntu/data.txt firstname.lastname@example.org:/home/project
You will be asked to enter remote user’s password before file transfer begins.
email@example.com's password: /home/ubuntu/data.txt 100% 0 0.0KB/s 00:00
If you don’t specify the destination file name then SCP will use the same file name as source file. If you want to use a different file name, mention it in the destination path.
$ sudo scp /home/ubuntu/data.txt firstname.lastname@example.org:/home/project/new_data.txt
If you want to copy a folder instead of a file, replace source and destination file with folder paths, and use -r option for recursive file transfer as shown below. Here is an example to copy files from /home/ubuntu/files on local machine to /home/project/new_files on remote machine.
$ sudo scp -r /home/ubuntu/files email@example.com:/home/project/new_files
Copy Remote Files to Local System with SCP command
Here is the command to copy files from remote location /home/project/ at 220.127.116.11 to local folder /home/ubuntu. It is very similar to above commands, you just need to swap the source and destination paths in above commands, that’s all.
$ sudo scp firstname.lastname@example.org:/home/project/new_data.txt /home/ubuntu/data.txt
Here you will be asked to enter password of remote user, before file transfer.
Copy Files Between Two Remote Systems with SCP
Here is the command to transfer file /home/ubuntu/data.txt between remote systems as 18.104.22.168 and 22.214.171.124.
$ sudo scp email@example.com:/home/ubuntu/data.txt firstname.lastname@example.org:/files
In this case, you will need to enter passwords of both remote users user1 and user2.
If you want to transfer the files via the machine on which you are issuing this command, use -3 option.
$ sudo scp -3 email@example.com:/home/ubuntu/data.txt firstname.lastname@example.org:/files
In this article, we have learnt how to use SCP command to transfer files & directories between local and remote machines, as well as between two remote machines.
How to Read YAML File to Python Dictionary
How to Delete Empty Lines from Text File in Linux
How to Install Supervisor in RHEL/CentOS/Fedora
How to Install OpenSSL in Ubuntu
How to Reset Root Password in Ubuntu