10 SCP commands for transferring files / folders in Linux

s2 0

Linux administrators should be familiar with the CLI Surroundings. Since GUI Mode on Linux servers is not installed frequently. SSH is possibly the most popular protocol that allows Linux administrators to remotely manage the servers in a secure manner. Built with SSH There is an order SCP Command. SCP is used to securely copy files between servers.

[ You might also like: How to Secure and Harden OpenSSH Server ]

10 Linux SCP commands

Basic syntax of the SCP command

The following command is as follows:copy source_file_name“in”Destination folder” at “Target_host“with”Username Account“.

scp source_file_name [email protected]_host:destination_folder

There are many parameters in the SCP Command you can use. Here are the parameters that can be used in everyday use.

Enter the detailed information about the SCP process with the parameter -v

The basics SCP Command without parameters copies the files in the background. Users will not see anything unless the process is complete or an error is displayed.

You can use the … “-v”Parameter to print debug information on the screen. It can help you debug connection, authentication, and configuration problems.

[email protected] ~/Documents $ scp -v Label.pdf [email protected]@202.x.x.x:.
Sample output
SCP show progress when copying files
Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp -v -t .
OpenSSH_6.0p1 Debian-3, OpenSSL 1.0.1c 10 May 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 202.x.x.x [202.x.x.x] port 22.
debug1: Connection established.
debug1: Host '202.x.x.x' is known and matches the RSA host key.
debug1: Found key in /home/pungki/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: Next authentication method: password
[email protected]'s password:
debug1: Authentication succeeded (password).
Authenticated to 202.x.x.x ([202.x.x.x]:22).
Sending file modes: C0770 3760348 Label.pdf
Sink: C0770 3760348 Label.pdf
Label.pdf 100% 3672KB 136.0KB/s 00:27
Transferred: sent 3766304, received 3000 bytes, in 65.2 seconds
Bytes per second: sent 57766.4, received 46.0
debug1: Exit status 0

Enter modification times, access times and modes from original files

The “-P”Parameter will help you. An estimated time and connection speed are displayed on the screen.

[email protected] ~/Documents $ scp -p Label.pdf [email protected]:.
Sample output
SCP estimates the time it takes to copy large files
[email protected]'s password:
Label.pdf 100% 3672KB 126.6KB/s 00:29

Speed ​​up the file transfer with the -C parameter

One of the parameters that can speed up your file transfer is the “-C“-Parameter. The “-C”Parameter compresses your files on the go. The unique thing is that the compression only takes place in the network. When the file arrives at the destination server, it returns to its original size, as it was before it was compressed.

Take a look at these commands. It uses a single file of 93 MB.

[email protected] ~/Documents $ scp -pv messages.log [email protected]:.
Sample output
SCP transfers files without compression
Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp -v -p -t .
OpenSSH_6.0p1 Debian-3, OpenSSL 1.0.1c 10 May 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 202.x.x.x [202.x.x.x] port 22.
debug1: Connection established.
debug1: identity file /home/pungki/.ssh/id_rsa type -1
debug1: Found key in /home/pungki/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: Trying private key: /home/pungki/.ssh/id_rsa
debug1: Next authentication method: password
[email protected]'s password:
debug1: Authentication succeeded (password).
Authenticated to 202.x.x.x ([202.x.x.x]:22).
debug1: Sending command: scp -v -p -t .
File mtime 1323853868 atime 1380425711
Sending file timestamps: T1323853868 0 1380425711 0
messages.log 100% 93MB 58.6KB/s 27:05
Transferred: sent 97614832, received 25976 bytes, in 1661.3 seconds
Bytes per second: sent 58758.4, received 15.6
debug1: Exit status 0

Copying files without “-C”Parameter leads to 1661.3 Seconds. You can compare the result with the following command, which you can use with the “-C “ Parameter.

[email protected] ~/Documents $ scp -Cpv messages.log [email protected]:.
Sample output
SCP transfers files faster with compression
Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp -v -p -t .
OpenSSH_6.0p1 Debian-3, OpenSSL 1.0.1c 10 May 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 202.x.x.x [202.x.x.x] port 22.
debug1: Connection established.
debug1: identity file /home/pungki/.ssh/id_rsa type -1
debug1: Host '202.x.x.x' is known and matches the RSA host key.
debug1: Found key in /home/pungki/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: Next authentication method: publickey
debug1: Trying private key: /home/pungki/.ssh/id_rsa
debug1: Next authentication method: password
[email protected]'s password:
debug1: Enabling compression at level 6.
debug1: Authentication succeeded (password).
Authenticated to 202.x.x.x ([202.x.x.x]:22).
debug1: channel 0: new [client-session]
debug1: Sending command: scp -v -p -t .
File mtime 1323853868 atime 1380428748
Sending file timestamps: T1323853868 0 1380428748 0
Sink: T1323853868 0 1380428748 0
Sending file modes: C0600 97517300 messages.log
messages.log 100% 93MB 602.7KB/s 02:38
Transferred: sent 8905840, received 15768 bytes, in 162.5 seconds
Bytes per second: sent 54813.9, received 97.0
debug1: Exit status 0
debug1: compress outgoing: raw data 97571111, compressed 8806191, factor 0.09
debug1: compress incoming: raw data 7885, compressed 3821, factor 0.48

