1. 程式人生 > >利用ssh-copy-id實現SSH無密碼登錄

利用ssh-copy-id實現SSH無密碼登錄

ssh copy id

第一步: 產生公鑰與私鑰對:

[[email protected]]# ssh-keygen -t rsa

按照提示輸入完後,會在~/.ssh目錄下生成id_rsa和id_rsa.pub這兩個文件

第二步:用ssh-copy-id將公鑰復制到遠程機器中

ssh-copy-id 將本機的公鑰復制到遠程機器的authorized_keys文件中,ssh-copy-id也會給遠程主機的用戶主目錄(home)和 ~./ssh 和 ~/.ssh/authorized_keys設置合適的權利。

語法:

  ssh-copy-id [-i [identity_file]] [[email protected]]machine

選項:

  -i:指定公鑰文件

實例:

把本地的ssh公鑰文件安裝到遠程主機對應的賬戶下:

[[email protected]]# ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host

    [email protected]‘s password:      #輸入遠程主機的用戶密碼
    Now try logging into the machine, with "ssh ‘remote-host‘", and check in:

      .ssh/authorized_keys

    to make sure we haven‘t added extra keys that you weren‘t expecting.


第三步: 登錄到遠程機器不用輸入密碼

[[email protected]]# ssh remote-host
Last login: Mon Sep 11 18:30:00 2017 from remote-host


常見問題:

[[email protected]]# ssh-copy-id -u demo -i ~demo/.ssh/id_rsa.pub [email protected]_host
/usr/bin/ssh-copy-id: ERROR: No identities found

上述是給demo用戶賦予無密碼登陸的權利

使用選項 -i ,當沒有值傳遞的時候或者 如果 ~/.ssh/identity.pub 文件不可訪問(不存在), ssh-copy-id 將顯示上述的錯誤信息 ( -i選項會優先使用將ssh-add -L的內容)

[[email protected]]# ssh-agent $SHELL


[[email protected]]# ssh-add -L

The agent has no identities.



[[email protected]]# ssh-add

Identity added: /home/demo/.ssh/id_rsa (/home/demo/.ssh/id_rsa)



[[email protected]]# ssh-add -L

ssh-rsa AAAAB3NUaC1TR2SJKAABIwAAAQEAsJIEILuftj8aSxMa3k8t6JvM79DpBV

aHreqPShTYp7kISDMUNzUpnyxsHpH1tQ/Ow== /home/demo/.ssh/id_rsa



[[email protected]]# ssh-copy-id -i remote-host

[email protected]‘s password:

Now try logging into the machine, with "ssh ‘remote-host‘", and check in:



.ssh/authorized_keys



to make sure we haven‘t added extra keys that you weren‘t expecting.

[Note: This has added the key displayed by ssh-add -L]



本文出自 “奔跑在路上” 博客,請務必保留此出處http://qiangsh.blog.51cto.com/3510397/1964412

利用ssh-copy-id實現SSH無密碼登錄