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.

Install

Scp is provided by ssh packages in linux distributions. So to install scp we will install ssh but ssh is generally installed by default.

1

$dnf install ssh-y

Scp Syntax

To get a simple and fast help run scp without any parameter like below.

1

2

3

4

$scp

usage:scp[-12346BCpqrv][-ccipher][-Fssh_config][-iidentity_file]

[-llimit][-ossh_option][-Pport][-Sprogram]

[[user@]host1:]file1...[[user@]host2:]file2

Man Scp

To get more detailed help man scp command can be used like below. Scp Unix usage is not different from Linux Scp usage.

1

$man scp

Man Scp

Copy Files With Scp

We will copy files scp. While copying files scp will ask for credentials.

1

2

$scp root@localhost:/root/original-ks.cfg.

original-ks.cfg100%79797.8KB/s00:00

We have copied original-ks.cfg by providing root user from localhost 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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

$ssh-keygen

Generating public/privatersa key pair.

Enter file inwhich tosave the key(/root/.ssh/id_rsa):

/root/.ssh/id_rsa already exists.

Overwrite(y/n)?y

Enter passphrase(empty forno passphrase):

Enter same passphrase again:

Your identification has been saved in/root/.ssh/id_rsa.

Your publickey has been saved in/root/.ssh/id_rsa.pub.

The key fingerprint is:

SHA256:o7XAQex8mVQ6sdAsVRtr4SyYeSqlRQLCQNClMsQRrhE root@poftut3

The key'srandomart image is:

+---[RSA2048]----+

|E*+oo.++oo=|

|o=..+o=+*=|

|+..oBo*o*|

|.+=o+++|

|.o+.S|

|.+o|

|..|

||

||

+----[SHA256]-----+

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.

Copy Folders Recursively With Scp

Up to now we have copied single files from remote. To copy folders with subfolder can be done with recursive parameter. Recursive parameter makes to copy entire directories recursively.

1

2

3

4

5

6

7

$scp-rroot@localhost:/root/.

times.json100%290.0KB/s00:00

webapps.json100%20.0KB/s00:01

key3.db100%16KB16.0KB/s00:00

sessionCheckpoints.json100%2880.3KB/s00:00

permissions.sqlite100%96KB96.0KB/s00:00

search.json.mozlz4100%23KB22.7KB/s00:00

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.

1

2

$scp-Coriginal-ks.cfgroot@localhost:/root/original-ks.cfg

original-ks.cfg100%79797.8KB/s00:00

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.

Scp Performance

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.