CentOS 7 SSH 免密登录的方法

先决条件

3 台 CentOS 7

HOSTNAME IP ROLE
server1 10.8.26.197 Master
server2 10.8.26.196 Slave1
server3 10.8.26.195 Slave2

1. 用 root 用户登录。每台服务器都生成公钥,再合并到 authorized_keys。

2. CentOS 默认没有启动 ssh 无密登录,去掉<font style="color:rgb(192, 52, 29);background-color:rgb(251, 229, 225);"> /etc/ssh/sshd_config </font>其中 2 行的注释,每台服务器都要设置。

1
2
<font style="color:rgb(0, 0, 0) !important;background-color:rgb(255, 255, 255) !important;">RSAAuthentication </font>``<font style="color:rgb(255, 20, 147) !important;background-color:rgb(255, 255, 255) !important;">yes</font>
<font style="color:rgb(0, 0, 0) !important;background-color:rgb(255, 255, 255) !important;">PubkeyAuthentication </font>``<font style="color:rgb(255, 20, 147) !important;background-color:rgb(255, 255, 255) !important;">yes</font>

3. 每台服务器下都输入命令<font style="color:rgb(192, 52, 29);background-color:rgb(251, 229, 225);"> ssh-keygen -t rsa</font>,生成 key,一律不输入密码,直接回车,<font style="color:rgb(192, 52, 29);background-color:rgb(251, 229, 225);">/root</font> 就会生成<font style="color:rgb(192, 52, 29);background-color:rgb(251, 229, 225);"> .ssh </font>文件夹。

4. 在 Master 服务器下,合并公钥到<font style="color:rgb(192, 52, 29);background-color:rgb(251, 229, 225);"> authorized_keys </font>文件,进入 <font style="color:rgb(192, 52, 29);background-color:rgb(251, 229, 225);">/root/.ssh </font>目录,通过 SSH 命令合并.

1
2
3
<font style="color:rgb(0, 130, 0) !important;background-color:rgb(255, 255, 255) !important;"># cat id_rsa.pub>> authorized_keys</font>
<font style="color:rgb(0, 130, 0) !important;background-color:rgb(255, 255, 255) !important;"># ssh root@10.8.26.196 cat ~/.ssh/id_rsa.pub>> authorized_keys</font>
<font style="color:rgb(0, 130, 0) !important;background-color:rgb(255, 255, 255) !important;"># ssh root@10.8.26.195 cat ~/.ssh/id_rsa.pub>> authorized_keys</font>

5. 把 Master 服务器的<font style="color:rgb(192, 52, 29);background-color:rgb(251, 229, 225);"> authorized_keys</font><font style="color:rgb(192, 52, 29);background-color:rgb(251, 229, 225);">known_hosts </font>复制到 Slave 服务器的 <font style="color:rgb(192, 52, 29);background-color:rgb(251, 229, 225);">/root/.ssh `目录

1
2
3
4
<font style="color:rgb(0, 130, 0) !important;background-color:rgb(255, 255, 255) !important;"># scp authorized_keys root@server2:/root/.ssh/</font>
<font style="color:rgb(0, 130, 0) !important;background-color:rgb(255, 255, 255) !important;"># scp authorized_keys root@server3:/root/.ssh/</font>
<font style="color:rgb(0, 130, 0) !important;background-color:rgb(255, 255, 255) !important;"># scp known_hosts root@server2:/root/.ssh/</font>
<font style="color:rgb(0, 130, 0) !important;background-color:rgb(255, 255, 255) !important;"># scp known_hosts root@server3:/root/.ssh/</font>

6. 完成,<font style="color:rgb(192, 52, 29);background-color:rgb(251, 229, 225);">ssh root@10.8.26.196</font><font style="color:rgb(192, 52, 29);background-color:rgb(251, 229, 225);">ssh root@10.8.26.195 </font>就不需要输入密码了。

另一种方式:

1
2
3
4
<font style="color:rgb(0, 0, 0) !important;background-color:rgb(255, 255, 255) !important;">1、cd .ssh/</font>
<font style="color:rgb(0, 0, 0) !important;background-color:rgb(255, 255, 255) !important;">2、ssh-keygen -t rsa</font>
<font style="color:rgb(0, 0, 0) !important;background-color:rgb(255, 255, 255) !important;">3、ssh-copy-id root@ip </font>
<font style="color:rgb(0, 0, 0) !important;background-color:rgb(255, 255, 255) !important;">4、ssh ip #测试成功</font>

示例:在166上执行ssh-keygen -t rsa,然后ssh-copy-id root@10.20.31.169

[root@master1 ~]# chmod 0750 /root -R
[root@master1 ~]# chmod 600 ~/.ssh/id_rsa

非root用户传递文件的话别忘了修改文件属主

ssh-copy-id root@10.20.31.170 ssh-copy-id root@10.20.31.171 ssh-copy-id root@10.20.31.172 ssh-copy-id root@10.20.31.175
posted @ 2025-06-24 16:53  youemby  阅读(109)  评论(0)    收藏  举报