SSH免密登录

打印 上一主题 下一主题

主题 936|帖子 936|积分 2808

基于公私钥的认证(免密码登录)

基于密钥对认证,也就是所谓的免密码登录,理解免密登录原理:
  1. 1.机器A 想免密码登录 机器B
  2. 2.机器A得发送自己的公钥给机器B
复制代码
  1. 1.master-61机器生成一对公私钥
  2. 2.master-61机器发送自己的公钥,ssh-copy-id命令发给 web-7,此时需要输入web-7的账号密码,输入正确密码后。
  3. 3.web-7机器将master-61的公钥写入本地的~/.ssh/authorized_keys 已信任的公钥文件中
  4. 4.下一次master-61再次ssh登录web-7,web-7去本地的~/.ssh/authorized_keys文件里搜索master-61的公钥,如果找到了,生成随机字符串
  5. 5.web-7将生成的随机字符串结合master-61的公钥加密处理,返回给master-61
  6. 6.master-61拿到该加密后的随机字符串,使用自己的私钥解密,解密成功后将原始随机字符串发给web-7
  7. 7.web-7比对该随机字符串,确认正确,允许登录。
复制代码
基于公私钥认证实践(重要)

原理很复杂、但是操纵很简单,其实就几条下令,天生了几个配置文件;
但是于超老师给你讲清晰原理,了解其背后的通信过程,无论是排错,还是ssh出现安全问题,回头思考这个流程,就能摸索出解决方案。

免密登录步调
  1. 1.创建秘钥对,全部回车,默认即可
  2. [root@master-61 ~]#ssh-keygen
  3. Generating public/private rsa key pair.
  4. Enter file in which to save the key (/root/.ssh/id_rsa):
  5. Created directory '/root/.ssh'.
  6. Enter passphrase (empty for no passphrase):
  7. Enter same passphrase again:
  8. Your identification has been saved in /root/.ssh/id_rsa.
  9. Your public key has been saved in /root/.ssh/id_rsa.pub.
  10. The key fingerprint is:
  11. SHA256:ENZzEVp+qIjG+Cb/MBko8anhY8JGrbqLhR8+6ZI9B2o root@master-61
  12. The key's randomart image is:
  13. +---[RSA 2048]----+
  14. |      o.  +o     |
  15. |     . .o+..     |
  16. |.     . .oo .    |
  17. | o.= . o . .     |
  18. |o.=.= . S        |
  19. |+=oo o           |
  20. |+@+o*            |
  21. |XE*=.o           |
  22. |*=++...          |
  23. +----[SHA256]-----+
  24. 2.查看生成的公私钥
  25. [root@master-61 ~]#ls -l ~/.ssh/
  26. total 8
  27. -rw------- 1 root root 1679 Apr 22 19:43 id_rsa
  28. -rw-r--r-- 1 root root  396 Apr 22 19:43 id_rsa.pub
  29. 3.发送公钥给目标机器
  30. [root@master-61 ~]#ssh-copy-id web-7
  31. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
  32. The authenticity of host 'web-7 (10.0.0.7)' can't be established.
  33. ECDSA key fingerprint is SHA256:Csqwr63+SZRFFOug/IGoFTgRe8hDSI/QalSMBcC6IaU.
  34. ECDSA key fingerprint is MD5:4c:9a:37:e2:5b:b5:de:a8:bf:90:b5:28:d8:5b:ac:60.
  35. Are you sure you want to continue connecting (yes/no)? yes
  36. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  37. /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
  38. root@web-7's password:
  39. Number of key(s) added: 1
  40. Now try logging into the machine, with:   "ssh 'web-7'"
  41. and check to make sure that only the key(s) you wanted were added.
  42. 4.测试是否可以免密登录
  43. [root@master-61 ~]#ssh root@web-7
  44. Last login: Fri Apr 22 17:50:42 2022 from 10.0.0.1
  45. [root@web-7 ~]#
复制代码
查抄web-7上的authorized_keys
  1. [root@web-7 ~]#cat ~/.ssh/authorized_keys
  2. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRsvpXAYBkQ/q3X9Rs7s+W5ppBaHj4zqtLk6Dvk0yvpFYIJvgvK27Q0hZWE5lXgiSpeYY3wXsg0SLI0/DAEU+mi2mrSUaCMDyia9A0vtpKsu574QDl2eOgU46sBrKfUw1vxC5Ow5awCzHu6RCdvo6mqVLDfqBG4e+pUEvYP4XVL4LMPqK0Wp5OZNprtIXzu57xE+wNUcbwC+hWc/2VSyBAtu9VXtVebrUk9t8hVAhKc2e7m8feexd+/WK5a4/FTj7oQb6P7GK+7gVXY6Thgwv54uIR9gSDU1U5aqEI9ng0xPUyI5KDMWjn2O2mfPY2tMF9ZsAgXJ/S7daMefRzdFvp root@master-61
复制代码
查验master-61的公私钥文件
  1. 公钥
  2. [root@master-61 ~]#cat ~/.ssh/id_rsa.pub
  3. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRsvpXAYBkQ/q3X9Rs7s+W5ppBaHj4zqtLk6Dvk0yvpFYIJvgvK27Q0hZWE5lXgiSpeYY3wXsg0SLI0/DAEU+mi2mrSUaCMDyia9A0vtpKsu574QDl2eOgU46sBrKfUw1vxC5Ow5awCzHu6RCdvo6mqVLDfqBG4e+pUEvYP4XVL4LMPqK0Wp5OZNprtIXzu57xE+wNUcbwC+hWc/2VSyBAtu9VXtVebrUk9t8hVAhKc2e7m8feexd+/WK5a4/FTj7oQb6P7GK+7gVXY6Thgwv54uIR9gSDU1U5aqEI9ng0xPUyI5KDMWjn2O2mfPY2tMF9ZsAgXJ/S7daMefRzdFvp root@master-61
  4. 私钥文件
  5. [root@master-61 ~]#ls -l  ~/.ssh/id_rsa
  6. -rw------- 1 root root 1679 Apr 22 19:43 /root/.ssh/id_rsa
  7. 已连接过的主机指纹
  8. [root@master-61 ~]#cat   ~/.ssh/known_hosts
  9. web-7,10.0.0.7 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBL/Sx3bAaNcKqo7pC4FTYk3gyZ6hd1D/DKUWVfOd4gZb/8XwlAxWauceHe/BAsW5Z8pEmG6AjSyHM8ckOs94c7Y=
复制代码
配置文件总结

在整个免密登录过程中,涉及的配置文件
  1. 客户端,需要生成公私钥,检查如下目录
  2. [root@master-61 ~]#ls ~/.ssh/
  3. id_rsa  id_rsa.pub  known_hosts
  4. 服务端,记录客户端的公钥
  5. [root@web-7 ~]#ls ~/.ssh/
  6. authorized_keys  id_rsa  id_rsa.pub  known_hosts
  7. 其实整个过程就
  8. 1个目录 ~/.ssh/
  9. 四个配置文件
  10. authorized_keys  id_rsa  id_rsa.pub  known_hosts
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

写过一篇

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表