ssh免祕鑰登錄

ssh 無密碼登錄要使用公鑰與私鑰。linux下可以用用ssh-keygen生成公鑰/私鑰對,下面我以CentOS爲例。

有機器A(192.168.1.155),B(192.168.1.181)。現想A通過ssh免密碼登錄到B。

1.在A機下生成公鑰/私鑰對。

[chenlb@A ~]$ ssh-keygen -t rsa -P ''


-P表示密碼,-P '' 就表示空密碼,(這裏的密碼是跳轉到B機器的時候需要的密碼,如果爲空就免祕鑰登錄了)也可以不用-P參數,這樣就要三車回車,用-P就一次回車。
它在/home/chenlb下生成.ssh目錄,.ssh下有id_rsa和id_rsa.pub。

2.把A機下的id_rsa.pub複製到B機下,在B機的.ssh/authorized_keys文件裏,我用scp複製。

[chenlb@A ~]$ scp .ssh/id_rsa.pub [email protected]:/home/chenlb/id_rsa.pub 
[email protected]'s password:
id_rsa.pub                                    100%  223     0.2KB/s   00:00


由於還沒有免密碼登錄的,所以要輸入密碼。

3.B機把從A機複製的id_rsa.pub添加到.ssh/authorzied_keys文件裏。

[chenlb@B ~]$ cat id_rsa.pub >> .ssh/authorized_keys
[chenlb@B ~]$ chmod 600 .ssh/authorized_keys


如果希望ssh公鑰生效需滿足至少下面兩個條件:

     1) .ssh目錄的權限必須是700 

     2) .ssh/authorized_keys文件權限必須是600



4.A機登錄B機。

[chenlb@A ~]$ ssh 192.168.1.181
The authenticity of host '192.168.1.181 (192.168.1.181)' can't be established.
RSA key fingerprint is 00:a6:a8:87:eb:c7:40:10:39:cc:a0:eb:50:d9:6a:5b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.181' (RSA) to the list of known hosts.
Last login: Thu Jul  3 09:53:18 2008 from chenlb
[chenlb@B ~]$


第一次登錄是時要你輸入yes。

現在A機可以無密碼登錄B機了。

小結:登錄的機子可有私鑰,被登錄的機子要有登錄機子的公鑰。這個公鑰/私鑰對一般在私鑰宿主機產生。上面是用rsa算法的公鑰/私鑰對,當然也可以用dsa(對應的文件是id_dsa,id_dsa.pub)

想讓A,B機無密碼互登錄,那B機以上面同樣的方式配置即可



可能還會提示輸入密碼的解決方法:

1) 如果出現報警:"Address X.X.X.X maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!"。

在"192.168.201.236"(連接端)服務器上執行如下命令:

echo "GSSAPIAuthentication no" >> ~/.ssh/config

在"192.168.201.237"(被連接端)服務器上執行"vi /etc/ssh/sshd_config"命令,修改下面兩項值爲"no" :

"GSSAPIAuthentication no" 

"UseDNS no"

2) 如果出現報警:"Agent admitted failure to sign using the key."執行命令:"ssh-add"(把專用密鑰添加到ssh-agent的高速緩存中)

如果還不行,執行命令:"ps -Af | grep agent "(檢查ssh代理是否開啓,如果有開啓的話,kill掉該代理)

然後執行"ssh-agent"(重新打開一個ssh代理)

如果還是不行,繼續執行命令:"sudo service sshd restart"(重啓一下ssh服務)

3) 通過命令"/usr/sbin/sestatus -v" 查看SELinux狀態,如果"SELinux status"參數爲"enabled"(開啓狀態),則關閉SELinux。

臨時關閉方法(不用重啓機器):"setenforce 0"修改配置文件關閉方法(需要重啓機器):執行命令"/etc/selinux/config",將"SELINUX=enforcing"改爲"SELINUX=disabled"

4) 執行命令"vim /etc/ssh/sshd_config"去掉下面三行的註釋:

RSAAuthentication yes

PubkeyAuthentication yes

AuthorizedKeysFile      .ssh/authorized_keys

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