您的位置:首页 > 其它

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

2014-05-14 11:11 253 查看
ssh访问可以通过两种形式进行登录:

安全密码认证:登录username@remotehost,输入对应username的密码,该密码会被加密发给remotehost,接下来,remotehost会从密码数据库中进行匹配,如果成功,则登录成功。
密钥访问: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

wenry@MyHome:~$ssh-keygen -t rsa

Generating public/private rsa key pair.

Enter file in which to save the key (/home/wenry/.ssh/id_rsa):

Created directory '/home/wenry/.ssh'.

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /home/wenry/.ssh/id_rsa.

Your public key has been saved in /home/wenry/.ssh/id_rsa.pub.

The key fingerprint is:

0f:c5:e1:e9:b1:a4:f2:e0:ee:9f:0a:bc:c7:c8:c8:b9 wenry@MyHome

The key's randomart image is:

+--[ RSA 2048]----+

| . |

| o o |

| B |

| = o |

| o S o |

| . . + o |

| . +oo. . . |

| + o+o . |

| E..o+oo |

+-----------------+

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

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

这样配置之后,就可以在localhost上ssh username@hostname,无需密码直接访问了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: