How to use the SCP command in Linux to transfer files securely with examples?
Linux provides command cp to copy files/directories from one path to another. But what if we need to copy a file between folders located on two different machines?
The SCP or Secure copy is the utility on Linux that does the files/directories copying from one machine to another over the computer network. SCP command uses SSH protocol for secure data transfer over the network. It adds a layer of security that sends encrypted credentials along with that to prevent any snooping by the hackers.
In this tutorial, we will explain how to use the SCP command with examples. We will explore the various SCP options for the different types of copying.
Copying types with SCP:
- From remote system to local system.
- Local system to the remote system.
- From one remote system to another remote system.
Basic SCP command example:
The simplest use of SCP command example with no special options for copying a local file into a folder on the remote machine. Most of the time you just need this only.
remote machine details: user: root, path :/home/cspsprotocol , IP address = 192.168.1.192
# scp file.txt firstname.lastname@example.org:/home/cspsprotocol email@example.com's password: file.txt
Verify that file is transferred actually.
# ssh firstname.lastname@example.org email@example.com's password: Last login: Sun Aug 15 01:30:19 2021 from 192.168.1.201 [root@CentOs7-64-192 ~]# cd /home/cspsprotocol/ [root@CentOs7-64-192 cspsprotocol]# ls -lrt total 4 -rw-r--r-- 1 root root 11 Aug 15 01:34 file.txt [root@CentOs7-64-192 cspsprotocol]#
Syntax of SCP command on Linux.
Following is the syntax of the SCP command.
#scp [OPTIONS] [user@]source_address:]file1 [user@]destination_address:]file2
We can see that along with source and destination addresses the command has options. For all options, you can visit SCP options. Here we will cover mostly used options.
[user@]source_address:]file1 – Source machine user, IP address, and file that needs to copy.
[user@]destination_address:]file2 – Destination machine user, IP address, and the new name of the file. If you do not give the filename, it will have the same name as in the source.
Specify Port (-P) – With this option, you can use a port number other than the default. By default, the SCP server listens on port number 22. But for security reasons, the administrator could use another port. How to use a port number in the SCP command example is below.
#scp -P port_number file user@host:/path
Verbose (-v) – This option displays the detailed information for the command execution output.
#scp -v file.txt firstname.lastname@example.org:/home/cspsprotocol Sending file modes: C0644 0 file.txt Sink: C0644 0 file.txt file.txt 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 2096, received 2476 bytes, in 0.1 seconds Bytes per second: sent 25675.6, received 30330.5 debug1: Exit status 0
Limit network bandwidth (-l) – This option limits the network bandwidth for the file transfer. It is useful when there are big files that need to transfer and at the same time you do not want to drain all network speed by the SCP command.
#scp -l 3 file.txt email@example.com:/home/cspsprotocol
Preserve File timings (-p) – When transferring files using SCP, the destination file has timings when it is copied to the destination. But if you want to preserve the creation, modification, and access times from the source. For that, you can use the
'-p' option while transferring.
#scp -p file.txt firstname.lastname@example.org:/home/cspsprotocol
Recursive Copy(-r) – The option is for transferring a folder or directory. If you do not use this option, you need to first zip the folder then do the transfer.
# scp -r directory email@example.com:/home/cspsprotocol
You have learned how to use the SCP command on Linux. We have described basic options that can be used with the SCP command.