centos7配置集群各服务器之间的ssh免密登录

  • Post author:
  • Post category:其他


这里默认你们都有多台服务器

以下是我的集群配置,已经搭建好了5台服务器

配置集群hosts
192.168.74.205 nn1
192.168.74.206 nn2
192.168.74.207 s1
192.168.74.208 s2
192.168.74.209 s3

所有操作不要以root用户操作,新建其他用户

这里我都是以hadoop用户操作


hadoop

是我所有服务器都有的用户

从第一台nn1配置开始

执行

ssh-keygen -t rsa

命令

[hadoop@nn1 ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hadoop/.ssh/id_rsa):
Created directory '/home/hadoop/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/hadoop/.ssh/id_rsa.
Your public key has been saved in /home/hadoop/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:9qCZ967fj92ntDNbinxxUkYc7xzjWN3DPubPa9QDdLw hadoop@nn1.hadoop
The key's randomart image is:
+---[RSA 2048]----+
|              .o.|
|             ..o*|
|            . .B=|
|             .=E*|
|        S    ..B+|
|       = o    =o+|
|      + . .   o=o|
|       . . o =+Bo|
|        .++ =oO**|
+----[SHA256]-----+
[hadoop@nn1 ~]$ cd .ssh/
[hadoop@nn1 .ssh]$ ll
total 8
-rw-------. 1 hadoop hadoop 1679 Sep  3 04:04 id_rsa
-rw-r--r--. 1 hadoop hadoop  399 Sep  3 04:04 id_rsa.pub
[hadoop@nn1 .ssh]$

创建公钥存放文件夹 各集群服务器的公钥

[hadoop@nn1 ~]$ mkdir sshkey

将公钥复制到一个文件,文件名随便,你自己好用就行 并存放于sshkey文件夹中

[hadoop@nn1 ~]$ cp ~/.ssh/id_rsa.pub ./sshkey/nn1sshkey

切换到sshkey目录将nn1sshkey追加到授权中authorized_keys

[hadoop@nn1 ~]$ cd ./sshkey
[hadoop@nn1 sshkey]$ cat nn1sshkey >> authorized_keys

更改authorized_keys改文件的权限

[hadoop@nn1 ~]$ cd ./sshkey
[hadoop@nn1 sshkey]$ chmod 600 authorized_keys

第一台服务器的公钥收集完毕

接着收集nn2的公钥


第二台


登录到nn2

[hadoop@nn1 ~]$ ssh nn2.hadoop
输入yes
密码

生成密钥,注意这里已经登录到nn2服务器了

[hadoop@nn2 ~]$ ssh-keygen -t rsa

将公钥复制到nn1的sshkey文件夹并命名为nn2sshkey

[hadoop@nn2 ~]$ cd .ssh/
[hadoop@nn2 .ssh]$ scp ./id_rsa.pub hadoop@nn1.hadoop:~/sshkey/nn2sshkey

退出

exit


回到nn1的sshkey目录

将nn2sshkey追加到授权中

cat nn2sshkey >> authorized_keys

[hadoop@nn1 sshkey]$ ll
-rw-------. 1 hadoop hadoop 1196 Sep  3 04:56 authorized_keys
-rw-r--r--. 1 hadoop hadoop  399 Sep  3 04:33 nn1sshkey
-rw-r--r--. 1 hadoop hadoop  399 Sep  3 04:50 nn2sshkey

[hadoop@nn1 sshkey]$ cat nn2sshkey >> authorized_keys

其他服务器重复第二台步骤

所有服务器公钥收集完毕后

回到nn1服务器

sshkey的目录如下

[hadoop@nn1 sshkey]$ ll
total 24
-rw-------. 1 hadoop hadoop 1594 Sep  3 04:59 authorized_keys
-rw-r--r--. 1 hadoop hadoop  399 Sep  3 04:33 nn1sshkey
-rw-r--r--. 1 hadoop hadoop  399 Sep  3 04:50 nn2sshkey
-rw-r--r--. 1 hadoop hadoop  398 Sep  3 04:56 s1sshkey
-rw-r--r--. 1 hadoop hadoop  398 Sep  3 04:59 s2sshkey
-rw-r--r--. 1 hadoop hadoop  398 Sep  3 05:02 s3sshkey

