Introduction to SCP Command in Linux
SCP is termed as “secure copy” which allows us to secure transferring of files between localhost and remote host or between two remote hosts. It uses Secure Shell (SSH) protocol for security and authentication and security purpose on which it is based. SCP is used for it’s secured, simplified & pre-installed facility availability. In this topic, we are going to learn about SCP Command in Linux.
This command allows us to copy files from to or even between different hosts/computer files to be copied to, from, or between different hosts. It is a command-line utility that allows you to copy files & directories between two locations securely.
We can use SCP command for the below use:
- From our local systems to a remote system or host.
- From a remote system or server or host to our local system.
- Among two or more remote systems from our local system.
As security is one of the aspects of any Linux based command and secure copy (SCP) is one of them, where both files and passwords are encrypted from the malware or even anyone trying to gain sensitive information on the traffic.
Scp command syntax takes the following form :
scp [OPTIONS] [username@][Source_HOST:]file_name1 [username@]Destination_HOST:]file_name2
1. OPTIONS: We have different options such as ssh configuration, cipher, limit, ssh port, recursive copy and many more.
2. [username@]Source_HOST:]file_name1: Source file with username & host.
3. [username@]Destination_HOST:]file_name2: Destination file with username & host.
Note: Absolute or relative path can also be mentioned in case of local files while remote file names need to include a username and host with “@” annotation.
SCP command provides a variety of options. Let us discuss most commonly used:
- scp –P port: It Specify the port by which one can connect on the remote host.
- scp –p: This option is used to check access times, modification times and different modes from the original file.
- scp –q: This option is used to disable the progress done by the file while copying.
- scp –r: This option is used to recursively copy the files present in the entire directory.
- scp –S program: This option is used for encrypted connection with the name of the program, for which the program must understand the ssh(1) option.
- scp –v: This option will enable the command in verbose mode. So when we run the command with these options about the progress with different messages. This is helpful in authentication, debugging connection & configuration problems.
- scp -C: This option will enable the compression of files while copying, which increases the performance in file transfer.
- scp -i: This is option is used to file whether its a private key or a file.
Examples of SCP Command in Linux
Now let us discuss each option with different examples so that we can have a proper understanding of the SCP command concepts.
1. Copying of a file from the local system to the remote system
Let us consider, we want to copy a jdk file from our local Linux system to remote systems (172.20.11.32) by scp command then we will use the following command :
scp jdk-linux-x32.rpm firstname.lastname@example.org:/app
From the above output, the command will copy a file from the local system to our remote system under /app folder.
2. Copying a file from a remote system to a local system
In case there is a need to copy a file from a remote system to the local system under the /app folder then we need to execute the below command:
scp email@example.com:/root/Technical_file.txt /app
3. Copying of files with Verbose mode enabled
If we want to know what’s going behind the transfer of file then we can use verbose mode with the “-v” option.
scp -v jdk-linux-x32.rpm firstname.lastname@example.org:/app
From the above output, we can see how detail level information is displayed in verbose mode.
4. Transfering of multiple files to a remote system
There may be a requirement where we need to transfer multiple files in one go. So in this scp plays a vital role. By running the below command we can achieve this.
scp File_test.txt Demo_file.html jdk-linux-x32.rpm email@example.com:/app
5. Transferring files between two remote host
In order to copy files and directory between two remote hosts which from be from a local Linux system to two different remote Linux systems, below will help us to achieve this.
scp firstname.lastname@example.org:~/backup-file.zip email@example.com:/app
ssh firstname.lastname@example.org "ls -l /app/backup-file.zip" .
6. Copying of files and directory recursively (-r)
Option -r can be used to copy the files in a directory with the help of the below command recursively.
scp -r Document email@example.com:/app/
By the below command, we can verify whether the Document folder is copied to a remote system or not.
7. Increasing the file transfer speed by using compression option (-C)
To transfer the file quickly option -C can be used. This file is compressed and decompressed at the destination hosts.
scp -r -C Document firstname.lastname@example.org:/app
In the above, we are transferring the Document directory with the compression option enabled.
8. Limiting the bandwidth while copying of files ( -l )
By ‘-l’ option we can restrict bandwidth during file transfer. Bandwidth is measured in Kbit/s, shown by below example:
scp -l 500 jdk-linux-x32.rpm email@example.com:/app
9. Specifying different ssh port in scp command ( -P)
There may be some scenarios where ssh port gets changed on destination host so we can specify the port number using the ‘-P’ option.
scp -P 2030 jdk-linux-x32.rpm firstname.lastname@example.org:/app
In above ssh port for remote/destination host is ‘2030’
10. Transfer files in quiet/silent mode ( -q)
We can use the ‘-q’ option to hide the details of information and different messages.
The below command can easily do this.
scp -q -r Document email@example.com:/app/
Conclusion – SCP Command in Linux
In this way, we have learned about SCP command, different options, and application in the Linux environment for secure copying of data.
This is a guide to SCP Command in Linux. Here we discuss the different examples along with the output to understand the SCP command concepts. You may also have a look at the following articles to learn more –