注意:本文档配置的为root用户的SSH免密。

资源规划

组件 LTSR003 LTSR005 LTSR006 LTSR007 LTSR008
OS centos7.6 centos7.6 centos7.6 centos7.6 centos7.6

SSH免密配置

Φ 生成公钥、私钥

1.以[root]用户登录产生密钥。(集群所有机器执行脚本

  1. # 所有节点切换到root
  2. su root
  3. cd ~
  4. ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa

注:以rsa算法,生成公钥、私钥对,-P ‘’表示空密码。该命令运行完后,会在个人主目录下生成.ssh目录,里面会有二个文件idrsa(私钥) ,id_rsa.pub(公钥)。
2.导入authorized_keys。(
节点1**执行脚本_**)

  1. cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
  2. chmod 600 ~/.ssh/authorized_keys
  3. chmod 600 ~/.ssh/id_rsa

3.[root]用户设置SSH配置文件权限。(集群所有机器执行脚本

  1. chmod 766 /etc/ssh/sshd_config

4.[root]用户修改SSH配置文件。(集群所有机器执行脚本

  1. sudo vi /etc/ssh/sshd_config

配置如下:

  1. RSAAuthentication yes ## 启用 RSA 认证
  2. PasswordAuthentication yes ## 开启ssh密码登陆
  3. PubkeyAuthentication yes ## 启用公钥私钥配对认证方式
  4. PermitRootLogin yes ## 不限制登录方式
  5. AuthorizedKeysFile .ssh/authorized_keys ## 公钥文件路径(和上面生成的文件同)

设置完之后记得重启SSH服务,才能使刚才设置有效。

sudo systemctl restart sshd.service
sudo systemctl status sshd.service
sudo systemctl start sshd.service
sudo systemctl enable sshd.service # 随开机启动
sudo systemctl disabled sshd.service
sudo systemctl stop sshd.ervice

5.验证(节点1执行脚本

ssh localhost
ssh LTSR003

Φ 其它机器生成公钥、密钥

  1. 以root用户身份登录其它几个节点生成公钥、密钥(如果此步骤在上一步骤中已做,略过)。

    ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
    
  2. 然后用scp命令,把其他节点公钥文件发放给节点1。

    ## 节点2节点上运行
    scp ~/.ssh/id_rsa.pub root@LTSR003:~/id_rsa_LTSR005.pub
    ## 节点3节点上运行
    scp ~/.ssh/id_rsa.pub root@LTSR003:~/id_rsa_LTSR006.pub
    ## 节点4节点上运行
    scp ~/.ssh/id_rsa.pub root@LTSR003:~/id_rsa_LTSR007.pub
    ## 节点5节点上运行
    scp ~/.ssh/id_rsa.pub root@LTSR003:~/id_rsa_LTSR008.pub
    

    Φ 合并所有节点密钥

    在节点1上将其他节点上传的id_rsa_xx.pub,导入authorized_keys。

    cat ~/id_rsa_LTSR005.pub >> ~/.ssh/authorized_keys
    cat ~/id_rsa_LTSR006.pub >> ~/.ssh/authorized_keys
    cat ~/id_rsa_LTSR007.pub >> ~/.ssh/authorized_keys
    cat ~/id_rsa_LTSR008.pub >> ~/.ssh/authorized_keys
    

    经过这个步骤,节点1上拥有所有集群机器的authorized_keys。

    Φ 分发公钥集合

  3. 在节点1上分发authorized_keys。

    ## 将节点1上的“最全”公钥,复制到其它机器
    scp ~/.ssh/authorized_keys root@LTSR005:~/.ssh/authorized_keys
    scp ~/.ssh/authorized_keys root@LTSR006:~/.ssh/authorized_keys
    scp ~/.ssh/authorized_keys root@LTSR007:~/.ssh/authorized_keys
    scp ~/.ssh/authorized_keys root@LTSR008:~/.ssh/authorized_keys
    
  4. 修改其它节点上authorized_keys文件的权限。

    chmod 600 ~/.ssh/authorized_keys
    chmod 600 ~/.ssh/id_rsa
    chmod g-w ~/.ssh
    chmod 700 ~/.ssh
    

    Φ 验证

    # 各节点相互登录验证
    ssh LTSR003
    ssh LTSR005
    ssh LTSR006
    ssh LTSR007
    ssh LTSR008
    exit