CentOS 7 配置ssh免密码登录

本文介绍如何在CentOS7中配置SSH免密码登录。通过生成密钥对,并将公钥复制到目标主机上,实现从一台服务器无密码登录到另一台服务器的功能。文章详细记录了配置步骤和验证过程。

CentOS 7 配置ssh免密码登录

这里是使用root用户登录和执行相关操作

我这里是配置了hosts,下面操作的直接域名,可以把域名换成IP。

192.168.247.129 master
192.168.247.131 slave1
192.168.247.132 slave2

生成密钥

切换到用户主目录下的.ssh,如root用户 /root/.ssh/ ,执行ssh-keygen -t rsa生成公钥和密码

cd /root/.ssh/
ssh-keygen -t rsa

执行结果(默认回车3次车),生成2个文件id_rsa id、rsa.pub

[root@slave2 .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:VeTLrMmHpweWBlBCYmhlxf96uD/IV7H7tzgWDUC9XHI root@slave2
The key's randomart image is:
+---[RSA 2048]----+
|   o=++.. .o+    |
|  oo .o.   + o E |
| .     o  . + =  |
|        o. o.=   |
|        So .+oo  |
|         .*+o. . |
|       . *=oo..  |
|        = ++oo. .|
|        .=oo.ooo.|
+----[SHA256]-----+
[root@slave2 .ssh]# ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts

know_hosts : 已知的主机公钥清单
id_rsa : 生成的私钥文件
id_rsa.pub : 生成的公钥文件

复制公钥到其它主机

这里使用ssh-copy-id 命令 ssh-copy-id user@host,不写用户默认是root

ssh-copy-id master
ssh-copy-id slave1
ssh-copy-id slave2
  • 验证
    使用ssh指令 ssh user@ip,不指定user默认root
ssh master
ssh slave1
ssh slave2

如果希望ssh公钥生效需满足至少下面两个条件:1、ssh目录的权限必须是700 2、 .ssh/authorized_keys文件权限必须是600

  • 执行过程
[root@slave2 .ssh]# ssh-copy-id master
/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@master's password: 

Number of key(s) added: 1

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

[root@slave2 .ssh]# ssh master
Last login: Wed Apr 11 22:09:27 2018 from 192.168.247.132
[root@master ~]# exit
logout
Connection to master closed.
[root@slave2 .ssh]# ssh-copy-id slave1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'slave1 (192.168.247.131)' can't be established.
ECDSA key fingerprint is SHA256:4L+UTEoSu7D5Z0KwVZ8hiLXORJsLdhj5v+9NUrdJyA8.
ECDSA key fingerprint is MD5:a6:13:a5:aa:4b:f4:57:b4:7e:93:06:19:a9:8c:a1:56.
Are you sure you want to continue connecting (yes/no)? yes
/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@slave1's password: 

Number of key(s) added: 1

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

[root@slave2 .ssh]# ssh slave1
Last login: Wed Apr 11 21:37:40 2018 from 192.168.247.129
[root@slave1 ~]# exit
logout
Connection to slave1 closed.
[root@slave2 .ssh]# 

可以看出首远程登录其它服务器需要输入密码,退出再重新连接则不需要再输入密码就可以登录了。
这个就是免密码登录配置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值