linux系統中scp命令的用法__linux

來源:互聯網
上載者:User
inux系統中scp命令的用法
注意: 在使用過程中如果出現   -bash: scp: command not found 請執行 yum install openssh-clients

    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 看到更細緻的英文說明. 
=============================================================== 實踐一例: 本地機:X.X.29.12        遠程機:X.X.29.18   遠程機使用者:aaron 要求將本地機上的/www/xinpindao     複製到遠程機的/tmp/www %scp  -r   /www/xinpindao     aaron@X.X.29.18:/tmp/www aaron@X.X.29.13's password: 正常情況下輸入aaron使用者的密碼即可完成複製。。但此次複製沒有成功出現如下提示: scp: /tmp/www/xinpindao: Permission denied 排錯: 重新查看scp命令。。。沒有問題。。  重新輸入口令。。。沒有錯誤。。 查看遠程機目錄許可權。。。 drwxr-xr-x 2 root  root  4096 May  7 17:30 www 原來問題出現在這兒,此目錄是後來使用root使用者建立,但是對於aaron用來來講,沒有寫入許可權,更改許可權 %chmod  777   /tmp/www 再次執行上述命令。。。成功。。。   總結:當使用scp命令進行檔案複製時如果出現檔案許可權問題,請仔細檢查目錄許可權,小的細節請引起注意。。 實踐二例: %scp   /www/xinpindao    root@X.X.29.18:/tmp    遠程機使用者root root@X.X.29.18's password: 
Permission denied, please try again. 經過多次檢查後最終發現- 為保證安全前期布署過程中已將ssh服務關閉root使用者的登入許可權 解決方案:

要修改root的ssh許可權,即修改 /etc/ssh/sshd_config檔案中

PermitRootLogin no 改為 PermitRootLogin yes

重啟ssh服務

%/etc/init.d/sshd   restart 

重新測試成功。。。

使用SCP命令之前先SU ROOT

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.