搜索
首页系统教程LINUX在 Linux 中传输文件文件夹的 10个 scp 命令

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

Feb 10, 2024 pm 08:30 PM
linuxlinux教程linux系统linux命令外壳脚本嵌入式linuxlinux入门linux学习

scp 命令的基本用法

下面的命令的意思是 把 source_file_name复制到destination_hostusername account下的destination_folder里。

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

> scp source_file_name username@destination_host:destination_folder

复制

有很多参数可以在scp命令中使用。以下是一些在日常操作中可能会用到的参数。

使用-v参数显示 scp 过程的详细信息

默认的scp命令不带参数,会在后台默默地复制文件。用户只有在过程结束或者出现错误时才能看到结果。

你可以使用 -v 参数来把调试信息输出到屏幕上。这样可以帮助你排查连接、认证和配置方面的问题。

rumenz@local $ scp -v Label.pdf rumenz@192.168.1.110:.
输出

复制文件时 scp 显示进度

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 参数将帮助你解决这个问题。估计时间和连接速度将出现在屏幕上。

rumenz@local $ scp -p Label.pdf rumenz@192.168.1.110:.
输出

scp 估计复制大文件所需的时间

> rumenz@202.x.x.x's password:
Label.pdf 100% 3672KB 126.6KB/s 00:29

使用 -C 参数加快文件传输速度

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

> rumenz@local $ scp -pv messages.log rumenz@192.168.1.110:.
输出

scp 在不压缩的情况下传输文件

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 允许压缩

rumenz@local $ scp -Cpv messages.log rumenz@192.168.1.110:.
输出

scp 使用压缩更快地传输文件

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

如果你要通过网络复制大量文件,-C 参数将帮助你减少所需的总时间。

压缩方法不适用于所有文件。当源文件已经被压缩时,就没有什么效果了。文件如.zip,.rar,pictures, 和.iso

更改 scp 密码以加密文件

默认情况下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

使用 scp 命令限制带宽使用

另一个可能有用的参数是 -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 一起使用的特定端口

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 本身没有配置代理。当你的环境使用代理时,你必须告诉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 文件包含你的 usernamepassword 以明文格式,请确保该文件只能自己访问。

选择不同的ssh_config 文件

对于经常在公司网络和公共网络之间切换的移动用户来说,总是在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参数。

以上是在 Linux 中传输文件文件夹的 10个 scp 命令的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:良许Linux教程网。如有侵权,请联系admin@php.cn删除
比较和对比Linux和Windows的安全模型。比较和对比Linux和Windows的安全模型。Apr 24, 2025 am 12:03 AM

Linux和Windows的安全模型各有优势。Linux提供灵活性和可定制性,通过用户权限、文件系统权限和SELinux/AppArmor实现安全。Windows则注重用户友好性,依赖WindowsDefender、UAC、防火墙和BitLocker保障安全。

Linux和Windows之间的硬件兼容性有何不同?Linux和Windows之间的硬件兼容性有何不同?Apr 23, 2025 am 12:15 AM

Linux和Windows在硬件兼容性上不同:Windows有广泛的驱动程序支持,Linux依赖社区和厂商。解决Linux兼容性问题可通过手动编译驱动,如克隆RTL8188EU驱动仓库、编译和安装;Windows用户需管理驱动程序以优化性能。

Linux和Windows之间虚拟化支持有哪些差异?Linux和Windows之间虚拟化支持有哪些差异?Apr 22, 2025 pm 06:09 PM

Linux和Windows在虚拟化支持上的主要区别在于:1)Linux提供KVM和Xen,性能和灵活性突出,适合高定制环境;2)Windows通过Hyper-V支持虚拟化,界面友好,与Microsoft生态系统紧密集成,适合依赖Microsoft软件的企业。

Linux系统管理员的主要任务是什么?Linux系统管理员的主要任务是什么?Apr 19, 2025 am 12:23 AM

Linux系统管理员的主要任务包括系统监控与性能调优、用户管理、软件包管理、安全管理与备份、故障排查与解决、性能优化与最佳实践。1.使用top、htop等工具监控系统性能,并进行调优。2.通过useradd等命令管理用户账户和权限。3.利用apt、yum管理软件包,确保系统更新和安全。4.配置防火墙、监控日志、进行数据备份以确保系统安全。5.通过日志分析和工具使用进行故障排查和解决。6.优化内核参数和应用配置,遵循最佳实践提升系统性能和稳定性。

很难学习Linux吗?很难学习Linux吗?Apr 18, 2025 am 12:23 AM

学习Linux并不难。1.Linux是一个开源操作系统,基于Unix,广泛应用于服务器、嵌入式系统和个人电脑。2.理解文件系统和权限管理是关键,文件系统是层次化的,权限包括读、写和执行。3.包管理系统如apt和dnf使得软件管理方便。4.进程管理通过ps和top命令实现。5.从基本命令如mkdir、cd、touch和nano开始学习,再尝试高级用法如shell脚本和文本处理。6.常见错误如权限问题可以通过sudo和chmod解决。7.性能优化建议包括使用htop监控资源、清理不必要文件和使用sy

Linux管理员的薪水是多少?Linux管理员的薪水是多少?Apr 17, 2025 am 12:24 AM

Linux管理员的平均年薪在美国为75,000至95,000美元,欧洲为40,000至60,000欧元。提升薪资可以通过:1.持续学习新技术,如云计算和容器技术;2.积累项目经验并建立Portfolio;3.建立职业网络,拓展人脉。

Linux的主要目的是什么?Linux的主要目的是什么?Apr 16, 2025 am 12:19 AM

Linux的主要用途包括:1.服务器操作系统,2.嵌入式系统,3.桌面操作系统,4.开发和测试环境。Linux在这些领域表现出色,提供了稳定性、安全性和高效的开发工具。

互联网在Linux上运行吗?互联网在Linux上运行吗?Apr 14, 2025 am 12:03 AM

互联网运行不依赖单一操作系统,但Linux在其中扮演重要角色。Linux广泛应用于服务器和网络设备,因其稳定性、安全性和可扩展性受欢迎。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)