next up previous contents
Next: sshpass 傳送密碼 Up: SSH Server Previous: SSH 免密碼登入   Contents

ssh-copy-id

  1. 上一節免密碼設定的複雜過程,linux 上已幫你寫好一支腳本 ssh-copy-id,先執行 ssh-keygen 產生 ssh key。
    [root@kvm3 ~]# ssh-keygen 
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa): 
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in /root/.ssh/id_rsa.
    Your public key has been saved in /root/.ssh/id_rsa.pub.
    The key fingerprint is:
    01:be:06:3d:89:bd:5b:b4:7a:e6:37:b0:07:44:86:35 root@kvm3.deyu.wang
    The key's randomart image is:
    +--[ RSA 2048]----+
    |      .oE        |
    |     =.oo.       |
    |    o *oo        |
    |     . =.o       |
    |      +.S        |
    |     . +o        |
    |      o o+       |
    |       +. +      |
    |        .o .     |
    +-----------------+
    
  2. 執行 ssh-copy-id 參數為要登入的主機,就可以將 public key 傳送到該主機,並幫忙設定好免密碼登入的信任主機。
    [root@kvm3 ~]# ssh-copy-id 192.168.122.7
    The authenticity of host '192.168.122.7 (192.168.122.7)' can't be established.
    ECDSA key fingerprint is 30:b2:6e:89:45:69:cc:2f:52:a2:c7:d0:dc:2b:aa:e9.
    Are you sure you want to continue connecting (yes/no)? yes
    /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
    /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
    root@192.168.122.7's password: 
    
    Number of key(s) added: 1
    
    Now try logging into the machine, with:   "ssh '192.168.122.7'"
    and check to make sure that only the key(s) you wanted were added.
    
  3. 登入 192.168.122.7 已不需要輸入密碼。
    [root@kvm3 ~]# ssh 192.168.122.7
    Last login: Thu Feb 15 11:58:57 2018 from deyu.wang
    [root@kvm7 ~]#
    



De-Yu Wang 2018-09-25