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,无需密码直接访问了。

    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值