Linux管理員應該熟悉命令行環境。由於GUI(圖形用戶界面)模式在Linux服務器上通常不會安裝。
SSH可能是最受歡迎的協議,允許Linux管理員以遠程安全的方式管理服務器。內置的SSH命令還有SCP命令,用於以安全的方式在服務器之間複製文件。
SCP指令基本語法
以下命令將解讀為將“source_file_name”複製到“destination_folder”在“destination_host”使用“username”帳戶。
scp source_file_name username@destination_host:destination_folder
在SCP命令中有許多參數可以使用。這裡是一些可能用於日常使用的參數。
目錄
在 Linux 中安全傳輸檔案
基本的 SCP 命令不帶參數將在背景複製檔案。除非過程完成或出現某些錯誤,否則用戶將看不到任何內容。
您可以使用“-v
”參數將除錯資訊列印到螢幕上。它可以幫助您除錯連接、驗證和配置問題。
從本地主機複製檔案到遠程伺服器
下列指令將檔案“scp-cheatsheet.pdf”從本地複製到遠端Linux系統的/home/tecmint目錄下。
$ scp -v scp-cheatsheet.pdf [email protected]:/home/tecmint/.
範例輸出:
Executing: program /usr/bin/ssh host 192.168.0.183, user tecmint, command scp -v -t /home/tecmint/. OpenSSH_8.2p1 Ubuntu-4ubuntu0.5, OpenSSL 1.1.1f 31 Mar 2020 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files debug1: /etc/ssh/ssh_config line 21: Applying options for * debug1: Connecting to 192.168.0.183 [192.168.0.183] port 22. debug1: Connection established. debug1: identity file /home/tecmint/.ssh/id_rsa type -1 debug1: identity file /home/tecmint/.ssh/id_rsa-cert type -1 debug1: identity file /home/tecmint/.ssh/id_dsa type -1 debug1: identity file /home/tecmint/.ssh/id_dsa-cert type -1 debug1: identity file /home/tecmint/.ssh/id_ecdsa type -1 debug1: identity file /home/tecmint/.ssh/id_ecdsa-cert type -1 debug1: identity file /home/tecmint/.ssh/id_ecdsa_sk type -1 debug1: identity file /home/tecmint/.ssh/id_ecdsa_sk-cert type -1 debug1: identity file /home/tecmint/.ssh/id_ed25519 type -1 debug1: identity file /home/tecmint/.ssh/id_ed25519-cert type -1 debug1: identity file /home/tecmint/.ssh/id_ed25519_sk type -1 debug1: identity file /home/tecmint/.ssh/id_ed25519_sk-cert type -1 debug1: identity file /home/tecmint/.ssh/id_xmss type -1 ...
從遠端主機複製檔案到本地主機
下列指令將檔案“ssh-cheatsheet.pdf”從遠端主機複製到本地系統的/home/tecmint目錄下。
$ scp -v [email protected]:/home/ravi/ssh-cheatsheet.pdf /home/tecmint/.
範例輸出:
Executing: program /usr/bin/ssh host 192.168.0.183, user tecmint, command scp -v -f /home/ravi/ssh-cheatsheet.pdf OpenSSH_8.2p1 Ubuntu-4ubuntu0.5, OpenSSL 1.1.1f 31 Mar 2020 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files debug1: /etc/ssh/ssh_config line 21: Applying options for * debug1: Connecting to 192.168.0.183 [192.168.0.183] port 22. debug1: Connection established. debug1: identity file /home/tecmint/.ssh/id_rsa type -1 debug1: identity file /home/tecmint/.ssh/id_rsa-cert type -1 debug1: identity file /home/tecmint/.ssh/id_dsa type -1 debug1: identity file /home/tecmint/.ssh/id_dsa-cert type -1 debug1: identity file /home/tecmint/.ssh/id_ecdsa type -1 debug1: identity file /home/tecmint/.ssh/id_ecdsa-cert type -1 debug1: identity file /home/tecmint/.ssh/id_ecdsa_sk type -1 debug1: identity file /home/tecmint/.ssh/id_ecdsa_sk-cert type -1 debug1: identity file /home/tecmint/.ssh/id_ed25519 type -1 debug1: identity file /home/tecmint/.ssh/id_ed25519-cert type -1 debug1: identity file /home/tecmint/.ssh/id_ed25519_sk type -1 ...
從遠端主機複製檔案到另一個遠端主機
下列指令將檔案“ssh-cheatsheet.pdf”從遠端主機複製到另一個遠端主機系統的/home/tecmint目錄下。
$ scp -v [email protected]:/home/ravi/ssh-cheatsheet.pdf [email protected]:/home/anusha/.
複製檔案並保留原始建立日期和時間
使用“-p
”參數將在複製檔案時保留檔案的原始修改和存取時間,並且在螢幕上顯示估計時間和連線速度。
$ scp -p scp-cheatsheet.pdf [email protected]:/home/tecmint/.
範例輸出:
[email protected]'s password: scp-cheatsheet.pdf 100% 531 721.4KB/s 00:00
複製檔案時的Scp壓縮
其中一個可以加快您的檔案傳輸的參數是“-C
”參數,它用於在傳輸過程中壓縮您的檔案。獨特之處在於,壓縮僅在網絡中發生。當檔案到達目的地伺服器時,它將恢復到壓縮前的原始大小。
查看這些命令。它正在使用一個93 Mb的單一檔案。
$ scp -pv messages.log [email protected]:.
樣本輸出:
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 [email protected]'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
”參數將導致1661.3秒。您可以將結果與下面使用“-C"
參數的命令進行比較。
$ scp -Cpv messages.log [email protected]:.
樣本輸出:
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 [email protected]'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
如您所見,當您使用壓縮時,傳輸過程在162.5秒內完成。它比不使用“-C
”參數快10倍。如果您正在網絡上複製大量檔案,“-C
”參數將幫助您減少所需的總時間。
我們應該注意的是,壓縮方法對任何檔案都不起作用。當源檔案已經被壓縮時,您將不會在那裡找到任何改進。像.zip、.rar、圖片和.iso這樣的檔案將不會受到“-C
”參數的影響。
更改 SCP 密碼以加密文件
默認情況下,SCP 使用「AES-128」來加密文件。如果您想更改為另一種密碼來加密它,您可以使用「-c
」參數。
看一下這個命令。
$ scp -c 3des Label.pdf [email protected]:. [email protected]'s password: Label.pdf 100% 3672KB 282.5KB/s 00:13
上面的命令告訴 SCP 使用 3des 算法 來加密文件。請小心,使用「-c
」參數而不是「-C
」。
使用 SCP 命令限制帶寬使用
另一個可能有用的參數是「-l
」參數。「-l」參數將 限制使用的帶寬。如果您要編寫自動化腳本來複製大量文件,但不想讓帶寬被 SCP 進程耗盡,這將非常有用。
$ scp -l 400 Label.pdf [email protected]:. [email protected]'s password: Label.pdf 100% 3672KB 50.3KB/s 01:13
「-l
」參數後面的 400 值表示我們將帶寬限制為 SCP 進程僅 50 KB/sec。
需要記住的一點是,帶寬以 千比特/秒(kbps)為單位指定。這意味著 8 位 等於 1 字節。
雖然SCP計算在千字節/秒(KB/s)。因此,如果您想將您的帶寬限制在SCP最大僅50KB/s,您需要將其設置為50 x 8=400。
使用不同端口的SCP
通常,SCP使用端口22作為默認端口,但基於安全原因,您可能將端口更改為另一個端口。例如,我們使用端口2249。
那麼命令應該是這樣的。
$ scp -P 2249 Label.pdf [email protected]:. [email protected]'s password: Label.pdf 100% 3672KB 262.3KB/s 00:14
請確保它使用大寫的“P
”而不是“p
”,因為“p
”已經用於保留時間和模式。
SCP – 遞歸複製文件和目錄
有時我們需要複製目錄及其內部所有文件/目錄。如果能夠使用“-r
”參數在單個命令中完成這一點將會更好,該參數遞歸地複製整個目錄。
$ scp -r documents [email protected]:. [email protected]'s password: Label.pdf 100% 3672KB 282.5KB/s 00:13 scp.txt 100% 10KB 9.8KB/s 00:00
當複製過程完成時,在目標服務器上您將找到一個名為“documents”的目錄及其所有文件。目錄“documents”是自動創建的。
SCP – 關閉進度訊息
如果您選擇不查看SCP的進度計和警告/診斷訊息,您可以使用“-q
”參數來禁用它。這裡有一個例子。
$ scp -q Label.pdf [email protected]:. [email protected]'s password: pungki@mint ~/Documents $
如您所見,在您輸入密碼後,沒有關於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
之後,您可以像往常一樣透明地進行SCP操作。
請注意,corkscrew可能尚未安裝在您的系統上。在我的Linux Mint上,我需要先使用標準的Linux Mint安裝程序安裝它。
$ apt-get install corkscrew
對於其他 yum 基礎系統,用戶可以使用以下 yum 命令 來安裝 corkscrew。
# yum install corkscrew
另一點是,由於 “~/.ssh/proxyauth” 文件包含您的 “用戶名” 和 “密碼” 以明文格式,請確保該文件只能由您訪問。
選擇不同的 ssh_config 文件
對於經常在公司網絡和公共網絡之間切換的移動用戶來說,總是在 SCP 中更改設置將是一種痛苦。如果我們能夠放置一個不同的 ssh_config 文件以滿足我們的需求,那就更好了。
在公司網絡中使用代理,但在公共網絡中不使用,並且您經常切換網絡。
$ scp -F /home/pungki/proxy_ssh_config Label.pdf [email protected]:. [email protected]'s password: Label.pdf 100% 3672KB 282.5KB/s 00:13
默認情況下,每個用戶的 “ssh_config” 文件將放置在 “~/.ssh/config” 中。創建一個具有代理兼容性的特定 “ssh_config” 文件將使在網絡之間切換變得更容易。
當您在公司網絡上時,可以使用 “-F
” 參數。當您在公共網絡上時,可以跳過 “-F
” 參數。
以上是有關SCP的全部內容。您可以查看man頁面以獲取更多關於SCP的詳細資訊。歡迎隨時留下評論和建議。