SSH免密码登录(RSA)

环境介绍:
192.168.74.11    server1
192.168.74.12    server2
192.168.74.13    server3

# 步骤一:
# 每个节点执行
[root@localhost ~]#  cat >>/etc/hosts < 192.168.74.11    server1
192.168.74.12    server2
192.168.74.13    server3
EOF
# 每个节点执行
ssh-keygen -t rsa   # 一路回车
# 将公钥添加到认证文件中
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
# 并设置authorized_keys的访问权限
chmod 600 ~/.ssh/authorized_keys

# 拷贝公钥

ssh-copy-id -i 192.168.74.11 #登录server2,将公钥拷贝到server1的authorized_keys中

ssh-copy-id -i 192.168.74.11 #登录server3,将公钥拷贝到server1的authorized_keys中

# 注意第一次ssh登录时需要输入密码,再次访问时即可免密码登录。

最终authorized_keys文件内容如下:

more ~/.ssh/authorized_keys

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC/zyHwbBRxeB6BqXgecoWnWlNcXKqbdF+Pk1X1pNxXdXCNQrsaJQ54j+YzFNZ5IzxSEQ6yVTXSBqcYYilPMck2zMSlH6gU4+h1++LKHQwksQ1WUh+uF5ExBodyZOWQ7dKKqAu
kE/ToiWJ1waFLW9TdHjPkYKdoqzm/Yir7FI8G51WU6IRFvYQ84cPjEMRdUmwrHz7RUaxH4Lh+AFFwTVNAW24lYO0KbhrOV7KA/iMfLSYgWBtyMihWv/J2j6OB1ziN2aXQA6DcsQ2nQnPAB4bzWDPKRqz8SBTgzqqpm1jEm3P9vV
+1OLz4Keg76C9hGRzEZCP8mzdEZKiz+04PG4Xd root@master-node
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCeSVRh0ghRuYX7KNev5rG0S2O5ljqWvMSi6sEzG9/AVFy1IjD3QMW7d0fkWkEtU6IG1WZIT5Q8pfI3ylUuaHBRzJhlHTjMcY7mT01T7Y5W2BUzg9BDIdE3StKsCYARuqi66jU
s+4ew3jDLPkMct+PdRdWpp3gpylQXQGuvPrLDRBKPbeiCa13TjMDhIptq3KqHaha50qco2p89Rvk84AT2G1Xe21Go6lUQ85N5T0LMsAUKk45LA5QUNQMKpm5dlrrgYuAbss9t2VZEQFhoCAuB8swhzUpJzTmmgSoMWI+qD/0uLv
vPLwBkKdI9ABOhRRUoSEywGigKaSynOY1UtAot root@master-node
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCxBAvtkFfNQMLsaNtyJfjI5cLHb+E75VAN1X0s1ZBp4Cy61HB7vI6ujkJZJA3NE2hB1HRWr01r5BWr0ROuzVVo3deErfFnuZlrcsrs32yBK2y4+ytAcgpcviIsVuFJHBTcmj2
ZNGRxGqhqK1nBLhLQKDCk1FCw/orj2FsBY/mjvozBULEWgz0bZYx7Vh3qRmFjJmJlb6b2KVqTBLPjaaPFbOS5Zbb8/JSwgjqLDD1QKITNVFlJXSWSCbNiZ+ZIaXsF1ac47/YYeQUmsTKcys40MkZBgZiwvnik8B4Dc372f6kbez
Sn93zP04E80gPxNR377BsxoIrDhba6idw0ALlv root@data-node1
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDMJO1QTBeNPEPuJysP3PQcbEdV1Fkdsk/qTgHQ+zczShDpp08T/W2wjeJxt3zdA1L4KF9NOaQgZpN79CITJuIGjcJt0aWMidUpYAlRcyoEn6fY+X+ypXS0xWa6tJWp6FmJ5sE
EvL3PiXGjlEfpzQdnIimqc+jltTpor9AZvGgBKqmXmacCcpGjOY8X8kJuVH1ipk3C1wwY+lKDvRb8BvS+334V5J9mzYj5l/DS1JbS2me7eXrvCdt24Fx73b7c3Iqo0J93neYceq0PZxnYdZ89eROs1NTxNRekdgTokdEyQoTbkZ
NbTZt2oyEwZhsUwXvzQai2MwUqnoJ3gBYAwcod root@data-node2


# 登录到server1上分发整合后的文件到其它节点
scp ~/.ssh/authorized_keys 192.168.74.12:~/.ssh/
scp ~/.ssh/authorized_keys 192.168.74.13:~/.ssh/

# 第一次连接时会问你是否连接,点击yes就可以了。下次登录就不会再提示了。
# 在192.168.74.11上测试
ssh 192.168.74.12
ssh 192.168.74.13

SSH免密码登录(RSA)_第1张图片

# 在192.168.65.12上测试
ssh 192.168.74.11
ssh 192.168.74.13

SSH免密码登录(RSA)_第2张图片


# 在192.168.65.13上测试
ssh 192.168.74.11
ssh 192.168.74.12

SSH免密码登录(RSA)_第3张图片

# exit 退出登录

# 删除密钥
#  rm -fr /root/.ssh/

你可能感兴趣的:(linux,基础服务)