Linux多节点间的免密码登录设置

四台主机(以centos为例):

192.168.80.129  master

192.168.80.130  worker1

192.168.80.131  worker2

192.168.80.132  worker3

修改主机名

修改主机名,分别为 master worker1 worker2 worker3

vi /etc/hostname

退出编辑  :wq

显示主机名

hostname

修改ip主机名映射

vi /etc/hosts

重启计算机

reboot

配置ssh

外网环境下,安装ssh

sudo yum -y install openssh-server

查看ssh是否安装成功

ps -e|grep ssh

结果

为方便操作,通过配置 SSH 无密码验证,实现各节点间的无密码通信。

总体思路

  1. 每台机器生成秘钥
  2. 将每台机器的公钥复制到master节点上
  3. master将公钥追加到authorized_keys中
  4. master再讲authorized_keys分发给其余节点

生成密钥,其中,'' 表示两个单引号(四台主机都需要执行)

ssh-keygen[Enter][Enter][Enter]

此时,在 /root/.ssh (如果不是root用户,路径为:/root/${username}/.ssh)中生成了两个密钥文件:id_rsa和id_rsa.pub,其中id_rsa为私钥,id_rsa.pub 为公钥。

需要将 id_rsa.pub 追加到 authorized_keys 中,authorized_keys 用于保存所有允许以当前用户身份登录到 ssh 客户端用户的公钥内容。

master中

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

查看现在能否免密码登录 ssh

ssh localhost

输入“yes”继续登录。完成后,输入 “exit”退出 localhost。再次登录使就不需要密码了。

在worker节点中,将公钥拷贝到master

worker1节点

scp ~/.ssh/id_rsa.pub [email protected]:/root/.ssh/id_rsa.pub.worker1

worker2节点

scp ~/.ssh/id_rsa.pub [email protected]:/root/.ssh/id_rsa.pub.worker2

worker3节点

scp ~/.ssh/id_rsa.pub [email protected]:/root/.ssh/id_rsa.pub.worker3

master 中将公钥追加到authorized_key

cat ~/.ssh/id_rsa.pub.worker1 >> ~/.ssh/authorized_keys
cat ~/.ssh/id_rsa.pub.worker2 >> ~/.ssh/authorized_keys
cat ~/.ssh/id_rsa.pub.worker3 >> ~/.ssh/authorized_keys

master中将authorized_keys拷贝到其他节点

scp ~/.ssh/authorized_keys [email protected]:/root/.ssh/authorized_keys
scp ~/.ssh/authorized_keys [email protected]:/root/.ssh/authorized_keys
scp ~/.ssh/authorized_keys [email protected]:/root/.ssh/authorized_keys

master节点中

ssh master
ssh worker1
ssh worker2
ssh worker3

 

你可能感兴趣的:(Linux多节点间的免密码登录设置)