Linux公鑰ssh免密登錄

(一)問題:

假如我們現在有兩臺機器:ServerA和ServerB,現在想要讓ServerA不用輸入密碼就能夠進行訪問。

(二)方法和原理:

我們使用ssh-keygen在ServerA上生成private和public密鑰,將生成的public密鑰拷貝到遠程機器ServerB上後,就可以使用ssh命令無需密碼登錄到另外一臺機器ServerB上。

在linux系統中,ssh是遠程登錄的默認工具,因爲該工具的協議使用了RSA/DSA的加密算法【默認是DSR算法】,該工具做linux系統的遠程管理是非常安全的。

(三)實驗步驟:

1.登錄ServerA

2.ssh-keygen -t rsa,將會生成公鑰和私鑰文件id_rsa和id_rsa.pub【如果一直回車下去,最後這兩個文件應該在/home/$USER/.ssh下面】

3.將 .pub 文件複製到ServerB機器的 .ssh 目錄下,並保存爲authorized_keys

可以使用:

ssh-cop-id命令會將指定的公鑰文件複製到遠程計算機

[oracle@Test232 ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
28
[email protected]'s password: 
Now try logging into the machine, with "ssh '[email protected]'", and check in:


  .ssh/authorized_keys


to make sure we haven't added extra keys that you weren't expecting.
[oracle@Test232 ~]$ ssh [email protected]
[email protected]'s password: 
Last login: Thu Nov 24 16:05:32 2011 from 192.168.55.229
[oracle@Test232 ~]$ 

4.大功告成,從A機器登錄B機器的目標賬戶,不再需要密碼了

5.設置文件和目錄權限【這一步可以省略,但是爲了安全起見,加上也是有必要的~】

設置authorized_keys權限

chmod 644 authorized_keys

設置.ssh目錄權限

chmod 700 -R .ssh

6.要保證.ssh和authorized_keys都只有用戶自己有寫權限。否則驗證無效。(今天就是遇到這個問題,找了好久問題所在),其實仔細想想,這樣做是爲了不會出現系統漏洞。

報錯:

The authenticity of host ‘192.168.20.59 (192.168.20.59)’ can’t be established.
RSA key fingerprint is 6a:37:c0:e1:09:a4:29:8d:68:d0:ca:21:20:94:be:18.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.20.59’ (RSA) to the list of known hosts.
[email protected]’s password:
Permission denied, please try again.
[email protected]’s password:
Permission denied, please try again.
[email protected]’s password:
Permission denied (publickey,gssapi-with-mic,password).

如果說以上權限沒有問題的話,那就趕緊去看看你的配置文件吧,因爲有人會喜歡改這個東東,導致你無論如何都不會成功:

打開/etc/ssh/ssh_config文件,找到以下部分:

IdentityFile ~/.ssh/identity

IdentityFile ~/.ssh/id_rsa

看看你的私鑰名字是不是這個 ~/.ssh/id_rsa

哈哈,還是得好好看配置文件嘍~

假如有臺中控機已經打通了到各臺應用主機間的無密碼遠程登陸,現在需要打通另一臺中控,下面是我寫的腳本來實習此功能:

############################################ 實現中控無密碼登陸 ############################################

#!/bin/bash
#Author:zhuying

scriptdir=/home/oracle/zy/changecps

for ip in grep -v ^# "$scriptdir"/cps.ip
do
scp /home/oracle/zy/changecps/169keys oracle@$ip:~/ </dev/null
ssh $ip “cat ~/169keys >> /home/oracle/.ssh/authorized_keys;rm ~/169keys” </dev/null
ssh $ip “cat /home/oracle/.ssh/authorized_keys|sort|uniq > /home/oracle/.ssh/tmp.keys” </dev/null
ssh $ip “mv /home/oracle/.ssh/tmp.keys /home/oracle/.ssh/authorized_keys” </dev/null
ssh $ip “chmod 644 /home/oracle/.ssh/authorized_keys” </dev/null
done

########################################### 實現中控無密碼登陸 ##############################################

ps:

如果我們添加公鑰後,還是無法訪問,也可能是ServerA主機上面的一個文件known_hosts中已經存在ServerB的ip信息,刪除重新訪問即可。

另外,如果以上方法嘗試過後還是不行,可能是因爲登錄失敗次數過多被鎖定,這個時候我們就需要去看看我們的系統日誌了/var/log/messages,將之前的鎖定信息日誌刪除就可以了。

參考文章:

http://os.51cto.com/art/200812/101989_1.htm

問題:

(1)Connection closed by $IP

可能是超過系統默認失敗次數了,然後清理就ok了~【faillog -a;faillog -r】

(2)在前面步驟都正確無誤的情況下,每次遠程登錄還是提示讓輸入密碼,這時候有可能是權限問題:

chmod 644 authorized_keys再次登錄就ok了~【如果是Red Hat 5.6的話,最好改成600,否則認爲是不安全的~】

原文:https://blog.csdn.net/zhuying_linux/article/details/7049078

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章