authorized_keys的文件内容如下

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDQssT/VDEFMjlMKvsjr/E0wwtBkNET51J9dhSUO+g5OzoMHst/yZQUxFkEQae5piw29ZVREejwdtTBYyJ5yTy2Fb8SisaBMfnUldXk9d1O9q7q+ZxJ/V3Q8kX4mws8KtMX8ma3Dl+a2gjDWjer7k9WucfvClyJXo0iLh00AlEsvHo33O0lfWDwnfKHOQlug7+ITjBSAi188Fo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itdNpnm+jE2cPaXohxqzj1KnQRD53Zv/JBF/PlkHx4MTwIYuv82mu5VFnO2dgaqW+JO/acRtRnBEqVdakPetc0E7LF01EMWjN32nUTMxETEX7BO9kLXQm8sCiAffiSOVK3tmXXrcCsfjP1MqpWakCIE1MV20og13uW5EpClmb7YHgi/x4unEOIops8n5SwMNWiuvO3CsVtO+dlWsY4ScX5QfjXS9PvYirbP3g7mLXGC6ozQYf3 hadoop@s2.hadoop
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD8hI4G1tH+aOMK9IqAPkBoZuSWngPaFGu5ZKL4gN0kH/2xt7lZ5juYfo1DvHFrWlInB0Y9JBQsBdGpU2t6yh7JfZVXVjkehw0R3jIa+9gSC3XwDxNEW+DeoU/l+r04JLfV/wRpVUc6FvkkHbHX+qazBPmFm5Io6Inp1wXWEej1tnVcFvXrcRCHxJhIDLSSTTbFwnooBo/S8LNqUTG8O4ra1c4ccmJss2IqrTCMbZeK7gS3uXgH1+9JBiTukj0oe8gATmfrFJtLU4J4bKh3WNmUVPv3V0BKc2OQNFVQDWKYWbyYFbVM5TwqePpTrygQ/q/m7gtkUAohw9PCFdaxF2Af hadoop@s3.hadoop

将公钥分发下去,将sshkey文件夹下的authorized_keys复制到.ssh文件夹下

[hadoop@nn1 sshkey]$ scp ./authorized_keys hadoop@nn1.hadoop:~/.ssh/
[hadoop@nn1 sshkey]$ ll ~/.ssh/
total 16
-rw-------. 1 hadoop hadoop 1992 Sep  3 05:08 authorized_keys
-rw-------. 1 hadoop hadoop 1679 Sep  3 04:04 id_rsa
-rw-r--r--. 1 hadoop hadoop  399 Sep  3 04:04 id_rsa.pub
-rw-r--r--. 1 hadoop hadoop  932 Sep  3 05:08 known_hosts

同理

[hadoop@nn1 sshkey]$ scp ./authorized_keys hadoop@nn2.hadoop:~/.ssh/
[hadoop@nn1 sshkey]$ scp ./authorized_keys hadoop@s1.hadoop:~/.ssh/
[hadoop@nn1 sshkey]$ scp ./authorized_keys hadoop@s2.hadoop:~/.ssh/
[hadoop@nn1 sshkey]$ scp ./authorized_keys hadoop@s3.hadoop:~/.ssh/

测试

[hadoop@s3 ~]$ ssh nn1.hadoop
Last login: Thu Sep  3 04:03:42 2020 from 192.168.74.1
[hadoop@nn1 ~]$ ssh nn2.hadoop
Last login: Thu Sep  3 05:14:28 2020 from nn1.hadoop
[hadoop@nn2 ~]$ ssh s1.hadoop
Last login: Thu Sep  3 05:14:52 2020 from nn2.hadoop
[hadoop@s1 ~]$ ssh nn1.hadoop
Last login: Thu Sep  3 05:15:51 2020 from s3.hadoop

各服务器之间已经不用密码登录其他服务器了

在这里插入图片描述



版权声明:本文为weixin_43606738原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。