SCP is a command-line tool in linux distributions for securely copying files and directories across systems over a network. SCP stands for Secure Copy as it uses the ssh protocol to copy files.
When copying, the scp command establishes an ssh connection to the remote system. In other words, we can say that scp uses the same SSH security mechanism on the backend, which requires a password or key for authentication.
SCP Command Syntax
Copy from local system to remote host
scp <options> <files_or_directories> user@target_host:/<folder>
Copy from remote host to local system
scp <options> user@target_host:/files <folder_local_system>
Some of the most widely used options in the scp command are listed below
- -C enable compression
- -i identity file or private key
- -l limit bandwidth when copying
- -P target host SSH port number
- -p preserve file permissions, mode and access time when copying
- -q Suppresses SSH warning messages
- -r copy files and directories recursively
- -v verbose output
1) Copy files from local to remote
Suppose we want to copy the jdk rpm package from the local Linux system to the remote system (172.20.10.8) /opt directory
$ scp jdk-linux-x64_bin.rpm [email protected]:/opt
[email protected]'s password:
jdk-linux-x64_bin.rpm 100% 10MB 27.1MB/s 00:00
$
2) Copy files from remote to local
Suppose we want to copy a file from remote system to our local system under the /tmp folder, execute the following,
Suppose we want to copy files from remote to local /tmp directory
$ scp [email protected]:/root/Technical-Doc-RHS.odt /tmp
[email protected]'s password:
Technical-Doc-RHS.odt 100% 1109KB 31.8MB/s 00:00
$ ls -l /tmp/Technical-Doc-RHS.odt
-rwx------. 1 pkumar pkumar 1135521 Oct 19 11:12 /tmp/Technical-Doc-RHS.odt
$
3) Verbose output when copying files
We can enable verbose output using the -v option while copying files. Using the verbose output, we can easily discover what is going on in the background. This is useful when debugging connection, authentication and configuration issues.
$ scp -v jdk-linux-x64_bin.rpm [email protected]:/opt
Executing: program /usr/bin/ssh host 172.20.10.8, user root, command scp -v -t /opt
OpenSSH_7.8p1, OpenSSL 1.1.1 FIPS 11 Sep 2018
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Reading configuration data /etc/ssh/ssh_config.d/05-redhat.conf
debug1: Reading configuration data /etc/crypto-policies/back-ends/openssh.config
debug1: /etc/ssh/ssh_config.d/05-redhat.conf line 8: Applying options for *
debug1: Connecting to 172.20.10.8 [172.20.10.8] port 22.
debug1: Connection established.
…………
debug1: Next authentication method: password
[email protected]'s password:
4) Copy multiple files to remote
Use the scp command to copy multiple files to the remote at one time. Specify multiple files separated by spaces, examples are as follows
$ scp install.txt index.html jdk-linux-x64_bin.rpm [email protected]:/mnt
[email protected]'s password:
install.txt 100% 0 0.0KB/s 00:00
index.html 100% 85KB 7.2MB/s 00:00
jdk-linux-x64_bin.rpm 100% 10MB 25.3MB/s 00:00
[pkumar@linuxtechi ~]$
5) Copy files across two remote systems
Using scp command, we can copy files and directories between two remote hosts, suppose we have a local Linux system, which can connect to two remote Linux systems, then from my local system, I can use scp command in this To copy files between two systems, the syntax is as follows:
$scp user@remote_hosts1:/<files_to_transfer> user@remote_host2:/<folder>
An example looks like this:
$ scp [email protected]:~/backup-Oct.zip [email protected]:/tmp
$ ssh [email protected] "ls -l /tmp/backup-Oct.zip"
-rwx------. 1 root root 747438080 Oct 19 12:02 /tmp/backup-Oct.zip
6) Copy files and directories recursively
Use the -r option in the scp command to recursively copy an entire directory from one system to another, as shown below
$ scp -r Downloads [email protected]:/opt
Verify that the download folder is copied to the remote system using the command
$ ssh [email protected] "ls -ld /opt/Downloads"
drwxr-xr-x. 2 root root 75 Oct 19 12:10 /opt/Downloads
$
7) Increase copy speed by enabling compression
We can increase transfer or copy speed by enabling compression with the -C option, which will automatically enable compression at the source and decompression at the destination.
Suppose we need to enable compression when recursively transferring the Downlod directory
$ scp -r -C Downloads [email protected]:/mnt
8) Limit bandwidth when copying
Use ‘-l’ option in scp command to put limit on bandwidth usage while copying. Bandwidth is specified in Kbit/s, example is shown below,
Use the -l option in the SCP command to limit bandwidth usage. The unit is Kbit/s, the example is as follows
$ scp -l 500 jdk-linux-x64_bin.rpm [email protected]:/var
9) Use a different SSH port
In some cases, the ssh port on the target host will be changed, we can use the -P option to specify the ssh port number.
$ scp -P 2022 jdk-linux-x64_bin.rpm [email protected]:/var
10) Preserve permissions, schema and access time when copying
Use the -p option with the scp command to preserve permissions, access times, and modes when copying from source to destination
$ scp -p jdk-linux-x64_bin.rpm [email protected]:/var/tmp
jdk-linux-x64_bin.rpm 100% 10MB 13.5MB/s 00:00
$
11) Copy files in silent mode
Use the -q option with the scp command to suppress ssh's transfer progress, warning, and diagnostic messages. The example is as follows
$ scp -q -r Downloads [email protected]:/var/tmp
12) Use the logo file
In most Linux environments, key-based authentication is preferred. In the scp command, we can use the -i option to specify the identification file or private key file, examples are as follows
$ scp -i my_key.pem -r Downloads [email protected]:/root
13) Use a different ssh configuration file
In some cases you use different network to connect to Linux system, maybe some network is behind proxy server, so in this case we must have different ssh_conig file.
Different ssh configuration files in the scp command are specified by the -F option, examples are as follows
$ scp -F /home/pkumar/new_ssh_config -r Downloads \
[email protected]:/root
[email protected]'s password:
jdk-linux-x64_bin.rpm 100% 10MB 16.6MB/s 00:00
backup-Oct.zip 100% 713MB 41.9MB/s 00:17
index.html 100% 85KB 6.6MB/s 00:00
$
14) Use a different password
By default, scp encrypts files using the AES-128 algorithm. If you want to use another algorithm, use the -c option followed by the algorithm name, example below
$ scp -c 3des-cbc -r Downloads [email protected]:/root
Use the following commands to view the encryption algorithms supported by ssh and scp
$ ssh -Q cipher localhost | paste -d , -s -
3des-cbc,aes128-cbc,aes192-cbc,aes256-cbc,[email protected],\
aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected],\
[email protected]
$