Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the SCP transfer Command of SSH in Linux

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "the use of SSH SCP transmission commands in Linux". The explanation in this article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn "the use of SSH SCP transmission commands in Linux".

The Linux system administrator should be familiar with the CLI environment because GUI is not usually installed in the Linux server. SSH is probably the most popular protocol for Linux system administrators to securely manage servers remotely. A command called SCP is built into the SSH command to securely transfer files between servers.

The following command can be interpreted as: use "username account"copy source file name" to "destination folder" on "destination host".

Basic syntax of the SCP command

The code is as follows:

Scp source_file_name username@destination_host:destination_folder

The SCP command has a number of parameters that can be used, which are used every time.

Use the-v parameter to provide details of the SCP process

Basic SCP commands without parameters copy files in the background, and users will not see any prompts on the interface unless the operation is completed or an error occurs. You can use the "- v" parameter to print debugging information on the screen, which can help you debug some problems with connection, authentication, and configuration.

The code is as follows:

Pungki@mint ~ / Documents $scp-v Label.pdf mrarianto@202.x.x.x:.

Partial output

The code is as follows:

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

Mrarianto@202.x.x.x'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

Get the modification time, access time and mode from the source file

The "- p" parameter helps to display the estimated time and connection speed on the screen.

The code is as follows:

Pungki@mint ~ / Documents $scp-p Label.pdf mrarianto@202.x.x.x:.

Partial output

The code is as follows:

Mrarianto@202.x.x.x's password:

Label.pdf 100% 3672KB 126.6KB/s 00:29

Use the-C parameter to make file transfer faster

One parameter that makes transferring files faster is the "- C" parameter, which is used to constantly compress the transferred files. What makes it special is that compression occurs in a network transfer, and when a file is transferred to the target server, it changes back to its original size before compression.

To take a look at these commands, we use a single file of 93 Mb as an example.

The code is as follows:

Pungki@mint ~ / Documents $scp-pv messages.log mrarianto@202.x.x.x:.

Partial output

The code is as follows:

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

Mrarianto@202.x.x.x'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

Instead of using the "- C" parameter to copy the file, it took 1661.3 seconds. You can compare the results after using the "- C" parameter.

The code is as follows:

Pungki@mint ~ / Documents $scp-Cpv messages.log mrarianto@202.x.x.x:.

Partial output

The code is as follows:

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

Mrarianto@202.x.x.x'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

See, after compressing the file, the transfer process is completed in 162.5 seconds, 10 times faster than without the "- C" parameter. If you want to copy many files over the network, the "- C" parameter can save you a lot of time.

One thing we need to note is that this compression method does not apply to all files. When the source file has been compressed, there is no way to compress much more. Such as files like .zip, .rar, pictures and .iso, it doesn't make sense to use the "- C" parameter.

Select a different encryption algorithm to encrypt the file

By default, SCP uses the "AES-128" encryption algorithm to encrypt the transmission. If you want to use another encryption algorithm to encrypt the transmission, you can use the "- c" parameter. Let's see.

The code is as follows:

Pungki@mint ~ / Documents $scp-c 3des Label.pdf mrarianto@202.x.x.x:.

Mrarianto@202.x.x.x's password:

Label.pdf 100% 3672KB 282.5KB/s 00:13

The above command tells SCP to encrypt the file with 3des algorithm. Note that this parameter is "- c" (lowercase) rather than "- C" (uppercase).

Limit bandwidth usage

Another useful parameter is the "- l" parameter, which limits bandwidth usage. This parameter is very useful if you execute an automated script to copy a lot of files and do not want the bandwidth to be exhausted by the SCP process.

The code is as follows:

Pungki@mint ~ / Documents $scp-l 400 Label.pdf mrarianto@202.x.x.x:.

Mrarianto@202.x.x.x's password:

Label.pdf 100% 3672KB 50.3KB/s 01:13

The 400 value after the "- l" parameter means that we limit the bandwidth to 50 KB/ seconds for the SCP process. One thing to keep in mind is that bandwidth is expressed in kilobits per second (kbps), while 8 bits equals 1 byte.

Because SCP is calculated in kilobytes per second (KB/s), if you want to limit the maximum bandwidth of SCP to 50 KB/s, you need to set it to 50 x 8 = 400.

Designated port

Usually SCP uses 22 as the default port. But for security reasons, the SSH listening port is changed to another port. For example, we want to use port 2249, in which case we have to specify a port. The command is as follows.

The code is as follows:

Pungki@mint ~ / Documents $scp-P 2249 Label.pdf mrarianto@202.x.x.x:.

Mrarianto@202.x.x.x's password:

Label.pdf 100% 3672KB 262.3KB/s 00:14

Make sure you write the capital letter "P" instead of "p", because "p" has been used to retain the modification time and mode of the source file (LCTT Note: different from the ssh command).

Recursively copy files and folders

Sometimes we need to copy the folder and all its internal files / subfolders, and it would be better if we could solve the problem with one command. SCP can do this with the "- r" parameter.

The code is as follows:

Pungki@mint ~ / Documents $scp-r documents mrarianto@202.x.x.x:.

Mrarianto@202.x.x.x's password:

Label.pdf 100% 3672KB 282.5KB/s 00:13

Scp.txt 100% 10KB 9.8KB/s 00:00

After the copy is complete, you will find a folder called "documents" on the target server, in which are all the files copied. "documents" is a folder created automatically by the system.

Disable progress bar and warning / diagnostic information

If you don't want to see progress bars and warnings / diagnostics in SCP, you can use the "- Q" parameter to silence them, as an example is.

The code is as follows:

Pungki@mint ~ / Documents $scp-Q Label.pdf mrarianto@202.x.x.x:.

Mrarianto@202.x.x.x's password:

Pungki@mint ~ / Documents $

As you can see, there is no feedback on the progress of SCP after you enter your password. When the progress is finished, you won't see any hints.

Use SCP to copy files through an agent

Proxy servers are often used in office environments, and SCP is naturally not configured in terms of proxies. When your environment is using agents, then you must "tell" SCP to associate with agents.

The scenario is as follows: the address of the agent is 10.0.96.6 and the port is 8080. The agent also implements the function of user authentication. First, you need to create a "~ / .ssh / config" file, and then enter the following command into the file.

The code is as follows:

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

Then you need to create a "~ / .ssh / proxyauth" file that also includes the following command.

The code is as follows:

Myusername:mypassword

Then you can use SCP as usual.

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

The code is as follows:

$apt-get install corkscrew

For other yum-based systems, users can install corkscrew with the following command.

The code is as follows:

# yum install corkscrew

Another point is that because the "~ / .ssh / proxyauth" file contains your "user name" and "password" in clear text, please make sure that only you can view the file.

Select a different ssh_config file

For mobile users who often switch between corporate and public networks, it is obviously painful to change the SCP settings all the time. It would be nice if we could put a ssh_config file with different configurations to match our needs.

Here is a simple scenario

Proxies are used on corporate networks but not public networks and you switch networks on a regular basis.

The code is as follows:

Pungki@mint ~ / Documents $scp-F / home/pungki/proxy_ssh_config Label.pdf

Mrarianto@202.x.x.x:.

Mrarianto@202.x.x.x's password:

Label.pdf 100% 3672KB 282.5KB/s 00:13

By default, each user puts the "ssh_config" file under the "~ / .ssh / config" path. Creating a specific "ssh_config" file with compatible agents makes it easier for you to switch networks.

You can use the "- F" parameter when you are on the corporate network, and you can ignore the "- F" parameter when you are on the public network.

Thank you for your reading, the above is the content of "the use of SSH SCP transmission commands in Linux". After the study of this article, I believe you have a deeper understanding of the use of SSH SCP transmission commands in Linux, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report