How to Securely Transfer Files and Directories Using SCP
SCP
, also known as secure copy
, is a command-line utility to transfer files and directories from local to a remote system and vice-versa. It also allows us to transfer files and directories between two remote systems. The files and passwords are encrypted during the transfer so that it is a more secure way of transfer.
SCP
Command
Syntax
scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2
Flags:
OPTION
: It representsscp
options such as cipher, ssh configuration, ssh port, limit, recursive copy …etc[user@]SRC_HOST:]file1
: source file or directory to be copied[user@]DEST_HOST:]file2
: path to the directory where the source file or directory needs to be copied
scp
provides various options to control the transfer. Some of the most widely used options are:
-P |
Specify ssh port of remote host |
-p |
Preserve files modification and access times. |
-q |
Suppress the progress meter and error messages |
-C |
Compress data while transfer |
-r |
Copy files recursively. |
Things to Be Noticed While Using scp
Command:
- This command uses
ssh
key or password to authenticate remote systems. - It recognizes remote systems with
:
symbol. - We must have a look at read permissions of source file or directory and write permissions of destination file or directory.
scp
overwrites files without warning. So we must be careful during the transfer of files that share the same name and location on both systems.
Transfer Files and Directories Using scp
Copy a File From Local System to Remote System
scp main.py remote_username@11.11.0.200:/Documents/directory
This command will prompt us for a user password and the transfer will start once we enter the correct password.
Output:
remote_username@11.11.0.200's password:
main.py 100% 0 0.0KB/s 00:00
It copies the file main.py
on our local system to the remote server with user-name remote_username
and IP address 11.11.0.200
. /Documents/directory
represents the destination directory on the remote server where the file needs to be copied to. If the remote directory is not specified, the file will be copied to the remote machine’s home directory.
If the SSH
on the remote is listening to other port than default port 22
, we can specify the port using the -P
option.
scp -P 8080 main.py remote_username@11.11.0.200:/Documents/directory
Copy a Directory From the Local System to the Remote System
We must use -r
argument with the scp
command to transfer directories that represents a recursive transfer of all the files inside the directory.
scp -r /Documents/myapp remote_username@11.11.0.200:/Documents/remote_app
It copies the directory myapp
inside Documents
directory on the local machine to the directory remote_app
inside Documents
of the remote machine.
Copy a File From the Remote System to the Local System
To copy a remote system file to the local system, we use a remote location as a source and local location as a destination.
scp remote_username@11.11.0.200:/remote/main.py /Documents/local
It copies the file main.py
from the remote system to our local system with destination directory /Documents/local
.
Copy File From One Remote Location to Another Remote Location
scp userA@11.11.0.200:/host1/main.py userB@11.11.0.205:/host2
It copies the file /host1/main.py
from the remote host with IP address 11.11.0.200
to the directory host2
in host with IP address 11.11.0.205
.
To route the traffic through the machine in which the command is issued, we add the -3
option to the scp
command.
scp -3 userA@11.11.0.200:/host1/main.py userB@11.11.0.205:/host2
Suraj Joshi is a backend software engineer at Matrice.ai.
LinkedIn