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參數,這樣就要三車回車,用-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 chenlb@192.168.1.181:/home/chenlb/id_rsa.pub 
chenlb@
192.168.1.181'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


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機以上面同樣的方式配置即可。


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