What is Secure Copy (SCP)?

Secure Copy (SCP) is a mechanism that allows us to transfer files (or folder) between a local and a remote host or between two remote hosts. It refers to two things, 1) Secure Copy Protocol, 2) Secure Copy (scp) Command.

Secure Copy (SCP) is an application level networking protocol that supports file transfer in secure mode. This protocol is based on BSD RCP and not defined in any RFC. SCP uses Secure Shell (SSH) for data transfer, therefore, the authenticity and privacy is maintained during data transfer. You can also retain the file attributes like permissions, time stamps of the source files to the destination ones. That means the transferred files will get the same file attributes as the corresponding source ones. The protocol runs over TCP (default port 22).

Secure Copy (scp) is also a Linux command, scp, which is used to transfer file across hosts. The generic syntax of the command:

scp [options] [[user@]host1:]file1 [[user@]host2:]file2

It has lot of options but I’ll discuss about the most commonly use cases in this document.

Examples of commonly used scp command

Copy a file “samplefile.txt” from a remote host (remotehost) to the local host (localhost). Localhost is the machine where you are running the command.
If the file is located at the home directory of remoteuser, user of the remote host and you want the copy the file in current directory where you are running the command from, the command will be:

$ scp remoteuser@remotehost:samplefile.txt .

The IP address of the remote host, can also be used in place of the host name (remotehost). If the IP address of the remote host is 123.45.2.1, the command will be:

$ scp remoteuser@123.45.2.1:samplefile.txt .

The command will ask for the password of the remoteuser1. If the command executes successfully, you will see the file samplefile.txt in the directoy from where the command ran on the localhost.

If you want to copy multiple files from remotehost to localhost, the command will be:

$ scp remoteuser@remotehost:\{samplefile1.txt,samplefile2.txt\} .

Here also, the files should be located at the home directory of remoteuser on remotehost.

If the file is located in “/some/remote/directory” on the remote host (remotehost) and you want to copy to “/some/local/directory” on localhost, the command will be:

$ scp remoteuser@remotehost:/some/remote/directory/samplefile.txt /some/local/directory/

If you want to change the copied file name in the destination to samplefile_copy.txt, command will be:

$ scp remoteuser@remotehost:/some/remote/directory/samplefile.txt /some/local/directory/samplefile_copy.txt

Now we’ll see how to copy the samplefile.txt file from localhost to a remote host (remotehost).

$ scp samplefile.txt remoteuser@remotehost:/some/remote/directory

The file needs to be present in the same directory where the command is running from on the localhost. The file will be copied to the “/some/remote/directory” of the remote host (remotehost).

If the file is not located where the command is running but located in “/some/local/directory”, the command will be:

$ scp /some/local/directory/samplefile.txt remoteuser@remotehost:/some/remote/directory

If you want to copy the file to the home directory of the remote host, the command would be:

$ scp samplefile.txt remoteuser@remotehost:~

If you want to copy multiple files from localhost to the home directory of the remotehost the command would be:

$ scp samplefile.txt samplefile.txt remoteuser@remotehost:~

If you want to copy the file to the home directory of the remote host using port 2345

$ scp -P 2345 samplefile.txt remoteuser@remotehost:~

Now we’ll see how to copy file from one remote host (remotehost1) to another remote host (remotehost2). If you want to copy samplefile.txt from the remoteuser1’s home directory of remotehost1 to remoteuser2’s home directoty of remotehost2, the command would be:

$ scp remoteuser1@remotehost1:samplefile.txt remoteuser2@remotehost2:~

If the file exists in /some/remote/directory1 location of remotehost1 and you want to copy that to /some/remote/directory2 of remotehost2

$ scp remoteuser1@remotehost1:/some/local/directory/samplefile.txt remoteuser2@remotehost2:/some/remote/directory

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation.


If you also want to contribute, click here.

Leave a Reply

Your email address will not be published. Required fields are marked *

0
0
0
0
1
0