10个在Linux中传输文件/文件夹的SCP命令

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

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

SCP命令的基本语法

下面的命令将被解释为将“源文件名”复制到“目标文件夹”在“目标主机”使用“用户名”账户。

scp source_file_name username@destination_host:destination_folder

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

在Linux中安全传输文件

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

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

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

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

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

示例输出:

SCP – Copy File to Remote Linux Server
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/tecmint目录中。

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

示例输出:

SCP – Copy File to Local System
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/tecmint目录中。

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

复制文件时保留原始创建日期和时间

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

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

示例输出:

SCP – Preserve File Timestamps
[email protected]'s password: 
scp-cheatsheet.pdf                                                                                                                                                                 100%  531   721.4KB/s   00:00    

复制文件时使用Scp压缩

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

请看以下命令。它使用了一个93 Mb的单个文件。

$ scp -pv messages.log [email protected]:.

示例输出:

SCP Transfers File 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

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

$ scp -Cpv messages.log [email protected]:.

示例输出:

SCP – Transfers File 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

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

我们应该注意的是,压缩方法并不适用于所有文件。当源文件已经压缩时,您将不会在那里找到任何改进。诸如.zip.rar图片.iso文件等将不会受到“-C”参数的影响。

更改SCP加密文件的密码

默认情况下,SCP使用“AES-128”加密文件。如果您想更改使用另一种密码加密,可以使用“-c”参数。

请看以下命令。

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

[email protected]'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 protected]:.

[email protected]'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 x8 = 400

使用不同端口的SCP

通常,SCP使用端口22作为默认端口,但出于安全原因,您可能将端口更改为另一个端口。例如,我们使用端口2249

那么命令应该像这样。

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

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

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

SCP -递归复制文件和目录

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

$ 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

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

SCP–禁用进度消息

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

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

[email protected]'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操作。

请注意,corkscrew可能尚未安装在您的系统上。在我的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 protected]:.
[email protected]'s password:
Label.pdf 100% 3672KB 282.5KB/s 00:13

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

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

关于SCP的内容就到这里。您可以查看SCP手册页以获取更多详细信息。请随时留下评论和建议。

Source:
https://www.tecmint.com/scp-commands-examples/