歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 0049 分布式中ssh本地登陸和兩台電腦相互登陸配置方法

0049 分布式中ssh本地登陸和兩台電腦相互登陸配置方法

日期:2017/3/3 12:38:44   编辑:Linux技術

文章轉載自:http://chenlb.iteye.com/blog/211809

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參數,這樣就要三車回車,用-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

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

Copyright © Linux教程網 All Rights Reserved