scp:secure copy的縮寫, scp是linux系統下基於ssh登陸進行安全的遠程文件拷貝命令。
scp命令用於Linux之間複製文件和目錄。和它類似的命令有cp,不過cp只是在本機進行拷貝不能跨服務器,而且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
[簡易寫法]:
1、scp [參數] file_source file_target
2、scp [參數] [源路徑] @IP:/目標路徑
查看參數:
man scp
詳細信息請參考help
參數解釋:
-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)的選項。
在虛擬機中模擬
192.168.102.22 [本地機]
192.168.102.110 [遠程機]
一、從本地機複製到遠程機
1、文件的操作
方式1:
scp local_file remote_username@remote_ip:remote_folder/file[目錄/文件]
方式2:
scp local_file remote_ip:remote_folder/file[目錄/文件]
2、目錄的操作
方式1:
scp -r local_folder remote_username@remote_ip:remote_folder
方式2:
scp -r local_folder remote_ip:remote_folder
注意:
方式1:命令執行後需要輸入指定用戶的密碼。
方式2:命令執行後需要輸入用戶名和密碼。
模擬測試:
在192.168.102.22[本地機]普通用戶odysee家目錄中
1、 創建一個scpLocal文件夾
2、 在文件夾中創建一個local文件
3、在文件local中輸入'hello scp!'
在192.168.102.110[遠程機]普通用戶odysee家目錄中
1、創建一個scpRemote
測試:
方式1:
scp local_file remote_username@remote_ip:remote_folder
把22的local文件copy到110 scpRemote/下面
scp local [email protected]:/home/odysee/scpRemote
輸入密碼後執行成功
查看遠程機是否存在
方式2:
scp local_file remote_ip:remote_folder/file
把22的local2文件copy到110 scpRemote/下面
scp local2 192.168.102.110:/home/odysee/scpRemote
查看遠程機110
關於對目錄的操作和文件類似
下面模擬一個:
1、 在本地機的scpLocal/創建一個scpLocal2文件
2、 在scpLocal2/創建一個local3文件並寫入'hello scp3!'
測試:
我們把本地機的scpLocal copy到遠程機的scpRemote/
注意-r遞歸copy
scp -r local_folder remote_username@remote_ip:remote_folder
即:
scp -r scpLocal [email protected]:/home/odysee/scpRemote
注意執行命令所在目錄
查看遠程機
成功
2、 從遠程機複製到本地機
相似於本地複製到遠程機[把兩個地址調換一下]
1、scp [參數] file_target file_source
2、scp [參數] @IP:/目標路徑 [源路徑]
如下:
1、文件的操作
Scp remote_username@remote_ip:remote_folder/file local_file
scp remote_ip:remote_folder/file local_file
2、目錄的操作
scp -r remote_username@remote_ip:remote_folder local_folder
scp -r remote_ip:remote_folder local_folder
模擬一個:
把遠程機的scpRemote複製到本地機的scpLocal/
scp -r [email protected]:/home/odysee/scpRemote scpLocal
測試成功
注意:
如遠程服務器防火牆為scp命令設置指定端口,我們需要使用-P來設置命令端口號
如下:
scp -P port[端口號] remote_username@remote_ip:remote_file local_folder
小提示:
遠程複製文件或者目錄還需要來回的輸入用戶或則密碼,可能稍微有點麻煩。
下一篇配置ssh免密碼登錄,再使用scp的時候就不用輸入密碼了。
閱讀更多 程序員日常筆記 的文章