Home > Article > System Tutorial > 10 scp commands to transfer file folders in Linux
The following command means Copy source_file_name
to destination_folder
under username account
of destination_host
.
> scp source_file_name username@destination_host:destination_folder
copy
There are many parameters that can be used in the scp
command. The following are some parameters that may be used in daily operations.
-v
parameter to display detailed information of the scp processThe default scp
command takes no parameters and will copy files silently in the background. Users only see the results when the process is complete or when an error occurs.
You can use the -v
parameter to output debugging information to the screen. This can help you troubleshoot connectivity, authentication, and configuration issues.
rumenz@local $ scp -v Label.pdf rumenz@192.168.1.110:.
scp displays 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 rumenz@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
-p
parameter will help you solve this problem. An estimated time and connection speed will appear on the screen.
rumenz@local $ scp -p Label.pdf rumenz@192.168.1.110:.
scp Estimate the time required to copy large files
> rumenz@202.x.x.x's password: Label.pdf 100% 3672KB 126.6KB/s 00:29
One of the parameters that can speed up file transfers is the -C
range. This -C
parameter will compress your files anytime and anywhere, the unique thing is that compression only happens on the network. When the file reaches the destination server, it is restored to its original size before compression.
> rumenz@local $ scp -pv messages.log rumenz@192.168.1.110:.
scp Transfer 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 rumenz@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
-C
Allow compression
rumenz@local $ scp -Cpv messages.log rumenz@192.168.1.110:.
scp Transfer files faster using 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 rumenz@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
If you are copying a large number of files over the network, the -C
parameter will help you reduce the total time required.
压缩方法不适用于所有文件。当源文件已经被压缩时,就没有什么效果了。文件如.zip
,.rar
,pictures
, 和.iso
默认情况下scp
使用 AES-128
来加密文件。如果你想更改为其他密码对其进行加密,你可以使用 -c
范围。看看这个命令。
rumenz@local $ scp -c 3des Label.pdf rumenz@192.168.1.110:. rumenz@202.x.x.x's password: Label.pdf 100% 3672KB 282.5KB/s 00:13
上面的命令告诉scp
使用3des algorithm
来加密文件。请注意此参数使用 -c
不是-C
。
另一个可能有用的参数是 -l
范围。这 -l
参数将限制使用的带宽制 。如果你执行自动化脚本来复制大量文件,这将很有用
rumenz@local $ scp -l 400 Label.pdf rumenz@192.168.1.110:. rumenz@202.x.x.x's password: Label.pdf 100% 3672KB 50.3KB/s 01:13
scp 默认带宽的单位是Kilobyte/sec
(KB/s
)。所以如果你想限制你的带宽scp
最多只有50 KB/s
,你需要将其设置为50 x 8
=400
.
scp
正在使用端口22
作为默认端口。但出于安全原因,你可以将端口更改为另一个端口. 例如,我们使用端口2249
.
rumenz@local $ scp -P 2249 Label.pdf rumenz@192.168.1.110:. rumenz@202.x.x.x's password: Label.pdf 100% 3672KB 262.3KB/s 00:14
有时我们需要复制目录和其中的所有文件
,目录
。
rumenz@local $ scp -r documents rumenz@192.168.1.110:. rumenz@202.x.x.x's password: Label.pdf 100% 3672KB 282.5KB/s 00:13 scp.txt 100% 10KB 9.8KB/s 00:00
复> 制过程完成后,你将在目标服务器上找到一个名为documents
及其所有文件。文件夹documents
是自动创建的。
rumenz@local $ scp -q Label.pdf rumenz@192.168.1.110:. rumenz@202.x.x.x's password: rumenz@local $
可以看到,输入密码后,没有关于 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
前提是需要安装corkscrew
$ apt-get install corkscrew
Centos系统可以用yum
安装corkscrew
# yum install corkscrew
由于 ~/.ssh/proxyauth
文件包含你的 username
和password
以明文格式,请确保该文件只能自己访问。
对于经常在公司网络和公共网络之间切换的移动用户来说,总是在scp中更改设置会很痛苦。
代理在公司网络中使用,但不在公共网络中使用,并且你定期切换网络。
rumenz@local $ scp -F /home/pungki/proxy_ssh_config Label.pdf rumenz@192.168.1.110:. rumenz@202.x.x.x's password: Label.pdf 100% 3672KB 282.5KB/s 00:13
默认情况下 ssh_config
每个用户的文件将被放置在 ~/.ssh/config
。创建一个特定的 ssh_config
具有代理兼容性的文件将更容易在网络之间切换。
当你在公司网络,你可以使用 -F
范围。当你在公共网络上时,你可以不用-F
参数。
The above is the detailed content of 10 scp commands to transfer file folders in Linux. For more information, please follow other related articles on the PHP Chinese website!