ssh远程免密登录

ssh远程连接分为五个阶段

  1. 版本号协商阶段
  2. 密钥和算法协商阶段
  3. 认证阶段
  4. 会话请求阶段
  5. 交互会话阶段

而上图的SessionKey即是在阶段2:密钥和算法协商阶段,服务器端和客户端利用DH交换(Diffie-Hellman Exchange)算法、主机密钥对等参数,生成的会话密钥

远程免密登录需要在客户端生成一对密钥,将公钥上传至服务器,即可完成免密登录

准备

客户端:192.168.220.220
服务器:192.168.220.131

方法一

#客户端
[root@localhost ~]# ssh-keygen -t rsa -b 2048 -f /root/.ssh/id_rsa -P ""
#-t 生成密钥算法 -b 密钥大小 -f 生成文件路径 -P 密码
Generating public/private rsa key pair.
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:ZMk7stzSEooEsLPQj1HDwludO02dccu0gHQ5liCIo4g root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|.. ooo.oo++=o    |
|.o=.+.ooooB= o   |
|B.o=   +=. .+    |
|E+.+  oo..       |
|. o . o.S        |
| . . o * .       |
|  . . = o        |
|       o         |
|                 |
+----[SHA256]-----+
[root@localhost ~]# ll /root/.ssh/
total 16
-rw-------  1 root root 1831 May  6 17:07 id_rsa
-rw-r--r--  1 root root  408 May  6 17:07 id_rsa.pub
-rw-------. 1 root root 1686 Apr 18 11:48 known_hosts
-rw-------. 1 root root  940 Apr 18 11:48 known_hosts.old
[root@localhost ~]# scp /root/.ssh/id_rsa.pub 192.168.220.131:/root/.ssh/
root@192.168.220.131's password: 
id_rsa.pub                                                                                                                                                                      100%  408   854.8KB/s   00:00    
[root@localhost ~]# ssh 192.168.220.131
#服务端
[root@rhce ~]# cd /root/.ssh/
[root@rhce .ssh]# cat id_rsa.pub >> authorized_keys
#验证
[root@localhost ~]# ssh 192.168.220.131
Activate the web console with: systemctl enable --now cockpit.socket

Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
Last login: Mon May  6 16:31:16 2024 from 192.168.220.131

方法二

#方法二全在客户端上操作
[root@localhost .ssh]# ssh-keygen -t rsa -b 2048 -f /root/.ssh/id_rsa -P "" -q
#-t 生成密钥算法 -b 密钥大小 -f 生成文件路径 -P 密码 -q 没有反馈
[root@localhost .ssh]# ll
total 16
-rw-------  1 root root 1831 May  6 17:28 id_rsa
-rw-r--r--  1 root root  408 May  6 17:28 id_rsa.pub
-rw-------. 1 root root 1686 Apr 18 11:48 known_hosts
-rw-------. 1 root root  940 Apr 18 11:48 known_hosts.old
[root@localhost .ssh]# ssh-copy-id root@192.168.220.131
/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@192.168.220.131's password: 

Number of key(s) added: 1

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

[root@localhost .ssh]# ssh 192.168.220.131
Activate the web console with: systemctl enable --now cockpit.socket

Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
Last login: Mon May  6 17:09:54 2024 from 192.168.220.220

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值