在 Linux 中傳輸檔案資料夾的 10個 scp 指令
scp 指令的基本用法
下面的指令的意思是 把 source_file_name
複製到destination_host
的username account
下的destination_folder
裡。
> 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
文件包含你的 username
和password
以明文格式,请确保该文件只能自己访问。
选择不同的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中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

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

VS Code 系統要求:操作系統:Windows 10 及以上、macOS 10.12 及以上、Linux 發行版處理器:最低 1.6 GHz,推薦 2.0 GHz 及以上內存:最低 512 MB,推薦 4 GB 及以上存儲空間:最低 250 MB,推薦 1 GB 及以上其他要求:穩定網絡連接,Xorg/Wayland(Linux)

Linux系統的五個基本組件是:1.內核,2.系統庫,3.系統實用程序,4.圖形用戶界面,5.應用程序。內核管理硬件資源,系統庫提供預編譯函數,系統實用程序用於系統管理,GUI提供可視化交互,應用程序利用這些組件實現功能。

雖然 Notepad 無法直接運行 Java 代碼,但可以通過借助其他工具實現:使用命令行編譯器 (javac) 編譯代碼,生成字節碼文件 (filename.class)。使用 Java 解釋器 (java) 解釋字節碼,執行代碼並輸出結果。

vscode 內置終端是一個開發工具,允許在編輯器內運行命令和腳本,以簡化開發流程。如何使用 vscode 終端:通過快捷鍵 (Ctrl/Cmd ) 打開終端。輸入命令或運行腳本。使用熱鍵 (如 Ctrl L 清除終端)。更改工作目錄 (如 cd 命令)。高級功能包括調試模式、代碼片段自動補全和交互式命令歷史。

要查看 Git 倉庫地址,請執行以下步驟:1. 打開命令行並導航到倉庫目錄;2. 運行 "git remote -v" 命令;3. 查看輸出中的倉庫名稱及其相應的地址。

VS Code擴展安裝失敗的原因可能包括:網絡不穩定、權限不足、系統兼容性問題、VS Code版本過舊、殺毒軟件或防火牆干擾。通過檢查網絡連接、權限、日誌文件、更新VS Code、禁用安全軟件以及重啟VS Code或計算機,可以逐步排查和解決問題。

在 Visual Studio Code(VSCode)中編寫代碼簡單易行,只需安裝 VSCode、創建項目、選擇語言、創建文件、編寫代碼、保存並運行即可。 VSCode 的優點包括跨平台、免費開源、強大功能、擴展豐富,以及輕量快速。

VS Code 可以在 Mac 上使用。它具有強大的擴展功能、Git 集成、終端和調試器,同時還提供了豐富的設置選項。但是,對於特別大型項目或專業性較強的開發,VS Code 可能會有性能或功能限制。
