linux下scp命令詳解

lizhongren發表於2012-11-13

linux下scp命令詳解

http://www.9usb.net/201006/linux-scp-mingling-yongfa.html

scp是 secure copy的縮寫, scp是linux系統下基於ssh登陸進行安全的遠端檔案拷貝命令。linux的scp命令可以在linux伺服器之間複製檔案和目錄.
scp命令的用處:
scp在網路上不同的主機之間複製檔案,它使用ssh安全協議傳輸資料,具有和ssh一樣的驗證機制,從而安全的遠端拷貝檔案。
scp命令基本格式:
scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
[-l limit] [-o ssh_option] [-P port] [-S program]
[[user@]host1:]file1 [...] [[user@]host2:]file2
scp命令的引數說明:
-1
強制scp命令使用協議ssh1
-2
強制scp命令使用協議ssh2
-4
強制scp命令只使用IPv4定址
-6
強制scp命令只使用IPv6定址
-B
使用批處理模式(傳輸過程中不詢問傳輸口令或短語)
-C
允許壓縮。(將-C標誌傳遞給ssh,從而開啟壓縮功能)
-p 保留原檔案的修改時間,訪問時間和訪問許可權。
-q
不顯示傳輸進度條。
-r
遞迴複製整個目錄。
-v 詳細方式顯示輸出。scp和ssh(1)會顯示出整個過程的除錯資訊。這些資訊用於除錯連線,驗證和配置問題。
-c cipher
以cipher將資料傳輸進行加密,這個選項將直接傳遞給ssh。
-F ssh_config
指定一個替代的ssh配置檔案,此引數直接傳遞給ssh。
-i identity_file
從指定檔案中讀取傳輸時使用的金鑰檔案,此引數直接傳遞給ssh。
-l limit
限定使用者所能使用的頻寬,以Kbit/s為單位。
-o ssh_option
如果習慣於使用ssh_config(5)中的引數傳遞方式,
-P port  注意是大寫的P, port是指定資料傳輸用到的埠號
-S program
指定加密傳輸時所使用的程式。此程式必須能夠理解ssh(1)的選項。
scp命令的實際應用
1>從本地伺服器複製到遠端伺服器
(1) 複製檔案:
命令格式:
scp local_file remote_username@remote_ip:remote_folder
或者
scp local_file remote_username@remote_ip:remote_file
或者
scp local_file remote_ip:remote_folder
或者
scp local_file remote_ip:remote_file
第1,2個指定了使用者名稱,命令執行後需要輸入使用者密碼,第1個僅指定了遠端的目錄,檔名字不變,第2個指定了檔名
第3,4個沒有指定使用者名稱,命令執行後需要輸入使用者名稱和密碼,第3個僅指定了遠端的目錄,檔名字不變,第4個指定了檔名
例項:
scp /home/linux/soft/scp.zip root@www.mydomain.com:/home/linux/others/soft
scp /home/linux/soft/scp.zip root@www.mydomain.com:/home/linux/others/soft/scp2.zip
scp /home/linux/soft/scp.zip www.mydomain.com:/home/linux/others/soft
scp /home/linux/soft/scp.zip www.mydomain.com:/home/linux/others/soft/scp2.zip
(2) 複製目錄:
命令格式:
scp -r local_folder remote_username@remote_ip:remote_folder
或者
scp -r local_folder remote_ip:remote_folder
第1個指定了使用者名稱,命令執行後需要輸入使用者密碼;
第2個沒有指定使用者名稱,命令執行後需要輸入使用者名稱和密碼;
例子:
scp -r /home/linux/soft/ root@www.mydomain.com:/home/linux/others/
scp -r /home/linux/soft/ www.mydomain.com:/home/linux/others/
上面 命令 將 本地 soft 目錄 複製 到 遠端 others 目錄下,即複製後遠端伺服器上會有/home/linux/others/soft/ 目錄
2>從遠端伺服器複製到本地伺服器
從遠端複製到本地的scp命令與上面的命令雷同,只要將從本地複製到遠端的命令後面2個引數互換順序就行了。
例如:
scp root@www.mydomain.com:/home/linux/soft/scp.zip /home/linux/others/scp.zip
scp www.mydomain.com:/home/linux/soft/ -r /home/linux/others/
linux系統下scp命令中很多引數都和 ssh1 有關 , 還需要看到更原汁原味的引數資訊,可以執行man scp 看到更細緻的英文說明.

相關文章