Linux Scp Command Usage With Eamples
What is Scp? Scp is acronym for Secure Copy. It is used primarily for copying files over ssh connection. The copied files are encrypted and assumed secure as ssh. If the files contains sensitive data copying them with scp or similar encrypted protocol is the best solution. In this tutorial we will look how to use scp in a detailed manner.
Scp is provided by ssh packages in Linux distributions. So to install
scp we will install ssh but ssh is generally installed by default.
Ubuntu, Debian, Mint, Kali
$ sudo apt install ssh -y
$ sudo dnf install ssh -y
To get a simple and fast help run scp without any parameter like below.
To get more detailed help man scp command can be used like below. Scp Unix usage is not different from Linux Scp usage.
$ man scp
Copy Files With Scp
We will copy files scp. While copying files scp will ask for credentials.
$ scp firstname.lastname@example.org:/home/ismail/Downloads/backup.tar.gz .
We have copied backup.tar.gz by providing
ismail user from local system to the current directory with . It is not asked for a password because I have already copied required credentials.
Copy Ssh Credentials
We will create a key pair.
After creating key pair we will deplot public key with the following command to the remote system by providing user. It will ask password for the user. After deployment we can use ssh without password.
$ ssh-copy-id root@localhost
Copy Local File To The Remote With Scp
We will copy local file to the remote. As you will see there is no difference from copying from remote to local.
$ scp backup.tar.gz email@example.com:/home/ismail/Downloads
Ok there is no problem. original-ks.config is local file and path but /root/original-ks.config is the remote file and path. In root@localhost root is the user and localhost is the server.
Copy Folders Recursively With Scp
Up to now we have copied single files from remote. To copy folders with sub-folder can be done with recursive parameter. Recursive parameter makes to copy entire directories recursively. We will use
$ scp -r Downloads 192.168.122.233:/home/ismail/Documents
Compress Files While Copying With Scp
Scp encrypts files while copying and this makes some performance loss for the transmission. We can eliminate this performance loss by compressing files. Keep in mind that files are compressed for transfer not entirely. We will use
-C option for compression.
$ scp -C original-ks.cfg root@localhost:/root/original-ks.cfg
By providing -C parameter we have enabled compression for the transfer.
Use Different Ssh Port For Scp
Ssh runs by default tcp/22 but sometimes it may be different from that. Scp assumes the port of ssh 22 too. We can change port like below. We will provide the port name with the
-P option and the port number.
$ scp -P 2222 original-ks.cfg root@localhost:/root/original-ks.cfg
We have provided port number with -P parameter and the value 2222 .
Passing Password To Scp
We have already setup passwordless key based authentication but we may need to use scp with password. In this case we can provide password with simple app named sshpass to the scp like below.
$ dnf install sshpass
We have installed it. Because it is separate from ssh tools.
$ sshpass -p "123456" scp original-ks.cfg root@localhost:/root/original-ks.cfg
Here -p for the password and “123456” is our password. Note that our password is not strong and it is easily hackable. Then we provide regular scp command.
How Escape Spaces In The Path With Scp
If there is spaces in the local or remote path it may become problem. Say we have a folder named test 1 . There is two solution for this
$ scp original-ks.cfg root@localhost:/root/Downloads/<strong>test\\\ 1</strong>/
We have simple add \\ before space in the folder name.
$ scp original-ks.cfg root@localhost:"'/root/Downloads/test 1/'"
We have added single and double quote around path like above.
As we know scp uses some encryption algorithms. Scp by default uses Triple-DES to encrypt network traffic. Triple-DES can cause some performance bottlenecks so more fast Blowfish or aes128-ctr encryption algorithm usage will make transfers faster.
$ scp -c aes128-ctr tmux.tar ubu1:/home/ismail/backup/