ssh 無密碼登陸要使用公鑰與私鑰。linux下能夠用用ssh-keygen生成公鑰/私鑰對,下面我以CentOS爲例。
有機器A(192.168.1.155),B(192.168.1.181)。現想A經過ssh免密碼登陸到B。
1.在A機下生成公鑰/私鑰對。linux
[chenlb@A ~]$ ssh-keygen -t rsa -P ''算法
-P表示密碼,-P '' 就表示空密碼,也能夠不用-P參數,這樣就要三車回車,用-P就一次回車。
它在/home/chenlb下生成.ssh目錄,.ssh下有id_rsa和id_rsa.pub。
2.把A機下的id_rsa.pub複製到B機下,在B機的.ssh/authorized_keys文件裏,我用scp複製。ssh
[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:00spa
因爲尚未免密碼登陸的,因此要輸入密碼。
3.B機把從A機複製的id_rsa.pub添加到.ssh/authorzied_keys文件裏。orm
[chenlb@B ~]$ cat id_rsa.pub >> .ssh/authorized_keys
[chenlb@B ~]$ chmod 600 .ssh/authorized_keysblog
authorized_keys的權限要是600。
4.A機登陸B機。ci
[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 ~]$get
第一次登陸是時要你輸入yes。
如今A機能夠無密碼登陸B機了。
小結:登陸的機子可有私鑰,被登陸的機子要有登陸機子的公鑰。這個公鑰/私鑰對通常在私鑰宿主機產生。上面是用rsa算法的公鑰/私鑰對,固然也能夠用dsa(對應的文件是id_dsa,id_dsa.pub)
想讓A,B機無密碼互登陸,那B機以上面一樣的方式配置便可。
參考:SSH-KeyGen 的用法 http://blog.163.com/chen98_2006@126/blog/static/158584272007101862513886/it