从主机A得到id_rsa.pub文件,在主机B创建用户danny加入该文件,实现主机A免密登录主机B

Posted dannylinux

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从主机A得到id_rsa.pub文件,在主机B创建用户danny加入该文件,实现主机A免密登录主机B相关的知识,希望对你有一定的参考价值。

操作步骤:

1.主机A

生成公钥id_rsa.pub文件并导出该文件

[email protected]:~# ssh-keygen 
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:
7e:0f:dc:05:2e:d3:e0:24:c0:5f:fe:cf:e9:7a:f7:49 [email protected]
The keys randomart image is:
+--[ RSA 2048]----+
|     ..          |
|      ..  .      |
|       ..oo .    |
|        .+.+ .   |
|        S +.o .  |
|       . . +..   |
|        . + .o E |
|         . o  * o|
|            o+.oo|
+-----------------+
[email protected]:~# sz /root/.ssh/id_rsa.pub 

这步之后也可以通过以下步骤直接实现公钥认证(但是需要知道主机B用户danny的密码)

# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]12.1.1.2
[email protected]12.1.1.2s password:      //输入主机B用户danny的登录密码

 

 

2.主机B

得到id_rsa.pub文件,创建用户danny,并在该用户家目录创建.ssh文件夹和其文件下的远程认证文件authorized_keys

[[email protected] danny]# mkdir .ssh
[[email protected] danny]# cd .ssh/
[[email protected] .ssh]# touch authorized_keys
[[email protected] .ssh]# ls
authorized_keys
[[email protected] .ssh]# cat /home/danny/id_rsa.pub >authorized_keys

 

主机A登录主机B

 

以上是关于从主机A得到id_rsa.pub文件,在主机B创建用户danny加入该文件,实现主机A免密登录主机B的主要内容,如果未能解决你的问题,请参考以下文章

SSH免密登录(并且免yes交互)

docker-machine 快速搭建docker环境

使用密钥解决scp命令需要输入密码问题

ssh设置无密码登录

多台linux主机之间建立免密通信

阿里云主机ssh 免密码登录