
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
command:
scp [OPTION] [[username@]src_host:]file1 [[username@]dest_host:]file2
Here,
- 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:
-P
ssh port number of remote host-p
Preserves permissions and access time of files-r
Copy files and directories recursively-C
Enable Compression-i
identity File or private key-l
limit the bandwidth while copying-q
Suppress warning message of SSH-v
verbose output
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 tecnstuff@22.33.44.55:/var/www/html
Here, info.php
is the file which we want to copy and tecnstuff
is the user of remote system. 22.33.44.55
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.
tecnstuff@22.33.44.55'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 tecnstuff@22.33.44.55:/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 tecnstuff@22.33.44.55:/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 tecnstuff@22.33.44.55:/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.33.44.55
run the following command:
scp tecnstuff@22.33.44.55:/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 22.44.55.66
to 55.44.22.11
:
scp host1user@22.44.55.66:/home/info.php host2user@55.44.33.11:/home
It will copy info.php
file from /home directory of 22.44.55.66
to another host 55.44.22.11
.
You will be asked to enter password for both remote accounts. On success file will be transfer from one to another system.
Conclusion
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.
Leave a Reply