How to use SCP command to Securely Copy Files
Linux scp
is a command that is used to copy files securely. It is used when we are transferring data between servers. It is similar to cp
command that copies files on the local machine.
In this article, we will learn to use scp
command to transfer files in the following scenarios:
-
SCP Local To Remote Server
-
SCP Remote Server to Local
-
SCP Remote to Remote Server
-
SCP Local to AWS
Let's start with the basic understanding of the command. The syntax of this command is:
scp [OPTION] [user@]SRC_HOST:]file_name1 [user@]DEST_HOST:]file_name2
Options: It may include ciphertext, port number, limit, etc.
[user@]SRC_HOST:]: Source address (IP address)
file_name1: Source filename
[user@]DEST_HOST:]: Destination address (IP address)
file_name2: destination location (file or directory)
While using scp
command for data transfer it prompts for a password and transfers the data in encrypted form so nobody can corrupt or access the original data.
To copy, the file must have read permission and the target destination must have write permission.
This command uses several options to deal with all types of copy operations. some of them are:
Option |
Description |
-3 |
It copies data between two remote hosts through the local host, without this option, the data is copied directly between the two remote hosts.
|
-4
|
It is used to copy data on IPv4 addresses only.
|
-6 |
It is used to copy data on IPv6 addresses only. |
-C |
It is used to enable compression.
|
-c
|
It is used to select the cipher for encrypting the data transfer.
|
-i |
It is used to select the file from which the identity (private key) for public-key authentication is read.
|
-j |
It is used to specify the destination |
-l |
It is used to limit the bandwidth. |
-P |
It is used to specify the port number on which the server is running. |
-p |
It is used to preserve the access and modification time from the original file. |
-q |
It is used to disable the progress meter and warning from the ssh. |
-r |
It is used to recursive copy the entire directory to the remote server. |
Let's start by copying a file to the remote server.
Copy a file from localhost to a remote server
Use the following command to copy a file from localhost to the remote server.
scp myfile.doc remote_username@101.155.10.20:/remote/directory
Where myfile.doc is a file name and remote_username is the name of the remote user. The 101.155.10.20 represents the remote address.
It works fine if the server runs on the default port but if the port number is different then use the below command:
scp -P 2022 myfile.doc remote_username@101.155.10.20:/remote/directory
Similarly, we can copy a directory to the remote server by using the -r option in the command. See the command below.
scp -r mydirectory remote_username@101.155.10.20:/remote/directory
Copy a file from Remote Server To LocalHost
The following command is used to copy a file from a remote server to the localhost.
scp remote_username@101.155.10.20:/remote/directory/myfile local/directory
It is similar to the above commands where we copy local to the remote server except the remote address is used first and then local directory path.
Copy a file from Remote Server To another Remote Server
The following command is used to copy a file from one remote server to another remote server by using the local system.
scp remote1_username@101.155.10.20:/remote/directory/myfile.txt remote2_username@15.150.15.10:/remote/directory
It will ask for the passwords for both the remote systems. So, provide the password for successful data transfer.
Copy a file from Remote Server To another Remote Server by using localhost
The following command is used to copy a file from one remote server to another remote server by using a local system.
scp -3 remote1_username@101.155.10.20:/remote/directory/myfile.txt remote2_username@15.150.15.10:/remote/directory
Notice, the -3 option is used while transferring via localhost.
Copy a file from localhost to AWS Server
If you are working on an AWS server want to copy a file from local to AWS then use the following command.
scp -i key-name.pem myfile.doc bitnami@40.13.23.10:/remote/directory
In the above command, we used .pem file that contains private keys for the server. We can use this to access the AWS server via ssh command and similarly can be used to transfer the file between local and AWS server.
Conclusion
In this article, we have learned about the scp command and its usage with different scenarios such as transfer data between local to a remote server, remote server to a local machine, and transfer between local to AWS server as well.
It is an easy and better way to transfer data over the server very securely.