Linux ssh免密登录配置
程序员文章站
2022-04-30 12:08:33
...
1 系统环境
1.1 系统环境
hostname | IP | 主从角色 | MHA角色 |
mgr | 10.11.12.9 | N/A | MHA-manager |
node1 | 10.11.12.10 | master | MHA-node |
node2 | 10.11.12.11 | slave | MHA-node |
vip | 10.11.12.100 |
1.2 操作系统版本
[[email protected] .ssh]# more /etc/redhat-release
Red Hat Enterprise Linux Server release 7.6 (Maipo)
[[email protected] .ssh]# uname -a
Linux node2 3.10.0-957.el7.x86_64 #1 SMP Thu Oct 4 20:48:51 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
2 配置ssh免密
2.1 生成RSA秘钥文件(所有节点)
ssh-****** -t rsa
2.2 RSA秘钥文件列表
[[email protected] .ssh]# ls -rtlh
total 12K
-rw-r--r--. 1 root root 392 May 13 09:24 id_rsa.pub
-rw-------. 1 root root 1.7K May 13 09:24 id_rsa
2.3 配置authorized_keys
--- 登录mgr,cppy authorized_keys至node1
ssh-copy-id -i node1
--- 登录node1,复制id_rsa.pub内容至authorized_keys
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
--- cppy authorized_keys至node2
ssh-copy-id -i node2
--- 登录node2,复制id_rsa.pub内容至authorized_keys
cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
--- 此时authorized_keys文件中存在所有节点的登录秘钥信息
more /root/.ssh/authorized_keys
[[email protected] .ssh]# more /root/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCuN6VHDscOXF/REJLpQlis4tj16x4xxGvUpgQXd7i+MVvewB9nM3QkVTcVicKsQ70naAycOcBDazVsrezolFGCIbJk2xzLMrIWy5d1u1aBCRR/UuWxRns/sB6O5DroqRJazuZyN4y+NVt72kRGYhvVbWQJaXlUL4cJVTc5zTg3hPjToisvvh7RLyyx8l4WQicFxBVt
J2Yn7eUUsEs56ibHRz/I11IgY8hcJvc6CYaqMWjbzF2CavqOifYUh8pFDhLVxaD5JZyVwLphiK0YnEXjMUHrVpLqu1CYl7b1sOs9oIjrS0sV1mFmnXucrp//gNP2EmvfWvFK10AlIkpBskVz [email protected]
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCyi1RQ3QsJQdPiw+aqJGErm4io1I+2x392p3w5mE6Slt37cqpjDfxTM8GH9ZCRB383oY71FwSnfTfsjMOums5j1ijs4ewG/uaZCSNqMpVO9m+5LLjonUYlCYwf50iYZ1F+8QUidy9kqYK1bSa1QZNguXhtAcQ8ieqN8p9ykg1lqK5v6zNLK4mvZHQtvaT4hQlzb4/C
uPS12kX1ODWe0mwInug2jPayA2l10EFobbXNKqKOlzzu2GJ/FC7lfzD3Jx9AOzEJFs+T25HZKSqzzLoRkD1GxhuznHC7MFmrkgaUqLisdbgPMC+gMDiuwe/3p/1FMm/881qHEHpSSOCOK7Vd [email protected]
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDSy2id6xw2ve8q3F/wuqTaZqINMzQKjl9Iu3nO3vvaCF5p42OMGFbHSOIHf+K4x8+Wl8bF4Nea7qeTPB/H3PJYrhUlLcFbhhhC0huB8R7AHqNbWa8k8OQ0AuqQJih8R7Z9qKY3Y3BtwuzsxRMyWk2QIaJ1iCMaECyY0Ds8pXId/F6lA1ilcZbi6mtYoKuKL4qAS6Rz
7jx8BHXCkLfwFPgYeIRUB90UhgJGYtn7S6V3nXs961ft/vB8oBqjR2EqcQEqMFcPX1XPEEaiQQo4uGor5ZcR9dNQkMQfqQiePxB1cwslLq9g5SzOZy+n1+hr6rVj3xTF1+wUaJAn6Lte3+6T [email protected]
--- copy authorized_keys至所有节点
scp -r /root/.ssh/authorized_keys [email protected]:/root/.ssh
scp -r /root/.ssh/authorized_keys [email protected]:/root/.ssh
至此,免密码登录已经设定完成,注意第一次ssh登录时需要输入密码,再次访问时即可免密码登录。