linux之間的文件互傳-scp命令

scp是secure copy的簡寫,用于在Linux下進行遠程拷貝文件或目錄的命令,基于ssh登陸進行安全的遠程文件拷貝命令,因此其傳輸是加密的。scp占用資源非常少,并不會提高多少系統負荷,在這一點上,另一個命令rsync就遠遠不及它了。雖然 rsync比scp會快一點,但當小文件眾多的情況下,rsync會導致硬盤I/O非常高,而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為單位。
-P port 注意是大寫的P, port是指定數據傳輸用到的端口號。
-S program 指定加密傳輸時所使用的程序。此程序必須能夠理解ssh(1)的選項。

應用實例

從本地服務器復制到遠程服務器

復制文件
  • 命令格式scp local_file remote_username@remote_ip:remote_folder,復制到遠程的文件夾中
# 本地使用命令
[root@virtue ~]# scp 1.txt root@172.16.20.38:/tmp/
The authenticity of host '172.16.20.38 (172.16.20.38)' can't be established.
ECDSA key fingerprint is SHA256:z8KeBlt/fTyXBAibRQk0kGevF9E8HgNvBB6bLJDw5zY.
ECDSA key fingerprint is MD5:b5:b6:90:c7:70:46:06:aa:44:3d:35:74:03:88:ec:f7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.20.38' (ECDSA) to the list of known hosts.
root@172.16.20.38's password: 
1.txt                                                                                  100%   37    23.6KB/s   00:00

# 遠程計算機中顯示
[root@ide ~]# ll /tmp/1.txt 
-rw-r--r--. 1 root root 37 1月  17 13:40 /tmp/1.txt
  • 命令格式scp local_file remote_username@remote_ip:remote_file,指定復制的文件名
# 本地使用命令
[root@virtue ~]# scp 1.txt root@172.16.20.38:/tmp/2.txt
root@172.16.20.38's password: 
1.txt                                                                                  100%   37    21.9KB/s   00:00    

# 遠程計算機中顯示
[root@ide ~]# ll /tmp/2.txt 
-rw-r--r--. 1 root root 37 1月  17 13:43 /tmp/2.txt
[root@ide ~]# diff /tmp/1.txt /tmp/2.txt 
[root@ide ~]# 
  • 命令格式scp local_file remote_ip:remote_folder,使用本機登錄用戶作為用戶名傳輸文件,復制文件到指定文件夾
# 本地使用命令
[root@virtue ~]# scp 1.txt 172.16.20.38:/tmp/
root@172.16.20.38's password: 
1.txt                                                                                  100%   37    50.8KB/s   00:00    

# 遠程計算機中顯示
[root@ide ~]# ll /tmp/1.txt 
-rw-r--r--. 1 root root 37 1月  17 13:47 /tmp/1.txt

可以看到,文件被直接進行了覆蓋,沒有提示

  • 命令格式scp local_file remote_ip:remote_file,使用本機登錄用戶作為用戶名傳輸文件,指定復制的文件名
# 本地使用命令
[root@virtue ~]# scp 1.txt 172.16.20.38:/tmp/3.txt
root@172.16.20.38's password: 
1.txt                                                                                  100%   37     7.5KB/s   00:00    

# 遠程計算機中顯示
[root@ide ~]# ll /tmp/3.txt 
-rw-r--r--. 1 root root 37 1月  17 13:52 /tmp/3.txt
[root@ide ~]# diff /tmp/1.txt /tmp/3.txt 
[root@ide ~]# 
復制目錄

與復制文件的命令格式基本相同,只不過需要增加-r參數

  • 命令格式scp -r local_folder remote_username@remote_ip:remote_folder,目錄整體復制到遠程目錄內。
# 本地使用命令
[root@virtue ~]# scp -P 22 -r script root@172.16.20.38:/tmp
root@172.16.20.38's password: 
kernel.txt                                                                             100%   59MB  80.0MB/s   00:00    
modify.py                                                                              100%  171   312.5KB/s   00:00    
ping.sh                                                                                100%  116   164.5KB/s   00:00    
setup.sh                                                                               100%  443   578.1KB/s   00:00    

# 遠程計算機中顯示
[root@ide ~]# ll /tmp/script/
總用量 60428
-rw-r--r--. 1 root root 61865984 1月  17 13:58 kernel.txt
-rw-r--r--. 1 root root      171 1月  17 13:58 modify.py
-rw-r--r--. 1 root root      116 1月  17 13:58 ping.sh
-rw-r--r--. 1 root root      443 1月  17 13:58 setup.sh
[root@ide ~]# 
  • 命令格式scp local_file remote_ip:remote_folder,使用本機登錄用戶作為用戶名傳輸文件,復制目錄到遠程目錄內。
# 本地使用命令
[root@virtue ~]# scp -P 22 -r script 172.16.20.38:/tmp
root@172.16.20.38's password: 
kernel.txt                                                                             100%   59MB  79.4MB/s   00:00    
modify.py                                                                              100%  171   294.9KB/s   00:00    
ping.sh                                                                                100%  116   205.7KB/s   00:00    
setup.sh                                                                               100%  443   991.4KB/s   00:00    

# 遠程計算機中顯示
[root@ide ~]# ll /tmp/script/
總用量 60428
-rw-r--r--. 1 root root 61865984 1月  17 14:01 kernel.txt
-rw-r--r--. 1 root root      171 1月  17 14:01 modify.py
-rw-r--r--. 1 root root      116 1月  17 14:01 ping.sh
-rw-r--r--. 1 root root      443 1月  17 14:01 setup.sh

可以看到,目錄被直接進行了覆蓋,也沒有提示

從遠程服務器復制到本地服務器

從遠程復制到本地的scp命令與上面的命令雷同,只要將從本地復制到遠程的命令后面2個參數互換順序就行了

復制文件
# 指定文件名
[root@virtue ~]# scp 172.16.20.38:/tmp/1.txt 1.txt 
root@172.16.20.38's password: 
1.txt                                                                                  100%   37    47.1KB/s   00:00    
[root@virtue ~]# ll 1.txt 
-rw-r--r--. 1 root root 37 1月  17 14:02 1.txt

# 指定保存路徑(本地路徑是當前目錄,命令最后有一個.目錄符號)
[root@virtue ~]# scp 172.16.20.38:/tmp/1.txt .
root@172.16.20.38's password: 
1.txt                                                                                  100%   37    32.6KB/s   00:00    
[root@virtue ~]# ll 1.txt 
-rw-r--r--. 1 root root 37 1月  17 14:03 1.txt

可以看到,無論那種復制方式都是直接將本地的文件進行了覆蓋,沒有提示信息。

復制目錄
[root@virtue ~]# scp -P 22 -r 172.16.20.38:/tmp/script .
root@172.16.20.38's password: 
kernel.txt                                                                             100%   59MB  52.8MB/s   00:01    
modify.py                                                                              100%  171   280.1KB/s   00:00    
ping.sh                                                                                100%  116   189.0KB/s   00:00    
setup.sh                                                                               100%  443   883.6KB/s   00:00    
[root@virtue ~]# ll script/
總用量 60428
-rw-r--r--. 1 root root 61865984 1月  17 14:09 kernel.txt
-rw-r--r--. 1 root root      171 1月  17 14:09 modify.py
-rw-r--r--. 1 root root      116 1月  17 14:09 ping.sh
-rw-r--r--. 1 root root      443 1月  17 14:09 setup.sh

可以看到目錄也是直接進行了覆蓋,沒有提示信息,所以scp使用的時候一定要確定無誤再使用,否則容易追悔莫及啊~~~

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容