配置SSH无密码登录【原著】

环境:两台Centos虚拟机,配置了静态的ip。(详见虚拟机如何配置静态的IP地址的操作步骤)

192.168.75.21
192.168.75.22


第一步:为每台服务器配置静态IP地址
参见: 虚拟机中CentOS7设置固定IP地址的方法

第二步:为每台服务器配置hostname

192.168.75.21上,配置/etc/hostname中,添加“master01”内容
192.168.75.22上,配置/etc/hostname中,添加“slave02”内容

在192.168.75.21上,执行下面命令,设置hostname

[root@master01 ~]# vim /etc/hostname
master01

在192.168.75.22上,执行下面命令,设置hostname

[root@slave02 ~]# vim /etc/hostname
slave02


第三步:配置hosts文件,新增两条映射规则

192.168.75.21 master01
192.168.75.22 slave02


以下开始配置SSH无密码登录

第四步:分别在master01和slave02上,执行命令:

#ssh-keygen -t rsa,一路回车,在~/.ssh/目录下产生2个文件,分别是id_rsa和id_rsa.pub

说明:id_rsa是私钥,id_rsa.pub是公钥
私钥,需要各自机器妥善保管。
公钥,需要添加到许可证文件authorized_keys中,保证每一台机器上的公钥都在许可证文件authorized_keys中。

注意 许可证文件的路径:~/.ssh/authorized_keys


第五步:将master01的公钥文件内容,添加到许可证文件 ~/.ssh/authorized_keys 中
使用这个命令:
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

说明:这个重定向符号“>>”会将命令执行后产生的标准输出重定向附加到(目标文件)该文件之后。注意,是附加到文件后面。


第六步:在slave02上,将slave02的公钥文件,添加到master01的许可证文件 ~/.ssh/authorized_keys中。.

ssh-copy-id -i ~/.ssh/id_rsa.pub root@master01

[root@slave02 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@master01
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@master01's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'root@master01'"
and check to make sure that only the key(s) you wanted were added.


暂时各个服务器不能免密操作,根据提示,输入master01的登录密码,然后,完成上面命令。


第七步:在master01上,将master01上的许可证文件拷贝到 slave02上


[root@master01 ~]# scp ~/.ssh/authorized_keys root@slave02:~/.ssh/authorized_keys
root@slave02's password:
authorized_keys 100% 789 156.8KB/s 00:00

第八步.在master01上,使用ssh 'root@slave02'命令,测试可以直接免密直接切换到slave02上。

[root@master01 ~]# ssh 'root@slave02'
Last login: Mon Jun 24 19:24:33 2019 from master01
[root@slave02 ~]#

第九步,在slave02上,使用ssh 'root@master01'命令,测试可以直接免密直接切换到master01上。

[root@slave02 ~]# ssh 'root@master01'
Last login: Mon Jun 24 19:25:03 2019 from slave02
[root@master01 ~]#

完成!!!

 参考文章:【完全分布式Hadoop】(三)集群设置SSH无密码登陆

猜你喜欢

转载自www.cnblogs.com/wangqiideal/p/11079145.html
今日推荐