Linux使用ssh公钥实现免密码登录Linux

作者:邓聪聪

Linux使用ssh公钥实现免密码登录Linux

ssh 无密码登录要使用公钥与私钥。linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例。

有机器A(192.168.0.28),B(192.168.0.29)。现想A通过ssh免密码登录到B。
首先以root账户登陆为例。

1.在A机下生成公钥/私钥对。

[root@A ~]# ssh-keygen -t rsa -P ''

-P表示密码,-P '' 就表示空密码,也可以不用-P参数,这样就要三车回车,用-P就一次回车。
该命令将在/root/.ssh目录下面产生一对密钥id_rsa和id_rsa.pub。

一般采用的ssh的rsa密钥:
id_rsa     私钥
id_rsa.pub 公钥
下述命令产生不同类型的密钥
ssh-keygen -t dsa
ssh-keygen -t rsa
ssh-keygen -t rsa1

2.把A机下的/root/.ssh/id_rsa.pub 复制到B机的 /root/.ssh/authorized_keys文件里,先要在B机上创建好 /root/.ssh 这个目录,用scp复制。

[root@B ~]#mkdir -p /root/.ssh/

[root@A ~]# scp /root/.ssh/id_rsa.pub root@192.168.0.29:/root/.ssh/authorized_keys
root@192.168.0.29's password:
id_rsa.pub                                    100%  223     0.2KB/s   00:00

由于还没有免密码登录的,所以要输入一次B机的root密码。

3.authorized_keys的权限要是600!!!,默认为644.

[root@B ~]# chmod 600 /root/.ssh/authorized_keys

4.A机登录B机。

[root@A ~]# ssh -l root 192.168.0.29
The authenticity of host '192.168.0.29 (192.168.0.29)' 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.0.29' (RSA) to the list of known hosts.
Last login: Thu Jul  3 09:53:18 2008 from root
[root@B ~]#

第一次登录是时要你输入yes。
现在A机可以无密码登录B机了。

问题解决之道:

reverse mapping checking getaddrinfo for bogon [192.168.1.190] failed - POSSIBLE BREAK-IN ATTEMPT!

出现这样的提示,不会影响正常登陆,但是响应时间延长。原因:ssh 登录的时候会做一系列安全检查,其中有一项是 主机名与ip地址是否能解析,如果解析不了就会报这个错误。

如果你有dns服务器 ,在服务器上做解析也行。总之,ping主机名必须解析到对应的ip地址。

解决办法:

解决方法:在/etc/hosts 文件加上对方的 ip地址  主机名,可以ping通主机名即可。

例如ssh登录是从机器A,到机器B,机器B的IP地址为192.168.1.190,host为B,则在机器A的 /etc/hosts中添加 192.168.1.190 B 即可

posted @ 2018-01-18 13:13  邓聪聪  阅读(587)  评论(0编辑  收藏  举报