歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> linux下如何實現ssh無密碼驗證登陸

linux下如何實現ssh無密碼驗證登陸

日期:2017/3/3 15:53:01   编辑:關於Linux

ssh配置

主機A:10.0.5.199

主機B:10.0.5.198

需要配置主機A無密碼登錄主機A,主機B

先確保所有主機的防火牆處於關閉狀態。

在主機A上執行如下:

1. $cd ~/.ssh

2. $ssh-keygen -t rsa ------------------然後一直按回車鍵,就會按照默認的選項將生成的私鑰保存在.ssh/id_rsa文件中,生成的公鑰保存在.ssh/id_rsa.pub中。

3. $cp id_rsa.pub authorized_keys

這步完成後,正常情況下就可以無密碼登錄本機了,即ssh localhost,無需輸入密碼。

4. $scp authorized_keys [email protected]:/home/summer/.ssh ------把剛剛產生的authorized_keys文件拷一份到主機B上.  

5. $chmod 600 authorized_keys

進入主機B的.ssh(chmod 700 .ssh)目錄,改變authorized_keys文件的許可權限。

(4和5可以合成一步,執行: $ssh-copy-id -i [email protected] )

正常情況下上面幾步執行完成後,從主機A所在機器向主機A、主機B所在機器發起ssh連接,只有在第一次登錄時需要輸入密碼,以後則不需要。

可能遇到的問題:

1.進行ssh登錄時,出現:”Agent admitted failure to sign using the key“ .

執行: $ssh-add 強行將私鑰 加進來。

2.如果無任何錯誤提示,可以輸密碼登錄,但就是不能無密碼登錄,在被連接的主機上(如A向B發起ssh連接,則在B上)執行以下幾步:

$chmod o-w ~/

$chmod 700 ~/.ssh

$chmod 600 ~/.ssh/authorized_keys

3.如果執行了第2步,還是不能無密碼登錄,再試試下面幾個

$ps -Af | grep agent

檢查ssh代理是否開啟,如果有開啟的話,kill掉該代理,然後執行下面,重新打開一個ssh代理,如果沒有開啟,直接執行下面:

$ssh-agent

還是不行的話,執行下面,重啟一下ssh服務

$sudo service sshd restart

4. 執行ssh-add時提示“Could not open a connection to your authenticationh agent”而失敗

執行: ssh-agent bash

本欄目更多精彩內容:http://www.bianceng.cn/OS/Linux/

Copyright © Linux教程網 All Rights Reserved