14 SCP Command Examples to Securely Transfer Files in Linux

SCP (Secure Copy) is command line tool in Linux and Unix like systems which is used to transfer files and directories across the systems securely over the network. When we use scp command to copy files and directories from our local system to remote system then in the backend it makes ssh connection to remote system. In other words, we can say scp uses the same SSH security mechanism in the backend, it needs either password or keys for authentication.

scp-command-examples-linux

In this tutorial we will discuss 14 useful Linux scp command examples.

Syntax of scp command:

# scp <options> <files_or_directories> [email protected]_host:/<folder>

# scp <options> [email protected]_host:/files   <folder_local_system>

First syntax of scp command demonstrate how to copy files or directories from local system to target host under the specific folder.

Second syntax of scp command demonstrate how files from target host is copied into local system.

Some of the most widely used options in scp command are listed below,

  •  -C         Enable Compression
  •  -i           identity File or private key
  •  -l           limit the bandwidth while copying
  •  -P          ssh port number of target host
  •  -p          Preserves permissions, modes and access time of files while copying
  •  -q          Suppress warning message of SSH
  •   -r          Copy files and directories recursively
  •   -v          verbose output

Let’s jump into the examples now!!!!

Example:1) Copy a file from local system to remote system using scp

Let’s assume we want to copy jdk rpm package from our local Linux system to remote system (172.20.10.8) using scp command, use the following command,

[[email protected] ~]$ 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
[[email protected] ~]$

Above command will copy jdk rpm package file to remote system under /opt folder.

Example:2) Copy a file from remote System to local system using scp

Let’s suppose we want to copy a file from remote system to our local system under the /tmp folder, execute the following scp command,

[[email protected] ~]$ scp [email protected]:/root/Technical-Doc-RHS.odt /tmp
[email protected]'s password:
Technical-Doc-RHS.odt                         100% 1109KB  31.8MB/s   00:00
[[email protected] ~]$ ls -l /tmp/Technical-Doc-RHS.odt
-rwx------. 1 pkumar pkumar 1135521 Oct 19 11:12 /tmp/Technical-Doc-RHS.odt
[[email protected] ~]$

 Example:3) Verbose Output while transferring files using scp (-v)

In scp command, we can enable the verbose output using -v option, using verbose output we can easily find what exactly is happening in the background. This becomes very useful in debugging connection, authentication and configuration problems.

[email protected] ~]$ 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:

Example:4) Transfer multiple files to remote system

Multiple files can be copied / transferred to remote system using scp command in one go, in scp command specify the multiple files separated by space, example is shown below

[[email protected] ~]$ 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
[[email protected] ~]$

Example:5) Transfer files across two remote hosts

Using scp command we can copy files and directories between two remote hosts, let’s suppose we have a local Linux system which can connect to two remote Linux systems, so from my local linux system I can use scp command to copy files across these two systems,

Syntax:

# scp [email protected]_hosts1:/<files_to_transfer>  [email protected]_host2:/<folder>

Example is shown below,

# 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

Example:6) Copy files and directories recursively (-r)

Use -r option in scp command to recursively copy the entire directory from one system to another, example is shown below,

[[email protected] ~]$ scp -r Downloads [email protected]:/opt

Use below command to verify whether Download folder is copied to remote system or not,

[[email protected] ~]$ ssh [email protected] "ls -ld /opt/Downloads"
drwxr-xr-x. 2 root root 75 Oct 19 12:10 /opt/Downloads
[[email protected] ~]$

Example:7) Increase transfer speed by enabling compression (-C)

In scp command, we can increase the transfer speed by enabling the compression using -C option, it will automatically enable compression at source and decompression at destination host.

[email protected] ~]$ scp -r -C Downloads [email protected]:/mnt

In the above example we are transferring the Download directory with compression enabled.

Example:8) Limit bandwidth while copying ( -l )

Use ‘-l’ option in scp command to put limit on bandwidth usage while copying. Bandwidth is specified in Kbit/s, example is shown below,

[[email protected] ~]$ scp -l 500 jdk-linux-x64_bin.rpm  [email protected]:/var

Example:9) Specify different ssh port while scp ( -P)

There can be some scenario where ssh port is changed on destination host, so while using scp command we can specify the ssh port number using ‘-P’ option.

[[email protected] ~]$ scp -P 2022 jdk-linux-x64_bin.rpm  [email protected]:/var

In above example, ssh port for remote host is “2022”

Example:10) Preserves permissions, modes and access time of files while copying (-p)

Use “-p” option in scp command to preserve permissions, access time and modes while copying from source to destination

[[email protected] ~]$ scp -p jdk-linux-x64_bin.rpm  [email protected]:/var/tmp
jdk-linux-x64_bin.rpm                            100%   10MB  13.5MB/s   00:00
[[email protected] ~]$

Example:11) Transferring files in quiet mode ( -q) in scp

Use ‘-q’ option in scp command to suppress transfer progress, warning and diagnostic messages of ssh. Example is shown below,

[[email protected] ~]$ scp -q -r Downloads [email protected]:/var/tmp
[[email protected] ~]$

Example:12) Use Identify file in scp while transferring ( -i )

In most of the Linux environments, keys-based authentication is preferred. In scp command we specify the identify file or private key file using ‘-i’ option, example is shown below,

[[email protected] ~]$ scp -i my_key.pem -r Downloads [email protected]:/root

In above example, “my_key.pem” is the identity file or private key file.

Example:13) Use different ‘ssh_config’ file in scp ( -F)

There are some scenarios where you use different networks to connect to Linux systems, may be some network is behind proxy servers, so in that case we must have different ssh_config file.

Different ssh_config file in scp command is specified via ‘-F’ option, example is shown below

[[email protected] ~]$ 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
[[email protected] ~]$

Example:14) Use Different Cipher in scp command (-c)

By default, scp uses ‘AES-128’ cipher to encrypt the files. If you want to use another cipher in scp command then use ‘-c’ option followed by cipher name,

Let’s suppose we want to use ‘3des-cbc’ cipher in scp command while transferring the files, run the following scp command

[[email protected] ~]# scp -c 3des-cbc -r Downloads [email protected]:/root

Use the below command to list ssh and scp ciphers,

[[email protected] ~]# 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]
[[email protected] ~]#

That’s all from this tutorial, to get more details about scp command, kindly refer its man page. Please do share your feedback and comments in comments section below.

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest