网站搜索

SCP Linux 命令 – 在 Linux 中安全传输文件


Linux 管理员应该熟悉命令行环境。由于Linux服务器中的GUI图形用户界面)模式并不常见。

SSH 可能是最流行的协议,使 Linux 管理员能够以远程安全的方式管理服务器。内置SSH命令有SCP命令,用于以安全的方式在服务器之间复制文件。

SCP命令的基本语法

以下命令将读取为使用“用户名”将“source_file_name”复制到“destination_host”处的“destination_folder” “ 帐户。

scp source_file_name username@destination_host:destination_folder

SCP 命令中有许多参数可供使用。以下是日常使用中可能用到的参数。

在 Linux 中安全传输文件

不带参数的基本 SCP 命令将在后台复制文件。除非该过程完成或出现一些错误,否则用户将看不到任何内容。

您可以使用“-v”参数将调试信息打印到屏幕上。它可以帮助您调试连接、身份验证和配置问题。

将文件从本地主机复制到远程服务器

以下命令将文件“scp-cheatsheet.pdf”从本地复制到远程 Linux 系统的 /home/howtoing 目录下。

scp -v scp-cheatsheet.pdf [email :/home/tecmint/.

示例输出

Executing: program /usr/bin/ssh host 192.168.0.183, user tecmint, command scp -v -t /home/tecmint/.
OpenSSH_8.2p1 Ubuntu-4ubuntu0.5, OpenSSL 1.1.1f  31 Mar 2020
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to 192.168.0.183 [192.168.0.183] port 22.
debug1: Connection established.
debug1: identity file /home/tecmint/.ssh/id_rsa type -1
debug1: identity file /home/tecmint/.ssh/id_rsa-cert type -1
debug1: identity file /home/tecmint/.ssh/id_dsa type -1
debug1: identity file /home/tecmint/.ssh/id_dsa-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa_sk type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ed25519 type -1
debug1: identity file /home/tecmint/.ssh/id_ed25519-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ed25519_sk type -1
debug1: identity file /home/tecmint/.ssh/id_ed25519_sk-cert type -1
debug1: identity file /home/tecmint/.ssh/id_xmss type -1
...

将文件从远程主机复制到本地主机

以下命令将文件“ssh-cheatsheet.pdf”从远程主机复制到本地系统的/home/howtoing目录下。

scp -v [email :/home/ravi/ssh-cheatsheet.pdf /home/tecmint/.

示例输出

Executing: program /usr/bin/ssh host 192.168.0.183, user tecmint, command scp -v -f /home/ravi/ssh-cheatsheet.pdf
OpenSSH_8.2p1 Ubuntu-4ubuntu0.5, OpenSSL 1.1.1f  31 Mar 2020
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to 192.168.0.183 [192.168.0.183] port 22.
debug1: Connection established.
debug1: identity file /home/tecmint/.ssh/id_rsa type -1
debug1: identity file /home/tecmint/.ssh/id_rsa-cert type -1
debug1: identity file /home/tecmint/.ssh/id_dsa type -1
debug1: identity file /home/tecmint/.ssh/id_dsa-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa_sk type -1
debug1: identity file /home/tecmint/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ed25519 type -1
debug1: identity file /home/tecmint/.ssh/id_ed25519-cert type -1
debug1: identity file /home/tecmint/.ssh/id_ed25519_sk type -1
...

将文件从远程主机复制到另一台主机

以下命令将文件“ssh-cheatsheet.pdf”从远程主机复制到 /home/howtoing 目录下的另一个远程主机系统。

scp -v [email :/home/ravi/ssh-cheatsheet.pdf [email :/home/anusha/.

复制带有原始创建日期和时间的文件

-p”参数将在复制文件时保留文件的原始修改和访问时间,并且屏幕上会显示预计时间和连接速度。

scp -p scp-cheatsheet.pdf [email :/home/tecmint/.

示例输出

[email 's password: 
scp-cheatsheet.pdf                                                                                                                                                                 100%  531   721.4KB/s   00:00    

复制文件时进行 scp 压缩

可以加快文件传输速度的参数之一是“-C”参数,该参数用于随时随地压缩文件。独特之处在于压缩仅发生在网络中。当文件到达目标服务器时,它将恢复到压缩前的原始大小。

看看这些命令。它使用 93 Mb 的单个文件。

scp -pv messages.log [email :.

示例输出

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 '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

复制不带“-C”参数的文件将导致 1661.3 秒。您可以使用“-C”参数将结果与下面的命令进行比较。

scp -Cpv messages.log [email :.

示例输出

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 '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

如您所见,当您使用压缩时,传输过程将在 162.5 秒内完成。比不使用“-C”参数快10倍。如果您要通过网络复制大量文件,“-C”参数将帮助您减少所需的总时间。

我们应该注意的是,压缩方法不适用于任何文件。当源文件已经压缩时,您不会发现任何改进。 .zip.rar图片.iso等文件不会受到“ -C”参数。

更改 SCP 密码以加密文件

默认情况下,SCP 使用“AES-128”来加密文件。如果你想换成其他密码来加密,可以使用“-c”参数。

看看这个命令。

scp -c 3des Label.pdf [email :.

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

上面的命令告诉SCP使用3des算法来加密文件。请注意该参数使用“-c”而不是“-C”。

使用 SCP 命令限制带宽使用

另一个可能有用的参数是“-l”参数。 “-l”参数将限制要使用的带宽。如果您使用自动化脚本来复制大量文件,但您不希望 SCP 进程耗尽带宽,那么这将很有用。

scp -l 400 Label.pdf [email :.

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

-l”参数后面的400值意味着我们将SCP进程的带宽限制为仅50 KB/秒。

要记住的一件事是,带宽以千位/ (kbps) 为单位指定。这意味着8位等于1字节

SCP 的单位为千字节/ (KB/s)。因此,如果您想将带宽限制为 SCP 最大值仅为 50 KB/s,则需要将其设置为 50 x 8=400

具有不同端口的 SCP

通常,SCP 使用端口 22 作为默认端口,但出于安全原因,您可以将该端口更改为其他端口。例如,我们使用端口2249

那么命令应该是这样的。

scp -P 2249 Label.pdf [email :.

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

确保它使用大写“P”而不是“p”,因为“p”已用于保留时间和模式。

SCP – 递归复制文件和目录

有时我们需要复制目录及其中的所有文件/目录。如果我们可以使用“-r”参数在单个命令中完成此操作,该参数会递归地复制整个目录,那就更好了。

scp -r documents [email :.

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

复制过程完成后,在目标服务器上您将找到一个名为“documents”的目录及其所有文件。将自动创建文件夹“documents”。

SCP – 禁用进度消息

如果您选择不查看来自 SCP 的进度表和警告/诊断消息,您可以使用“-q”参数禁用它。这是一个例子。

scp -q Label.pdf [email :.

[email 's password:
pungki@mint ~/Documents $

可以看到,输入密码后,并没有关于SCP进程的信息。该过程完成后,您将再次看到提示。

SCP – 使用代理复制文件

代理服务器通常用于办公环境。本质上,SCP 不是配置的代理。当您的环境使用代理时,您必须“告诉”SCP 与代理进行通信。

这是场景。代理地址为10.0.96.6,代理端口为8080。代理还实现了用户身份验证。首先,您需要创建“~/.ssh/config” 文件。其次,您将此命令放入其中。

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

然后您需要创建包含以下内容的文件“~/.ssh/proxyauth”。

myusername:mypassword

之后,您可以像往常一样透明地进行SCP。

请注意,您的系统上可能尚未安装开瓶器。在我的 Linux Mint 上,我需要首先使用标准 Linux Mint 安装过程来安装它。

apt-get install corkscrew

对于其他基于 yum 的系统,用户可以使用以下 yum 命令安装 corkscrew。

yum install corkscrew

另一件事是,由于“~/.ssh/proxyauth”文件包含您的明文格式的“用户名”和“密码” ,请确保该文件只能由您访问。

选择不同的 ssh_config 文件

对于经常在公司网络和公共网络之间切换的移动用户来说,总是要更改SCP中的设置将会很痛苦。如果我们可以放置不同的 ssh_config 文件来满足我们的需求,那就更好了。

代理用于公司网络,但不用于公共网络,并且您会定期切换网络。

scp -F /home/pungki/proxy_ssh_config Label.pdf

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

默认情况下,每个用户的“ssh_config”文件将放置在“~/.ssh/config”中。创建具有代理兼容性的特定“ssh_config”文件将使网络之间的切换变得更加容易。

当您在公司网络时,可以使用“-F”参数。当您在公共网络上时,可以跳过“-F”参数。

这就是SCP的全部内容。您可以查看SCP手册页了解更多详细信息。请随时留下意见和建议。