Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Transfer data with SCP

Hello readers! Transferring data to your friends’ machine is not a big deal when using storage devices such as pen drives, CDs, etc. But have you ever intended to migrate files between different systems over a network, to a machine that’s out of your reach, like transferring data between an EC2 instance and your local computer? If yes, then scp is a simple tool to help you out there. Let’s find out then, what is scp and what are its use cases?

What is Secure Copy Protocol (SCP)?

I recently did a post on how SSH works, which elaborate, behind the scene of the protocol. So, as we have SSH for a secure connection, similarly, we have scp for a secure copy. It is a network protocol, based on the BSD and RCP, which supports file transfers between hosts on a network.

scp uses Secure Socket Shell (SSH) for data transfer and uses the same mechanisms for authentication, thereby ensuring the authenticity and confidentiality of the data. Therefore, for using , you first need to have an  SSH server.

Syntax:

Normal “cp” (copy) command has the following syntax on *nix platforms. Here, data is copied to a different location in the same system.

$ cp [OPTION]... [-T] SOURCE DEST

Similarly, “scp” follows the same format. But, in this case, data is transferred between 2 different systems. Thus, its syntax is as follows.

$ scp [OPTION] user1@hostname:/source/ user2@hostname:/destination/

SCP with use cases

Transferring files on password-based-authentication

1. From localhost to a remote server.

$ scp ~/newfile [email protected]:~/

2. From a remote server to localhost.

$ scp [email protected]:~/newfile ~/

3. From one remote server to another, through localhost.

$ scp -3 [email protected]:~/newfile [email protected]:~/

“-3” (option) copies between two remote hosts, transferred through the localhost. Note that, this option disables the progress meter. Without this option, the data is copied directly between two remote hosts, for which, they should be accessible to each other.

Transferring files on key based authentication

1. From localhost to a remote server.

$ scp -i ~/private.key ~/newfile [email protected]:~/

“-i” (option) is used to include the identity file (our private key). Necessary for key based authentication.

2. From a remote server to localhost.

$ scp -i ~/private.key [email protected]:~/newfile ~/

3. From one remote server to another, through localhost.

$ scp -3 -i ~/private.key1 -i ~/private.key2 [email protected]:~/newfile [email protected]:~/

SCP with different parameters

Parameters either change the behavior or extends the functionality of a command. Let’s see some of the alterations of SCP.

Identity file:   -i

$ scp -i ~/private.key ~/newfile [email protected]:~/

As I have already mentioned this option includes identity file (private key).

Recursive:   -r

$ scp -i ~/private.key -r ~/source/folder [email protected]:~/destination/

This option recursively copies files from the source folder.

Verbose:   -v

$ scp -v -i ~/private.key [email protected]:~/source ~/destination/

This option gives you background information while transferring files as shown below.

debug1: channel 0: new [client-session]
debug1: Requesting [email protected]
debug1: Entering interactive session.
debug1: pledge: network
debug1: client_input_global_request: rtype [email protected] want_reply 0
debug1: Sending environment.
debug1: Sending env LANG = en_IN
debug1: Sending command: scp -v -t ~/
Sending file modes: C0664 0 file_on_client
Sink: C0664 0 file_on_client
file_on_client 100% 0 0.0KB/s 00:00 
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
Transferred: sent 3556, received 2600 bytes, in 2.9 seconds
Bytes per second: sent 1231.7, received 900.6
debug1: Exit status 0

Compressed: -C

$ scp -C -i ~/private1.key -i ~/private2.key [email protected]:~/source [email protected]:~/destination

This option compresses transferring files only in the network, such that when they are received by the destination host, they would return to their original size. Thereby, transfer speed increases while large files are being transferred.

Preserves: -p

$ scp -p ~/newfile [email protected]:~/

This option preserves modification times, access times, and modes from the original file.

If you want to explore more such parameters? Please refer SCP manual page with command.

$ man scp

Conclusion

SCP is a remote file copy program, that’s suit perfectly in cases when you have to transfer data in a single pass. However, FTP is more recommended to transfer multiple files/directories, due to its better session-based control over the connection.

Thanks for reading! And in case of a query, please feel free to write in the comments section below.



This post first appeared on The Geeky Way | Technology Inside Out, please read the originial post: here

Share the post

Transfer data with SCP

×

Subscribe to The Geeky Way | Technology Inside Out

Get updates delivered right to your inbox!

Thank you for your subscription

×