centos配置ssh免密登录

<code>本文由“开发者手册”原创,如需转载,拿走不谢!/<code>

前提概要

<code>ssh是我们常用的登录服务器命令,常规格式如下:ssh name@ip,然后验证密码。
对于服务器数量少的话这样操作其实也还好,毕竟可以练练手速。但是如果服务器多的话就比较麻烦,且容易出错。
今天给大家带来的就是一个ssh免密登录的设置方法,供大家参考使用。
好了,废话说的够多了,下面进入正题。/<code>

系统环境介绍

<code>centos7.5参数如下:

命令:uname -a
输出:Linux server160131 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

命令:rpm -q centos-release
输出:centos-release-7-5.1804.el7.centos.x86_64

需要centos7.5精简版安装包的同学可以评论或者私信我,私信关键字”centos“会自动回复您下载地址。/<code>

演示服务器介绍

<code>1: server160131(192.168.160.131)
2: server160132(192.168.160.132)
3: server160132(192.168.160.133)/<code>

搭建步骤

1、场景描述:

<code>我这边以server160131这台作为跳板。
需要实现从server160131这台免密ssh到server160132和server160133。/<code>

2、生成主跳板节点的秘钥对(在server160131上操作)

<code>注意:当前用户目录下有.ssh文件夹且存在id_rsa和id_rsa.pub这2个文件就可以忽略这一步操作。

我这边的主跳板节点是:server160131

命令: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:wbGgO1iQ5Wn/6CofgSR+KDSYReKTN7s6WzCmb+gVvvg root@server160131
The key's randomart image is:
+---[RSA 2048]----+
|..+o. . . |
|o+oo o o o |
|+*.o* + |
|oo==oo . |
|.*o++ . S |
|+ = oo o |
|.. =. . . |
|.oB .o |
|.*=Eo.. |
+----[SHA256]-----+
结果:
此时会在当前用户目录下生成.ssh文件夹,我这边是/root/.ssh。里面会有2个文件:
id_rsa和id_rsa.pub。/<code>

3、分发主跳板上的公钥文件到其他节点(在server160131上操作)

<code>分发到server160132节点

命令:ssh-copy-id [email protected]

输出如下:
/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
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added./<code>
<code>分发到server160133节点

命令:ssh-copy-id [email protected]

输出如下:
/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
[email protected]'s password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added./<code>

4、验证是否成功

<code>命令:ssh [email protected]

输出:
Last login: Mon Mar 23 12:01:40 2020 from server160131
[root@server160132 ~]/<code>

结束语

希望我的文章能给您带来帮助,如果您有什么疑问,可以给我留言,我会在第一时间给你提供解答。如果您有好的建议或想法,也欢迎给我留言。

centos配置ssh免密登录


分享到:


相關文章: