快速配置ssh无密码登录

325 阅读2分钟

本文已参与[新人创作礼]活动,一起开启掘金创作之路。

本文介绍快速配置ssh无密码登录,看到很多文章介绍的配置无密码登录需要很多步骤,虽然不算麻烦,但是本身只需要一个命令就能够配置的东西却搞半天,实在是很烦,所以我就在这里整理记录了两种快速配置ssh无密码登录的方法。

前提:现在要配置A服务器免密码登录B服务器。

第一种方法: 在A服务器上执行ssh-keygen,一路按确定即可,如果已经生成过密码,甚至这一步都可以省略。 接着执行ssh-copy-id root@B服务器ip,输入B服务器的密码就配置完成了。下面是操作步骤:

[root@localhost ~]# ssh-keygen

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:+FEfmqvoLiTOFp7hL+Ov8JiHHOvcYX0vYVWe5Fwpt+U
root@localhost.localdomain

The key's randomart image is:

+---[RSA 2048]----+
|              .  |
|           + + . |
|          B * +  |
|       . o O o E |
|      . S o .    |
| .+ o  + . .     |
|o*oO ...o .      |
|oB& o .o..       |
|+*+Bo++ o.       |
+----[SHA256]-----+
[root@localhost ~]# ssh-copy-id root@192.168.11.200
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.11.200 (192.168.11.200)' can't be established.
ECDSA key fingerprint is SHA256:fmsVpyf+39IPGV5YNO+MlDvAOViAKcYPrrla4ihULxo.
ECDSA key fingerprint is MD5:77:c0:4a:1e:0b:b3:47:1c:a0:7b:97:8b:91:df:b2:1d.
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@192.168.11.200's password:

第二种方法: 同上一步执行ssh-keygen,一路确定生成密钥,将/root/.ssh/id_rsa.pub的内容复制出来。 在B服务器用户家目录(如果用的root用户登录就是/root目录),创建.ssh目录。将刚才A服务器的id_rsa.pub的内容复制到authorized_keys文件,如果没有该文件的话创建一个就可以。