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.
In this tutorial we will discuss 14 useful Linux scp command examples.
Syntax of scp command:
# scp <options> <files_or_directories> user@target_host:/<folder>
# scp <options> user@target_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,
[pkumar@linuxtechi ~]$ scp jdk-linux-x64_bin.rpm root@172.20.10.8:/opt
root@172.20.10.8's password:
jdk-linux-x64_bin.rpm 100% 10MB 27.1MB/s 00:00
[pkumar@linuxtechi ~]$
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,
[pkumar@linuxtechi ~]$ scp root@172.20.10.8:/root/Technical-Doc-RHS.odt /tmp root@172.20.10.8's password: Technical-Doc-RHS.odt 100% 1109KB 31.8MB/s 00:00 [pkumar@linuxtechi ~]$ ls -l /tmp/Technical-Doc-RHS.odt -rwx------. 1 pkumar pkumar 1135521 Oct 19 11:12 /tmp/Technical-Doc-RHS.odt [pkumar@linuxtechi ~]$
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.
pkumar@linuxtechi ~]$ scp -v jdk-linux-x64_bin.rpm root@172.20.10.8:/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
root@172.20.10.8'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
[pkumar@linuxtechi ~]$ scp install.txt index.html jdk-linux-x64_bin.rpm root@172.20.10.8:/mnt
root@172.20.10.8'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 ~]$
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 user@remote_hosts1:/<files_to_transfer> user@remote_host2:/<folder>
Example is shown below,
# scp pkumar@172.20.10.9:~/backup-Oct.zip root@172.20.10.8:/tmp # ssh root@172.20.10.8 "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,
[pkumar@linuxtechi ~]$ scp -r Downloads root@172.20.10.8:/opt
Use below command to verify whether Download folder is copied to remote system or not,
[pkumar@linuxtechi ~]$ ssh root@172.20.10.8 "ls -ld /opt/Downloads"
drwxr-xr-x. 2 root root 75 Oct 19 12:10 /opt/Downloads
[pkumar@linuxtechi ~]$
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.
pkumar@linuxtechi ~]$ scp -r -C Downloads root@172.20.10.8:/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,
[pkumar@linuxtechi ~]$ scp -l 500 jdk-linux-x64_bin.rpm root@172.20.10.8:/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.
[pkumar@linuxtechi ~]$ scp -P 2022 jdk-linux-x64_bin.rpm root@172.20.10.8:/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
[pkumar@linuxtechi ~]$ scp -p jdk-linux-x64_bin.rpm root@172.20.10.8:/var/tmp
jdk-linux-x64_bin.rpm 100% 10MB 13.5MB/s 00:00
[pkumar@linuxtechi ~]$
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,
[pkumar@linuxtechi ~]$ scp -q -r Downloads root@172.20.10.8:/var/tmp
[pkumar@linuxtechi ~]$
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,
[pkumar@linuxtechi ~]$ scp -i my_key.pem -r Downloads root@172.20.10.8:/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
[pkumar@linuxtechi ~]$ scp -F /home/pkumar/new_ssh_config -r Downloads root@172.20.10.8:/root
root@172.20.10.8'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
[pkumar@linuxtechi ~]$
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
[root@linuxtechi ~]# scp -c 3des-cbc -r Downloads root@172.20.10.8:/root
Use the below command to list ssh and scp ciphers,
[root@linuxtechi ~]# ssh -Q cipher localhost | paste -d , -s -
3des-cbc,aes128-cbc,aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com,chacha20-poly1305@openssh.com
[root@linuxtechi ~]#
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.
from Linuxtechi https://ift.tt/2J82CJB
0 Comments