SSH的密匙管理, 免密码登录

ssh访问可以通过两种形式进行登录:

  1. 安全密码认证:登录username@remotehost,输入对应username的密码,该密码会被加密发给remotehost,接下来,remotehost会从密码数据库中进行匹配,如果成功,则登录成功。
  2. 密钥访问:localhost通过ssh-keygen来生成公钥密钥对,如果他想访问一个remotehost,则只需要将公钥添加到remotehost的~/.ssh/authorized_keys中,接下来,当localhost通过ssh登录username@remotehost时,remotehost会生成一个随机数,通过autrorized_keys中的公钥们生成一系列数值发给localhost,localhost会通过自己的私有密钥解密发过来的一系列数值(当然,只有用对应的公钥生成的数值才会被正常解密),随后,localhost将解密后的数值发回去,remotehost若发现发回来的数值是原先产生的随机数时,便会允许该localhost访问。当然,如果localhost生成的rsa密钥是需要密码的话,接下来还要输入该密码。

接下来,我就来举例如何通过ssh密钥实现自动访问。

  • 配置localhost:如果还没有密钥公钥对生成,则先用ssh-keygen指令生成一个密钥对。

[xhtml] view plaincopy
  1. wenry@MyHome:~$ssh-keygen -t rsa  
  2. Generating public/private rsa key pair.  
  3. Enter file in which to save the key (/home/wenry/.ssh/id_rsa):   
  4. Created directory '/home/wenry/.ssh'.  
  5. Enter passphrase (empty for no passphrase):   
  6. Enter same passphrase again:   
  7. Your identification has been saved in /home/wenry/.ssh/id_rsa.  
  8. Your public key has been saved in /home/wenry/.ssh/id_rsa.pub.  
  9. The key fingerprint is:  
  10. 0f:c5:e1:e9:b1:a4:f2:e0:ee:9f:0a:bc:c7:c8:c8:b9 wenry@MyHome  
  11. The key's randomart image is:  
  12. +--[ RSA 2048]----+  
  13. |          .      |  
  14. |         o o     |  
  15. |          B      |  
  16. |         = o     |  
  17. |      o S o      |  
  18. |   . . + o       |  
  19. | . +oo. . .      |  
  20. |  + o+o  .       |  
  21. |  E..o+oo        |  
  22. +-----------------+  

在输入ssh-keygen指令后,只需要连续输入三个回车键,就可以生成不需要密码的密钥公钥对了,从而实现自动访问。

  • 配置remotehost:将localhost的id_rsa.pub内容附加到remotehost的/home/username/.ssh/authorized_keys中。

这样配置之后,就可以在localhost上ssh username@hostname,无需密码直接访问了。

发布了71 篇原创文章 · 获赞 30 · 访问量 60万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章