Linux管理员应该熟悉命令行环境。由于在Linux服务器中通常不安装GUI模式。SSH可能是使得Linux管理员通过远程安全方式管理服务器的最流行协议。与SSH命令一起内建的是SCP命令。SCP用于用安全方式在服务器之间复制文件。

SCP命令的基本语法

以下命令将读成在本机上复制一个文件source_file_name到在以用户username登录的destination_host机器上文件夹destination_folder中。

scp source_file_name username@destination_host:destination_folder

在SCP命令中有很多你能够使用的参数。这里是在日常使用中可能使用的参数。

使用-v参数提供scp过程的详细信息

不带参数的基本scp命令将在后台复制文件。除非过程结束或者某个错误发生,否则你看到不任何东西。

你可以使用"-v"参数打印调试信息到屏幕。它可以帮助你调试连接,验证和配置问题。

[root@rockygu test]# scp -v test.tar.gz  root@192.168.50.228:/root/want/
Executing: program /usr/bin/ssh host 192.168.50.228, user root, command scp -v -t /root/want/
OpenSSH_8.0p1, OpenSSL 1.1.1k  FIPS 25 Mar 2021
debug1: Reading configuration data /etc/ssh/ssh_config
...
test.tar.gz                                                                       100% 2951KB 140.8KB/s   00:20
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 3025856, received 3044 bytes, in 21.1 seconds
Bytes per second: sent 143240.0, received 144.1
debug1: Exit status 0

提供来自源文件的修改时间,访问时间和模式

"-p"参数将做这件事。一个预计时间和连接速度将在屏幕上出现。

[root@rockygu test]# scp -p test.tar.gz  root@192.168.50.228:/root/want/
root@192.168.50.228's password:
Permission denied, please try again.
root@106.13.171.84's password:
test.tar.gz                                                                                                            100% 2951KB 141.9KB/s   00:20
[root@rockygu test]# ls -l test.tar.gz
-rw-r--r-- 1 root root 3021419 Jul  3 09:04 test.tar.gz

在目标位置上:

-bash-4.2# ls -l
total 2952
-rw-r--r-- 1 root root 3021419 Jul  3 09:04 test.tar.gz

使用-C参数使得文件传输更快

能够加速你文件传输的其中一个参数是"-C"参数。"-C"参数将在工作前压缩你的文件。唯一的事情是只在网络中压缩。当文件到达目的服务器时,它将变到与压缩发生前的原先大小一样。

看一下以下命令。它使用一个17MB的文件。

[root@rockygu test]# scp -pv base.tar 192.168.50.228:/root/want
Executing: program /usr/bin/ssh host  192.168.50.228, user (unspecified), command scp -v -p -t /root/want
...
Transferred: sent 17318804, received 5296 bytes, in 123.0 seconds
Bytes per second: sent 140797.3, received 43.1
debug1: Exit status 0

不带"-C"参数复制文件将产生123.0秒。你把这个结果与使用"-C"参数的命令做比较。

[root@rockygu test]# scp -Cpv base.tar  192.168.50.228:/root/want
Executing: program /usr/bin/ssh host  192.168.50.228, user (unspecified), command scp -v -p -t /root/want
...
Transferred: sent 3050816, received 5284 bytes, in 21.2 seconds
Bytes per second: sent 144019.6, received 249.4
debug1: Exit status 0
debug1: compress outgoing: raw data 17311149, compressed 3038822, factor 0.18
debug1: compress incoming: raw data 1867, compressed 1205, factor 0.65

如你所见,当你使用压缩时,传输过程在XX秒完成。它比不使用"-C"参数快XX倍。如果你在网络中复制大量文件,"-C"参数将帮助你减少你需要的整个时间。

我们应该注意的事情是压缩方法不是对所有文件有效。当源文件已经被压缩时,你将在那里没有改善。诸如.zip, .rar, .iso文件将不受"-C"参数影响。

更改SCP密码来加密文件

默认SCP使用"AES-128"来加密文件。如果你想要另一个密码来加密它,你可以使用"-c"参数。看一下以下命令。

[root@rockygu test]# scp -c aes256-ctr  test.tar.gz  root@192.168.50.228:/root/want/
root@192.168.50.228's password:
test.tar.gz                                                                       100% 2951KB 142.3KB/s   00:20

以上命令告诉SCP使用aes256-ctr算法加密文件。请注意这个参数使用"-c"不是"-C"。

限制于SCP命令一起的带宽使用

另一个有用的参数是"-l"参数。"-l"参数将限制使用的带宽。如果你写了一个复制大量文件的自动化脚本,但你不想带宽被SCP进程耗尽,这个参数是有用的。

[root@rockygu test]# scp -l 80 test.tar.gz  root@192.168.50.228:/root/want/
192.168.50.228's password:
test.tar.gz                                                                       100% 2951KB  10.0KB/s   04:54

在-l参数后面的80值表示我们限制SCP进程的带宽只有10KB/s。要记住的一件事情是用Kilobits/sec(kbps)指定带宽。它表示8比特等于1字节。

SCP要以KB/s计数。所以你想要限制SCP最大带宽只有10KB/s,你需要设置它为10*8=80。

为与SCP一起使用指定特定端口

通常,SCP是使用22作为默认端口。但出于安全原因,你会更改这个端口为另一个端口号。例如,我们使用6666。则命令应该像这样:

[blctrl@areadetector ~]$ scp -P 6666 edm-master.zip  root@192.168.50.74:/root
root@192.168.50.74's password:
edm-master.zip                                                                    100% 3320KB  52.4MB/s   00:00

由于"p"已经用于保留时间和模式,确认它使用大写"P"。

在目录中递归复制文件

有时我们需要复制目录以及在它中的所有文件/目录。如果我们在一条命令中做它将更好。SCP使用"-r"选项支持那种场景。

[root@rockygu ~]# scp -r test root@106.13.171.84:/root/want
root@106.13.171.84's password:
guid.txt                                                                          100%    0     0.0KB/s   00:00
perm.txt                                                                          100%    0     0.0KB/s   00:00
suid.txt                                                                          100%    0     0.0KB/s   00:00
.hidden                                                                           100%    0     0.0KB/s   00:00
rocky.sh                                                                          100%    0     0.0KB/s   00:00
rock.html                                                                         100%    0     0.0KB/s   00:00
rocky.c                                                                           100%    0     0.0KB/s   00:00
Rock.txt                                                                          100%    5     0.1KB/s   00:00
1.txt                                                                             100%    0     0.0KB/s   00:00
2.txt                                                                             100%    0     0.0KB/s   00:00
3.txt                                                                             100%    0     0.0KB/s   00:00
4.txt                                                                             100%    0     0.0KB/s   00:00
5.txt                                                                             100%    0     0.0KB/s   00:00
test.tar.gz                                                                       100% 2951KB 141.7KB/s   00:20

当复制过程结束时,你将在目标服务器上找到一个带有其所有文件的名为"test"目录。将自动创建文件夹"test"。

禁止进度条和警告/诊断信息

如果你选择不看来自SCP的进度条和警告/诊断消息,你可以使用"-q"参数禁用它。这是示例:

[root@rockygu ~]# scp -q test/test.tar.gz  root@106.13.171.84:/root/want
root@106.13.171.84's password:
[root@rockygu ~]#

如你所见,在你输入密码后,没有有关SCP进度的信息。在这个过程结束后,你将再次看到一个提示。

Logo

更多推荐