ssh免密码登录,折腾了我一天~~~,如果用root权限的话挺简单的,但是这样着实有些不正规,毕竟我是专业的好伐~~
所以带来了很多奇怪的问题,当解决了的时候发现,其实也没那么麻烦。
以下是我最开始用root用户做的117单向无密码连接到118,119,120。这个是构建 hdfs-HA-Federation+yarn之前必须要做的。
在设置免密码登录之前,要把自己的hosts中配置好每一个ip对应的名字,方便将来切换和使用,将自己的hostname顺便也改了吧,不改也是可以的。
1. 首先要使用下面的命令来生成ssh免密码的密钥:
[root@localhost ~]# ssh-keygen -t rsa -P ''
一路回车,如果之前有过,可以选择yes覆盖之前的密钥~~
Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: ca:14:3d:fa:96:ba:41:17:c6:60:7d:dc:31:67:9e:b8 root@localhost.localdomain The key's randomart image is: +--[ RSA 2048]----+ | o. . .o.o | | . +. o .* . | | . *. . o | | + o . | | + S E | | + + . | | + + | | + | | o. | +-----------------+
2.进入该用户的根目录,会有隐藏的.ssh文件夹,进入后会看到有个id_rsa.pub,这个就是免密码的密钥。将这个密钥追加到authorized_keys中。
[root@localhost .ssh]# cat id_rsa.pub >> authorized_keys
必须将authorized_keys修改权限为600,说实话我也不知道为啥。
[root@localhost .ssh]# chmod 600 authorized_keys
3.不管用什么方式,将生成的密钥传给要免密码的服务器中(将密钥分别给到118,119,120)。把自己密钥给了哪台机器,自己就可以登录哪台机器
[root@localhost .ssh]# scp id_rsa.pub root@192.168.75.118:/home/id_rsa.pub
复制过去之后重复上面的操作,将传递过来的id_rsa.pub中的值追加到生成的authorized_keys(其实直接复制到authorized_keys中就可以,同时也要将权限设置为600)
4.在root权限下,编辑sshd_config文件:
[root@localhost .ssh]# vim /etc/ssh/sshd_config
将RSAAuthentication 和PubkeyAuthentication 的注释取消,并修改AuthorizedKeyFile的位置(这个位置是相对于进入用户的位置,而不是root下的位置)
RSAAuthentication yes #启用RSA认证 PubkeyAuthentication yes #启用公钥私钥配对认证方式
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2 # but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys# 公钥文件路径(和上面生成的文件同)
配置好之后重启ssh服务:
[root@localhost .ssh]# service sshd restart
Redirecting to /bin/systemctl restart sshd.service
5.测试一下吧~~
[root@qiang117 ~]# ssh node117 Last login: Tue Aug 18 07:48:58 2015 [root@qiang117 ~]# exit logout Connection to node117 closed. [root@qiang117 ~]# ssh node118 Last login: Tue Aug 18 07:46:59 2015 from node117 [root@qiang118 ~]# exit logout Connection to node118 closed. [root@qiang117 ~]# ssh node119 Last login: Tue Aug 18 07:47:17 2015 from node117 [root@localhost ~]# exit logout Connection to node119 closed. [root@qiang117 ~]# ssh node120 Last login: Tue Aug 18 07:47:39 2015 from node117 [root@localhost ~]# exit logout Connection to node120 closed. [root@qiang117 ~]#
这个是后来用非root用户做的免密码登录(需要注意的是,每个服务器的用户名字应该是一样的,事实证明不然连不上...)
[qiang@qiang117 ~]$ ssh node117 Last login: Tue Aug 18 08:59:20 2015 [qiang@qiang117 ~]$ ssh node118 Last login: Tue Aug 18 08:59:27 2015 from node117 [qiang@qiang118 ~]$ exit logout Connection to node118 closed. [qiang@qiang117 ~]$ ssh node119 Last login: Tue Aug 18 08:59:40 2015 from node117 [qiang@qiang119 ~]$ exit logout Connection to node119 closed. [qiang@qiang117 ~]$ ssh node120 Last login: Tue Aug 18 09:00:39 2015 from node120 [qiang@qiang120 ~]$ exit logout Connection to node120 closed. [qiang@qiang117 ~]$
创建好新的用户之后,在该用户根目录下重新获取密钥,这与root用户下生成的密钥是不一样的~