How to use the SCP command in Linux?
Operating systems like Linux are widely used and offer rich resource management commands. It has a cp command that copies files/directories from one path to another. But what if we need to copy a file between folders located on two different machines?
What is the SCP command?
The command copies files and directories over a computer network in an encrypted format from one machine to another on Linux.
In order to ensure secure data transfer over the network, the SSH protocol is used. This protocol provides a layer of security that allows authentication credentials and SCP commands to be sent encrypted to prevent snooping by hackers.
This tutorial will examine the various options for copying files using the SCP command with examples.
SCP Command supports the following transfer scenarios.
- From remote system to local system.
- Local system to the remote system.
- From one remote system to another remote system.
Following is the most basic SCP command usage example.
An example is using SCP without special options to copy a local file to a folder on a remote machine. This is usually all you need in most cases.
remote machine details: user/passwod: root/***, destination path :/home/cspsprotocol , IP address of SCP server = 192.168.1.192
# scp file.txt root@192.168.1.192:/home/cspsprotocol
root@192.168.1.192's password:
file.txt
Verify that the file is transferred actually.
# ssh root@192.168.1.192
root@192.168.1.192'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 the command has options along with source and destination addresses. 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 file’s new name. If you do not give the filename, it will have the same name as in the source.
OPTIONS –
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 root@192.168.1.192:/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 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 root@192.168.1.192:/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 root@192.168.1.192:/home/cspsprotocol
Recursive Copy(-r) – The option is for transferring a folder or directory. If you do not use this option, you must first zip the folder and then transfer it.
# scp -r directory root@192.168.1.192:/home/cspsprotocol
Conclusion –
You have learned how to use the SCP command on Linux. We have described basic options that can be used with the SCP command.