天天學一個 Linux 命令(60):scp

圖片

昨日推薦:天天學一個 Linux 命令(59):sshnginx

命令簡介

scp 全拼secure copy,用於不一樣主機之間複製文件。mongodb

scp命令經常使用於在Linux系統下兩個不一樣主機之間傳輸文件,其功能與cp命令類似,可是不一樣是,cp命令只能用於在本機環境下傳輸或複製拷貝文件,scp命令能夠跨越不一樣主機,而scp傳輸文件是加密的。centos

scp 它使用ssh進行數據傳輸,並使用與ssh相同的身份驗證並提供相同的安全性,scp 會要求輸入密碼或其它方式以進行身份驗證。api

語法格式

scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] 
    [-l limit] [-o ssh_option] [-P port] [-S program]
    [[user@]host1:]file1 ... [[user@]host2:]file2
  • 源文件:須要複製的文件
  • 目標文件:格式爲 user@host:filename(文件名爲目標文件的名稱)

選項說明

-1  #指定使用ssh協議版本爲1
-2  #指定使用ssh協議版本爲2
-3  #指定兩個主機之間的副本經過本地主機傳輸
-4  #指定使用ipv4
-6  #指定使用ipv6
-B  #使用批處理模式
-C  #使用壓縮模式傳輸文件
-F  #使用指定的ssh配置文件
-i identity_file  #從指定文件中讀取傳輸時使用的密鑰文件
-l  #寬帶限制
-o  #使用的ssh選項
-P  #遠程主機的端口號
-p  #保留文件的最後修改時間,最後訪問時間和權限模式
-q  #不輸出複製進度信息
-r  #以遞歸方式複製
-S program  #指定加密傳輸時所使用的程序
-v  #顯示傳輸過程的詳細信息

應用舉例

從遠程服務器複製到本地服務器安全

#複製文件
scp root@192.168.1.2:/download/soft/nginx.tar.gz /download/soft/
#複製目錄
scp -r root@192.168.1.2:/app/soft/mongodb /app/soft/

以mingongge用戶身份將遠程主機mingongge.com上的/home/mingongge/backup.tar.gz 文件傳送到當前工做目錄下,並將傳輸限制爲每秒 80 KB服務器

scp -l 80 mingongge@mingongge.com:/home/mingongge/backup.tar.gz
#也能夠寫成以下
scp -l 80 mingongge@mingongge.com:/home/mingongge/backup.tar.gz ./

使用指定的端口號傳輸文件session

scp -P 9999 root@192.168.1.2:/download/soft/nginx.tar.gz /download/soft/

查看詳細的傳輸過程app

[root@centos7 ~]# scp -v root@192.168.1.100:/root/nginxWebUI-1.3.5.jar  /root/download/
Executing: program /usr/bin/ssh host 192.168.1.100, user root, command scp -v -f /root/nginxWebUI-1.3.5.jar
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug1: Connecting to 192.168.1.100 [192.168.1.100] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 192.168.1.100:22 as 'root'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: curve25519-sha256 need=64 dh_need=64
debug1: kex: curve25519-sha256 need=64 dh_need=64
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:GqOqgdhVZyEtg/wSM8l5YB+Y6GO8K3Ii7OFsKW9R2n0
The authenticity of host '192.168.1.100 (192.168.1.100)' can't be established.
ECDSA key fingerprint is SHA256:GqOqgdhVZyEtg/wSM8l5YB+Y6GO8K3Ii7OFsKW9R2n0.
ECDSA key fingerprint is MD5:cc:4b:7d:b6:59:0f:77:83:a9:a5:32:70:4e:87:0d:41.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.100' (ECDSA) to the list of known hosts.
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure.  Minor code may provide more information
No Kerberos credentials available (default cache: KEYRING:persistent:0)
debug1: Unspecified GSS failure.  Minor code may provide more information
No Kerberos credentials available (default cache: KEYRING:persistent:0)
debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/id_rsa
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: Trying private key: /root/.ssh/id_ed25519
debug1: Next authentication method: password
root@192.168.1.100's password: 
debug1: Authentication succeeded (password).
Authenticated to 192.168.1.100 ([192.168.1.100]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: pledge: network
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending command: scp -v -f /root/nginxWebUI-1.3.5.jar
Sending file modes: C0644 36196329 nginxWebUI-1.3.5.jar
Sink: C0644 36196329 nginxWebUI-1.3.5.jar
nginxWebUI-1.3.5.jar                 100%   35MB  12.1MB/s   00:02   
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: client_input_channel_req: channel 0 rtype eow@openssh.com reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
Transferred: sent 12604, received 36237992 bytes, in 4.3 seconds
Bytes per second: sent 2916.1, received 8384212.5
debug1: Exit status 0

經過上述過程信息,能夠知道scp傳輸文件的整個過程是什麼樣的,也能夠明白一些原理。ssh

天天學一個 Linux 命令(57):calide

天天學一個 Linux 命令(58):telnet

相關文章
相關標籤/搜索