Linux 下实现实例之间免密登录

文章
林里克斯

Linux 下实现实例之间免密登录


实验平台:CentOS Linux release 7.9.2009


一、生成密钥


1.使用 ssh-keygen 命令生成密钥文件

$ ssh-keygen 
Generating public/private rsa key pair. 
Enter file in which to save the key (/root/.ssh/id_rsa):    #密钥文件存放路径,默认是执行命令的用户的家目录
Created directory '/root/.ssh'.                           #在用户的根目录生成一个 .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:BkB4+5l17YJpYw22sJjpGg0GJxi1s8DCt0WNSsLpBM4 root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|=.ooo.o          |
|**.ooo .         |
|OE*o.o.    .     |
|.*.++ ..+ . .    |
|  +. = BSB .     |
| . o+ =.B o .    |
|  ...  o . .     |
|   ..            |
|  ..             |
+----[SHA256]-----+

2.查看 .ssh 目录

$ ls -la
total 16
-rw-rw-r--. 1 root root  395 Nov 30 23:04 authorized_keys
-rw-------. 1 root root 1675 Nov 30 23:04 id_rsa
-rw-r--r--. 1 root root  395 Nov 30 23:04 id_rsa.pub
-rw-r--r--. 1 root root  516 Nov 30 23:33 known_hosts
# authorized_keys 存放其他实例远程免密登录的公钥,默认没有。主要是存放多台实例的公钥
# id_rsa 生成的私钥文件
# id_rsa.pub 生成的公钥文件
# known_hosts 已知的实例,(连接实例的时候需要你选择 yes/no 的时候,选 yes 就会存在这个文件里)

二、配置免密登录


1.需要免密登录那台服务器就将另外台生成的 公钥 文件拷贝至 .ssh 目录下即可

实现步骤:

Server A 本地远程 SSH 实例

Server B 被远程 SSH 实例

1.将 Server A· 的公钥拷贝到需要密钥登录的Server B,重命名为authorized_keys`;

2.Server AServer B 发送一个 SSH 连接请求;

3.Server B 得到 Server A 的信息后,会在 authorized_key 中查找,如果有相应的 用户名IP,则随机生成一个字符串,并用 Server A 的公钥加密,发送给 Server A

4.Server A 得到 Server B 回发来的消息后,使用私钥进行解密,然后将解密后的字符串发送给 Server BServer B 进行和生成的对比,如果一致,则允许免登录。

总之:Server A 想要要免密码登录到Server B,Server B 首先要拥有 Server A 的公钥,然后 Server B 要做一次加密验证。对于非对称加密,公钥加密的密文不能公钥解开,只能私钥解开。

2.copy 公钥

  • 通过 ssh-copy-id
$ ssh-copy-id -i ~/.ssh/id_rsa.pub 10.10.12.7
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/jarbo/.ssh/id_rsa.pub"
The authenticity of host '10.10.12.7 (10.10.12.7)' can't be established.
ECDSA key fingerprint is SHA256:fx6FzTUrpVop0TbFhIAj2tvsLMaQd/4ykQ5nHFFxtU4.
ECDSA key fingerprint is MD5:84:5e:2f:b8:01:09:7e:df:76:cc:a8:3c:5c:c3:c0:e0.
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
jarbo@10.10.12.7's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '10.10.12.7'"
and check to make sure that only the key(s) you wanted were added.
# ssh-copy-id 命令会把本地当前用户的公钥复制发送到远端实例上,并自动创建 `authorized_keys` 文件和 `.ssh` 目录,并设置合适的权限。`authorized_keys` 文件需要 `600` 权限,`.ssh` 目录需要 `700` 权限。
  • 手动复制

1.在远程实例上的家目录里创建 .ssh 目录,默认没有

$ mkdir ~/.ssh/
#远程实例

2.本地实例 scp 或其他方式 copy 公钥至远程实例

$ cd ~/.ssh/
$ cat id_rsa.pub >> authorized_keys
$ scp ~/.ssh/authorized_keys user@romte ip
#本地实例,不加 user 默认使用本地执行 scp 命令的用户

3.授权否则会报错

$ chmod 700 ~/.ssh/
$ chmod 600 ~/.ssh/authorized_keys
# .ssh 目录的权限必须是 700
# authorized_keys 文件权限必须是 600

Over~

版权协议须知!

本篇文章来源于 Uambiguous ,如本文章侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意

869 0 2020-12-02


分享:
icon_mrgreen.gificon_neutral.gificon_twisted.gificon_arrow.gificon_eek.gificon_smile.gificon_confused.gificon_cool.gificon_evil.gificon_biggrin.gificon_idea.gificon_redface.gificon_razz.gificon_rolleyes.gificon_wink.gificon_cry.gificon_surprised.gificon_lol.gificon_mad.gificon_sad.gificon_exclaim.gificon_question.gif
博主卡片
林里克斯 博主大人
一个致力于Linux的运维平台
运维时间
搭建这个平台,只为分享及记载自己所遇之事和难题。

现在时间 2024-04-29

今日天气
站点统计
  • 文章总数:240篇
  • 分类总数:29个
  • 评论总数:10条
  • 本站总访问量 216830 次

@奥奥

@Wong arrhenius 牛比

@MakerFace 厉害了!