搜尋
首頁系統教程Linux在 Linux 中傳輸檔案資料夾的 10個 scp 指令

在 Linux 中傳輸檔案資料夾的 10個 scp 指令

Feb 10, 2024 pm 08:30 PM
linuxlinux教程linux系統linux指令shell腳本嵌入式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基礎知識?如何學習Linux基礎知識?Apr 10, 2025 am 09:32 AM

Linux基礎學習從零開始的方法包括:1.了解文件系統和命令行界面,2.掌握基本命令如ls、cd、mkdir,3.學習文件操作,如創建和編輯文件,4.探索高級用法如管道和grep命令,5.掌握調試技巧和性能優化,6.通過實踐和探索不斷提陞技能。

Linux最有用的是什麼?Linux最有用的是什麼?Apr 09, 2025 am 12:02 AM

Linux在服務器、嵌入式系統和桌面環境中的應用廣泛。 1)在服務器領域,Linux因其穩定性和安全性成為託管網站、數據庫和應用的理想選擇。 2)在嵌入式系統中,Linux因其高度定制性和高效性而受歡迎。 3)在桌面環境中,Linux提供了多種桌面環境,滿足不同用戶需求。

Linux的缺點是什麼?Linux的缺點是什麼?Apr 08, 2025 am 12:01 AM

Linux的缺點包括用戶體驗、軟件兼容性、硬件支持和學習曲線。 1.用戶體驗不如Windows或macOS友好,依賴命令行界面。 2.軟件兼容性不如其他系統,缺乏許多商業軟件的原生版本。 3.硬件支持不如Windows全面,可能需要手動編譯驅動程序。 4.學習曲線較陡峭,掌握命令行操作需要時間和耐心。

Linux難以學習嗎?Linux難以學習嗎?Apr 07, 2025 am 12:01 AM

Linuxisnothardtolearn,butthedifficultydependsonyourbackgroundandgoals.ForthosewithOSexperience,especiallycommand-linefamiliarity,Linuxisaneasytransition.Beginnersmayfaceasteeperlearningcurvebutcanmanagewithproperresources.Linux'sopen-sourcenature,bas

Linux的5個基本組件是什麼?Linux的5個基本組件是什麼?Apr 06, 2025 am 12:05 AM

Linux的五個基本組件是:1.內核,管理硬件資源;2.系統庫,提供函數和服務;3.Shell,用戶與系統交互的接口;4.文件系統,存儲和組織數據;5.應用程序,利用系統資源實現功能。

Ubuntu Home Automation:使用開源工具建立智能的居住空間Ubuntu Home Automation:使用開源工具建立智能的居住空間Apr 05, 2025 am 09:19 AM

開啟智能家居新篇章:基於Ubuntu的開源家庭自動化系統 智能家居技術徹底改變了我們與生活空間的互動方式,為日常生活帶來了便利、安全和能源效率。從遠程控制燈光和電器,到監控安全攝像頭和自動化氣候控制,智能家居技術變得越來越普及。 然而,許多商業智能家居系統存在局限性:高昂的成本、隱私問題以及有限的兼容性。幸運的是,開源軟件解決方案結合Ubuntu的強大功能,提供了一種替代方案——允許用戶創建可定制、經濟高效且安全的智能家居生態系統。 本指南將探討如何使用Ubuntu和開源工具設置家庭自動化系統。

Linux vs. Windows:什麼是2025年的差異?Linux vs. Windows:什麼是2025年的差異?Apr 05, 2025 am 09:05 AM

Linux與Windows:2025比較 考慮從MacOS或Windows切換? Linux可能是答案。 儘管MacOS用戶會發現相對平滑的過渡(由於MacOS的UNIX Core),但Windows用戶需要適應。此指南HIG

什麼是Linux設備?什麼是Linux設備?Apr 05, 2025 am 12:04 AM

Linux設備是運行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脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器