Linux Scp Command Usage With Examples

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-scp-command-usage-with-examples/
PINTEREST
PINTEREST
INSTAGRAM

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.

Ubuntu, Debian, Mint, Kali

scp is provided with the ssh package.

Fedora,CentOS, RHEL

We will use dnf or yum package manager.

OR

Scp Syntax

To get a simple and fast help run SCP without any parameter like below. As we can see we use scp command and provide options related with the connection. The we will provide username and remote host name with @ delimiter.

Scp Syntax
Scp Syntax

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.

Man Scp
Man Scp

Copy Files With Scp

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

We have copied backup.tar.gz by providing ismail user from local system to the /home/aliIt is not asked for a password because I have already copied required credentials.

LEARN MORE  How To Download and Upload Directories Recursively In Linux

Passwordless scp

If we want to use scp in a passwordless and more secure manner we need to setup SSH Key based authentication. In order to setup key based authentication we need to have keys which can be created with the ssh-keygen command like below.

After creating key pair we will deploy 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 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.

Copy Files With Scp
Copy Files With Scp

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 -r option.

Copy Folders Recursively With Scp
Copy Folders Recursively With Scp

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.

LEARN MORE  What is chmod 777

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.

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.

We have installed it. Because it is separate from ssh tools.

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

We have simple add \\ before space in the folder name.

LEARN MORE  How To Solve ssh-copy-id "ERROR: failed to open ID file" Error

We have added single and double quote around path like above.

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.

Scp Performance
Scp Performance

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-scp-command-usage-with-examples/
PINTEREST
PINTEREST
INSTAGRAM