DEV Community

Prashant Sharma
Prashant Sharma

Posted on • Updated on • Originally published at sharmapacific.in

Securely Transfer Files from/to a Remote Server using SCP

The SCP ( Secure Copy Protocol ) is a network protocol, based on the BSD RCP protocol, which supports file transfers between hosts on a network. SCP uses Secure Shell (SSH) for data transfer and uses the same mechanisms for authentication.

Using SCP you can copy file/directory :

  • From your local machine to a remote system.
  • From a remote system to your local system.
  • From one remote system to another remote system from your local system

While transferring data using SCP, files and password is encrypted, so that anyone snooping on the traffic doesn’t get anything sensitive.

Things to keep in mind before the start -

  • The scp command relies on ssh for transferring the data, so it requires an ssh key or password to authenticate on the remote systems.
  • To be able to copy file/directory you must have at least read permissions on the source file and write permission on the target system.

Syntax

The syntax for the scp command is:

scp [options] username@source_host:directory/filename  /where/to/put

In the below examples I Recursively copying entire directories -

Examples

From remote to local -

scp -r username@ipaddress:/directory/to/send /local/where/to/put

From local to remote -

scp -r /local/directory/to/send username@ipaddress:/where/to/put

Copying between two remote hosts -

scp -r username@ipaddress1:/file/to/send username@ipaddress2:/where/to/put

You can use scp with the following options according to your requirements.

Options

scp –P port : Generally 22 as a default port of scp. You can also specify a specific port.
scp –p : An estimated time and the connection speed will appear on the screen.
scp –q : Disable progress meter and warning.
scp –r : Recursively copy entire directories.
scp –v : Print debug information into the screen. It can help you debugging connection, authentication and configuration problems.
scp -c : By default SCP using “AES-128” to encrypt files. If you want to change to another cipher to encrypt it, you can use “-c”.

I hope that you now have understood how to make the best use of scp command to securely transfer files between the systems.

If you have any suggestions on your mind, please let me know in the comments. And if you know any other awesome scp command, do share with us.

Top comments (2)

Collapse
 
phlash profile image
Phil Ashby

Worth a note, because I've found people confuse them: SCP != SFTP, although both use SSH as the byte transport, the file transfer protocol is very different; as you say, SCP is based on BSD RCP, whereas SFTP is (unsurprisingly) derived from FTP (and improves on it).

SFTP is designed for interactive use and supports file management commands, SCP is intended for command line actions (as you demonstrate). It's common to find that network devices (routers, switches) support the simpler SCP for reading/writing their configuration data, but not SFTP.

Finally, FTPS is different again, using standard FTP over a secure socket (TLS) connection, not SSH, and doesn't benefit from any of SSH's capabilities such as authentication modes or fine-grained service access controls.

To add to this confusion, only the rare/obsolete FTPS is defined in any published RFCs!

Collapse
 
jwriopel profile image
Joe Riopel

linuxize.com/post/how-to-transfer-...

Another way to sync directories, on different hosts, over ssh.