SCP (secure copy) command line tool in Linux that is used to copy files over the networks in a secure way. It can be used to copy files between the local host and the remote host or between two remote hosts. It uses same authentication mechanism as used in the Secure Shell (SSH) protocol.
In this guide, we will see how to use the scp command with practical examples and common scp options.
SCP Command Syntax
Below is the basic syntax for
scp [OPTION] [[username@]src_host:]file1 [[username@]dest_host:]file2
- OPTION – scp options such as cipher, ssh configuration, ssh port, limit, recursive copy, etc.
- [[username@]src_host:]file1 – It’s source file which need to copy.
- [[username@]dest_host:]file2 – Destination where to copy.
For local files provide absolute path and relative path for remote host including user and host details.
Some of the most widely used options in scp command are listed below:
-Pssh port number of remote host
-pPreserves permissions and access time of files
-rCopy files and directories recursively
-iidentity File or private key
-llimit the bandwidth while copying
-qSuppress warning message of SSH
Make sure you have at least read permissions on the source file and write permission on the target system to be able to copy files.
Copy Files and Directories using scp
Following are the examples to copy files from local to remote, remote to local and between two remote systems.
Copy a Local File to a Remote System with the scp Command
Run below command to copy a file from local to remote system:
scp info.php firstname.lastname@example.org:/var/www/html
info.php is the file which we want to copy and
tecnstuff is the user of remote system.
18.104.22.168 is the ip address of remote system. The
/var/www/html is the path to the directory where you want to copy the file.
It will ask you to enter password and the copy process will start.
email@example.com's password: info.php 100% 0 0.0KB/s 00:00
Give new file name with remote path, if you want to set other than the original:
scp info.php firstname.lastname@example.org:/var/www/html/index.php
SCP command to copy directories is same as file just you have to specify the
-r option for recursive.
scp -r local/directory email@example.com:/remote/directory
If you SSH port of remote host is different than default
22 then you can specify using
-P flag as below:
scp -P 3452 info.php firstname.lastname@example.org:/var/www/html
Copy a Remote File to a Local System using the scp command
To copy file from remote host to local system, set remote file location as source and local path as the destination.
For example to copy a
info.php from a remote server with IP
22.214.171.124 run the following command:
scp email@example.com:/var/www/html/info.php /local/directory/info.php
Copy a File Between Two Remote Systems using the scp Command
It doesn’t required to login to either system to copy file between two remote systems:
For example, Run the below command to copy
info.php file from host
scp firstname.lastname@example.org:/home/info.php email@example.com:/home
It will copy
info.php file from /home directory of
126.96.36.199 to another host
You will be asked to enter password for both remote accounts. On success file will be transfer from one to another system.
In this tutorial, you learned how to use the scp command to copy files and directories.
If you have any question or feedback, leave a comment below.