设置ssh免密登录——主从、从从

主从

点击查看代码
[root@master ~]# cd ~/.ssh
[root@master .ssh]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:0zOk0cgnk1h2aMzwYh2DvYDRMKYwpdLtHjiYG54Bo1M root@master
The key's randomart image is:
+---[RSA 2048]----+
|o.. ==.*=..      |
| = +..+B*B       |
|= E . +oX.+      |
|+= o . ..O       |
|*.o o   S +      |
|.+oo .   . o     |
|.o  .            |
|                 |
|                 |
+----[SHA256]-----+

[root@master .ssh]# cp id_rsa.pub authorized_keys
[root@master .ssh]# chmod 664 authorized_keys
[root@master .ssh]# service sshd restart
Redirecting to /bin/systemctl restart sshd.service

[root@master .ssh]# ssh-copy-id clone1     //进入clone1时免密

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"

/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@clone1's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'clone1'"
and check to make sure that only the key(s) you wanted were added.

[root@master .ssh]# ssh clone1
Last login: Sun Apr  7 18:57:25 2024 from 192.168.10.1
[root@clone1 ~]# exit

登出
Connection to clone1 closed.

从从
第一台从机生成公钥私钥,把公钥追加到authorized_keys,然后发送到下一台从机,再做一遍,最后一台发送回主机,主机发送给各从机,之后依次重启sshd服务

失败
[root@clone1 .ssh]# ls
authorized_keys  known_hosts
[root@clone1 .ssh]# ssh-keygen -t rsa -P ""
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:zNbdFwSMgLjv8J8XO1WdhXvlXuf0UmYYEv2/78eRu7U root@clone1
The key's randomart image is:
+---[RSA 2048]----+
|      . ... ++.o |
|     . .   ...= o|
|      .      . B=|
|     . o . . .oo&|
|      . S . . oOB|
|     . o   . ..+=|
|      +     +  o=|
|       o  .+   o=|
|        .o. .  E*|
+----[SHA256]-----+
[root@clone1 .ssh]# ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts
[root@clone1 .ssh]# cat id_rsa.pub  >> aut*

[root@clone1 .ssh]# scp aut*  root@clone2:~/.ssh

root@clone2's password: 
authorized_keys                                                 100%  786   169.2KB/s   00:00    
[root@clone1 .ssh]# ssh clone2
Last login: Tue Apr 30 20:37:20 2024 from master
[root@clone2 ~]# cd .ssh


[root@clone2 .ssh]# ls
authorized_keys

[root@clone2 .ssh]# ssh-keygen -t rsa -P ""
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
.our identification has been saved in 
.pub.public key has been saved in 
The key fingerprint is:
SHA256:NL4ieYNO0hKMyKj1hSu4uaGgq0T8ZHceXlp33NWBT9g root@clone2
The key's randomart image is:
+---[RSA 2048]----+
|              +. |
|             o Eo|
|        o     o o|
|=o   . o .  . ...|
|+++o...oSo . o . |
|oo++.=+ =.. .    |
|=.+.O ++.        |
|=+ * o o         |
|Xo  .            |
+----[SHA256]-----+

[root@clone2 .ssh]# ls
?  authorized_keys  known_hosts  ?.pub
[root@clone2 .ssh]# ssh-keygen -t rsa -P ""
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
 already exists.
Overwrite (y/n)? y
.our identification has been saved in 
.pub.public key has been saved in 
The key fingerprint is:
SHA256:+xFDFNEisgpXyupuHoIQSlzw9mjsSq4AXblEUiecMT4 root@clone2
The key's randomart image is:
+---[RSA 2048]----+
| .o+*o.   ++     |
|. o+o=o ... .    |
|.o +Eo o ...     |
|oo+o*o.  .       |
|+ .B.o  S o      |
|+ + .    . o     |
|+o..    . .      |
|+.+.     . .     |
|o*o       .      |
+----[SHA256]-----+
[root@clone2 .ssh]# cat  ?.pub  >> aut*
[root@clone2 .ssh]# ls
?  authorized_keys  known_hosts  ?.pub

[root@clone2 .ssh]# scp aut*  root@master:~/.ssh
root@master's password: 

authorized_keys                                                 100% 1179    28.9KB/s   00:00    

[root@clone2 .ssh]# ssh master
root@master's password: 
Last login: Tue Apr 30 20:34:47 2024 from clone2

[root@master .ssh]# ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts
[root@master .ssh]# vim aut*

[root@master .ssh]# systemctl restart sshd
[root@master .ssh]# ssh clone2
Last login: Tue Apr 30 20:41:48 2024 from clone1
[root@clone2 ~]# systemctl restart sshd
[root@clone2 ~]# ssh clone1
root@clone1's password: 
Last login: Tue Apr 30 20:38:28 2024 from clone2
[root@clone1 ~]# systemctl restart sshd
[root@clone1 ~]# ssh clone2
Last login: Tue Apr 30 20:45:06 2024 from master

从机分别生成公钥私钥,互相免密
成功
[root@clone2 .ssh]# mv ?.pub  id_rsa.pub
[root@clone2 .ssh]# mv ? id_rsa
[root@clone2 .ssh]# ls

authorized_keys  id_rsa  id_rsa.pub  known_hosts

[root@clone2 .ssh]# ssh-copy-id clone1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@clone1's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'clone1'"
and check to make sure that only the key(s) you wanted were added.

[root@clone2 .ssh]# ssh clone1
Last login: Tue Apr 30 21:12:22 2024 from clone2


#以下是Ai
根据提供的信息,看起来您正在使用`ssh-copy-id`命令将公钥添加到远程主机上,以便使用SSH密钥进行身份验证。以下是相关信息的解释:

- `/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"`:这是指明要安装的公钥的来源路径,`/root/.ssh/id_rsa.pub`是公钥文件的路径。

- `/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed`:这表示`ssh-copy-id`正在尝试使用新的公钥进行身份验证,以过滤掉已经安装的密钥。

- `root@clone1's password:`:这是要求输入远程主机`clone1`的密码。`ssh-copy-id`需要输入密码,以便将公钥添加到远程主机的授权密钥文件中。

- `Number of key(s) added: 1`:这表示成功添加了1个公钥。

- `Now try logging into the machine, with:   "ssh 'clone1'"`:这是建议尝试使用新添加的公钥进行SSH登录到远程主机`clone1`。



ps:

待修改

posted @   基础狗  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示