As you can see, when using compression, the transfer process is done in 162.5 Seconds. it is 10 Times faster than without the “-C“-Parameter. If you copy a lot of files over the network, the “-C”Parameter would help you reduce the total time required.

What we should note is that the compression method doesn’t work for any files. If the source file is already compressed, you won’t see any improvement there. Files like .Postal code, .rar, pictures, and .iso Files are not used by the “-C“-Parameter.

Change the SCP encryption to encrypt files

By default SCP with “AES-128“To encrypt files. If you want to switch to a different encryption method in order to encrypt it, you can use the “-C“-Parameter. Take a look at this command.

[email protected] ~/Documents $ scp -c 3des Label.pdf [email protected]:.

[email protected]'s password:
Label.pdf 100% 3672KB 282.5KB/s 00:13

The above command says SCP to use them 3des algorithm to encrypt the file. Please make sure that this parameter is marked with “-C” not “-C“.

Limiting bandwidth usage with the SCP command

Another useful parameter is the “-l“-Parameter. The “-l”Parameter limits the bandwidth to be used. It is useful when you are running an automation script to copy a lot of files but you don’t want the bandwidth going through the SCP Process.

[email protected] ~/Documents $ scp -l 400 Label.pdf [email protected]:.

[email protected]'s password:
Label.pdf 100% 3672KB 50.3KB/s 01:13

the 400 Value behind the “-l”Parameter means that we have the bandwidth for that SCP just process 50 KB / sec.. Note that the bandwidth is specified in Kilobits/Sec (kbps). That’s mean 8 bit are the same 1 byte.

While SCP counts in kilobyte/Sec (KB / s). So if you want to limit your bandwidth for SCP maximum only 50 KB / s, You have to set it in 50 x 8 = 400.

Specify the specific port to be used with SCP

Usually, SCP uses port 22nd as the standard port. However, for security reasons, you can change the port to another port. For example we use port 2249. Then the command should be like this.

[email protected] ~/Documents $ scp -P 2249 Label.pdf [email protected]:.

[email protected]'s password:
Label.pdf 100% 3672KB 262.3KB/s 00:14

Make sure it uses capital “P.” not “P.” since “P.“Is already used for preserved times and modes.

Recursively copy files to the directory

Sometimes we have to copy the directory and everything Files / directories internally. It’ll be better if we make it in 1 Command. SCP supports this scenario with the “-R“-Parameter.

[email protected] ~/Documents $ scp -r documents [email protected]:.

[email protected]'s password:
Label.pdf 100% 3672KB 282.5KB/s 00:13
scp.txt 100% 10KB 9.8KB/s 00:00

When the copying process is complete, you will find a directory on the destination server called “documents“With all of its files. The folder “documents“Is created automatically.

Deactivate progress display and warning / diagnostic message

If you do not want to see the progress meter and the warning / diagnostic messages from SCP, you can use the “-Q“-Parameter. Here is the example.

[email protected] ~/Documents $ scp -q Label.pdf [email protected]:.

[email protected]'s password:
[email protected] ~/Documents $

As you can see, after entering the password there is no information about the SCP process. After the process is complete, you will see a prompt again.

Copy files with SCP via proxy

The proxy server is usually used in the office environment. SCP is natively not configured as a proxy. If your environment uses a proxy, you must “tell” SCP to communicate with the proxy.

Here is the scenario. The proxy address is 10.0.96.6 and the proxy port is 8080. The proxy also implemented user authentication. First you have to~ / .ssh / config “ File. Second, paste this command into it.

ProxyCommand /usr/bin/corkscrew 10.0.96.6 8080 %h %p ~/.ssh/proxyauth

Then you have to create a file “~ / .ssh / proxyauth” that includes.

myusername:mypassword

You can then carry out SCP transparently as usual.

Please note that the corkscrew may not be installed on your system yet. On my Linux Mint, I need to install it first using the standard Linux Mint installation method.

$ apt-get install corkscrew

On other yum-based systems, users can install Corkscrew with the following yum command.

# yum install corkscrew

Another thing is that since “~ / .ssh / proxyauth“File contains your”Username” and “password“In plain text format, please make sure that the file is only accessible to you.

Choose a different ssh_config file

For mobile users who frequently switch between company networks and public networks, it will be tedious to constantly change the settings in SCP. It is better if we can put another one ssh_config File that meets our requirements.

Here is an example scenario

Proxy is used in the company network but not in the public network and you change networks regularly.

[email protected] ~/Documents $ scp -F /home/pungki/proxy_ssh_config Label.pdf

[email protected]:.
[email protected]'s password:
Label.pdf 100% 3672KB 282.5KB/s 00:13

By default “ssh_config“File per user is saved in”~ / .ssh / config“. Creating a specific “ssh_config”File with proxy compatibility makes it easy to switch between networks.

If you are in the company network, you can use the “-F“-Parameter. If you are on a public network, you can use the “-F“-Parameter.

[ You might also like: Pscp – Transfer/Copy Files to Multiple Linux Servers Using Single Shell ]

That’s all SCP. You can see man pages from SCP for more details. Please don’t hesitate to leave comments and suggestions.

Tags: