阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Linux快速配置集群SSH互信

121次阅读
没有评论

共计 3348 个字符,预计需要花费 9 分钟才能阅读完成。

之前在《记录一则 Linux SSH 的互信配置过程》、《Vertica 7.1 安装最佳实践(RHEL6.4)》中,都分别提到了配置 ssh 互信的方法,本文在此基础上进一步整理配置 ssh 互信的方法,目的是将步骤尽可能的简化,从而更加适合在较大规模的集群中对 ssh 互信进行快速配置。

场景:适合较大规模集群 ssh 互信配置.

  • 1. 配置节点 1 的 /etc/hosts 文件
  • 2. 在节点 1 新建 2 个脚本
  • 3. 配置节点 1 的环境变量
  • 4. 配置整个集群间的 ssh 互信

1. 配置节点 1 的 /etc/hosts 文件

节点 1 编辑 /etc/hosts 文件,添加集群所有主机的 IP 地址和其对应的主机名:
vi /etc/hosts

192.168.56.102 JY-DB
192.168.56.103 JY-DB2

2. 在节点 1 新建 2 个脚本

在节点 1 上传两个安装脚本到 /usr/local/bin 目录下。

脚本 1:用来集群间同步拷贝文件。
cluster_copy_all_nodes

#!/bin/bash
SELF=`hostname`
if [-z "$NODE_LIST" ]; then
  echo
  echo Error: NODE_LIST environment variable must be set in .bash_profile
  exit 1
fi

for i in $NODE_LIST; do
  if [! $i = $SELF ]; then
    if [$1 = "-r" ]; then
      scp -oStrictHostKeyChecking=no -r $2 $i:$3
    else
      scp -oStrictHostKeyChecking=no $1 $i:$2
    fi
  fi
done
wait

脚本 2:用来集群间同步运行命令。
cluster_run_all_nodes

#!/bin/bash
if [-z "$NODE_LIST" ]; then
  echo
  echo Error: NODE_LIST environment variable must be set in .bash_profile
  exit 1
fi

if [[$1 = '--background' ]]; then
  shift
  for i in $NODE_LIST; do
    ssh -oStrictHostKeyChecking=no -n $i "$@" &
  done
else
  for i in $NODE_LIST; do
    ssh -oStrictHostKeyChecking=no $i "$@"
  done
fi
wait

授予两个脚本的可执行权限:

chmod +x /usr/local/bin/cluster_*

3. 配置节点 1 的环境变量

配置节点 1 的环境变量:
vi ~/.bash_profile

export NODE_LIST='JY-DB JY-DB2'

将集群中所有的主机名称列出,然后重新登录当前会话,或者执行下面命令使环境变量生效:

source ~/.bash_profile

4. 配置整个集群间的 ssh 互信

4.1 各节点 ssh-keygen 生成 RSA 密钥和公钥

cluster_run_all_nodes "hostname; ssh-keygen -q -t rsa  -N \"\"-f  ~/.ssh/id_rsa"

输出示例:

[root@JY-DB bin]# cluster_run_all_nodes "hostname ; ssh-keygen -q -t rsa  -N \"\"-f  ~/.ssh/id_rsa"
root@jy-db's password: 
JY-DB
root@jy-db2's password: 
JY-DB2

如果配置有误,或者清除 ssh 互信的当前所有配置信息:

cluster_run_all_nodes "hostname ; rm -rf ~/.ssh"
rm -rf ~/.ssh

4.2 将所有的公钥文件汇总到一个总的授权 key 文件中
在 192.168.56.102 执行汇总:
编辑脚本内容:

IP_NET="192.168.56."
for((i=102;i<=103;i++))
do
ssh $IP_NET$i cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
echo Summarize ssh info from $IP_NET$i into a single file.
done

注意:IP_NET 是定义网段的变量,for 循环包含了整个集群的 IP 范围,根据实际情况修改。

上述脚本内容是可以直接拷贝执行的,结果示例:

[root@JY-DB ~]# IP_NET="192.168.56."
[root@JY-DB ~]# for((i=102;i<=103;i++))
> do
> ssh $IP_NET$i cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
> echo Summarize ssh info from $IP_NET$i into a single file.
> done
root@192.168.56.102's password: 
Summarize ssh info from 192.168.56.102 into a single file.
root@192.168.56.103's password: 
Summarize ssh info from 192.168.56.103 into a single file.

出于安全性考虑,将这个授权 key 文件赋予 600 权限:

chmod 600 ~/.ssh/authorized_keys

4.3 将这个包含了所有互信机器认证 key 的认证文件,分发到各个机器中去

cluster_copy_all_nodes ~/.ssh/authorized_keys ~/.ssh/

4.4 验证 ssh 互信
节点 1 运行,都不输入密码返回主机名和时间即可:

cluster_run_all_nodes "hostname;date"

至此,ssh 集群间的互信已经配置完成。
但为了更加灵活的再其他节点也可以用到我们自定义的脚本,我们还可以做以下工作:

同步拷贝节点 1 的配置文件到其他节点:

cluster_copy_all_nodes ~/.bash_profile ~/
cluster_copy_all_nodes /etc/hosts /etc
cluster_copy_all_nodes /usr/local/bin/cluster_copy_all_nodes /usr/local/bin/
cluster_copy_all_nodes /usr/local/bin/cluster_run_all_nodes /usr/local/bin/

这时任意登录其他节点,也可以使用 cluster_run_all_nodes 验证 ssh 互信了:

cluster_run_all_nodes "hostname;date"

下面关于 SSH 相关的文章您也可能喜欢,不妨参考下:

Ubuntu 下配置 SSH 服务全过程及问题解决 http://www.linuxidc.com/Linux/2011-09/42775.htm

Ubuntu 14.04 下安装 Samba 及 SSH 服务端的方法 http://www.linuxidc.com/Linux/2015-01/111971.htm

SSH 服务远程访问 Linux 服务器登陆慢 http://www.linuxidc.com/Linux/2011-08/39742.htm

提高 Ubuntu 的 SSH 登陆认证速度的办法 http://www.linuxidc.com/Linux/2014-09/106810.htm

开启 SSH 服务让 Android 手机远程访问 Ubuntu 14.04  http://www.linuxidc.com/Linux/2014-09/106809.htm

如何为 Linux 系统中的 SSH 添加双重认证 http://www.linuxidc.com/Linux/2014-08/105998.htm

在 Linux 中为非 SSH 用户配置 SFTP 环境 http://www.linuxidc.com/Linux/2014-08/105865.htm

Linux 上 SSH 服务的配置和管理 http://www.linuxidc.com/Linux/2014-06/103627.htm

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-09/135430.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计3348字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中