Linux SSH免密码登陆

本文详细介绍了如何在 CentOS 7 上配置 SSH 免密码登录,包括生成密钥、导入密钥、添加主机名和 IP 到 hosts 文件、分发密钥到其他主机等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

centos7配置ssh免密码登陆

1.生成密钥,执行命令sh-keygen -t rsa,默认回车

[root@master ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
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:mD7BQPCmXfPwkK1OnT9HVFHFYHFNEwia1nSb6UFR1RE root@master
The key's randomart image is:
+---[RSA 2048]----+
|  ...      o.+XE#|
|   o   o  = o++.=|
|    + * .+ ..=   |
|   + + @.. .. .  |
|  . . B S   ..   |
|     + . . .     |
|      +   o .    |
|       .   o     |
|                 |
+----[SHA256]-----+
[root@master ~]#

2.将密钥导入到认证文件中

[root@master ~]# cd .ssh/
[root@master .ssh]# cat id_rsa.pub>>authorized_keys
[root@master .ssh]# ll
总用量 12
-rw-r--r--. 1 root root  393 715 15:11 authorized_keys
-rw-------. 1 root root 1675 715 15:08 id_rsa
-rw-r--r--. 1 root root  393 715 15:08 id_rsa.pub
[root@master .ssh]#

3.将集群中所有主机ip地址和对应的主机名添加到hosts文件中

[root@master .ssh]# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.101 master
192.168.1.102 slave1
192.168.1.103 slave2
192.168.1.104 slave3 

4.然后将master的authorized_keys传到slave1上的.ssh目录下

[root@master .ssh]# scp -r authorized_keys root@slave1:~/.ssh
The authenticity of host 'slave1 (192.168.1.102)' can't be established.
ECDSA key fingerprint is SHA256:NmkN2Uza23ugzRxBDc/xnUDJ/YRUMNBE3DleGPhyFUo.
ECDSA key fingerprint is MD5:65:22:2c:ed:83:9c:9c:18:f9:9f:d5:d6:ab:4a:88:d7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'slave1,192.168.1.102' (ECDSA) to the list of known hosts.
root@slave1's password:
authorized_keys                               100%  393   161.7KB/s   00:00    
[root@master .ssh]#

5.将slave1的密钥添加到认证文件中,重复以上步骤,直至所有的密钥都添加到认证文件中,最后将包含所有主机密钥的认证文件分发到每一台主机上覆盖原文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值