Technology Inside Out!

Index ¦ Archives ¦ Atom ¦ RSS

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 andRCP, 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  SSHserver.

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 ubuntu@18.222.18.222:~/

2. From a remote server to localhost.

$ scp ubuntu@18.222.18.222:~/newfile ~/

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

$ scp -3 ubuntu@18.222.18.222:~/newfile ubuntu@19.111.19.111:~/

"-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 ubuntu@18.222.18.222:~/

"-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 ubuntu@18.222.18.222:~/newfile ~/

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

$ scp -3 -i ~/private.key1 -i ~/private.key2 ubuntu1@18.222.18.222:~/newfile ubuntu1@19.111.19.111:~/

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 ubuntu@123.12.123:~/

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

Recursive:   -r

$ scp -i ~/private.key -r ~/source/folder ubuntu@123.12.123:~/destination/

This option recursively copies files from the source folder.

Verbose:   -v

$ scp -v -i ~/private.key ubuntu@123.12.123:~/source ~/destination/

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

debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: pledge: network
debug1: client_input_global_request: rtype hostkeys-00@openssh.com 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 ubuntu1@123.12.123:~/source ubuntu2@321.21.321:~/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 ubuntu@123.12.123:~/

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.

© The Geeky Way. Built using Pelican. Theme by Giulio Fidente on github.

Disclaimer Privacy policy