ssh密钥认证

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssh密钥认证相关的知识,希望对你有一定的参考价值。

?br/>?基于密钥的认证
实现基于密钥的认证,在登录过程中不需要输入密码,客户端和ssh服务端以密钥对进行认证,不在网络中传输用户名和密码信息,其公钥存储于ssh服务端,私钥当然是存储在客户端本地的。认证通过后才能登录成功。基于密钥的认证在我们常用的终端软件上设置好以后,我们在连接Linux主机时可自动登录,而且比常规ssh口令登录更加安全,而且服务器之间ssh登录也可以启用这种方式省去了输入口令的过程,而且在修改账号密码后也能继续使用原来的密钥进行认证。
?
?实现基于密钥的认证
?
?服务器之间实现密钥认证
?
?一般来说,要实现将主机A作为客户端,登录作为ssh服务端的主机B就需要有如下操作:
?
?以账户a身份登录主机A
?
?在主机A上生成a账户的密钥对
?
?将a账户的公钥注册到主机B目标账户b的~/.ssh/authorized_keys文件中
?
?将主机B的sshd启用公钥认证
?
?验证主机A是否可以通过密钥认证的方式登录主机B
这样实现的效果是:先以a的身份登录主机A,然后在a上可以以b的身份通过密钥认证的方式登录到主机B。
?
?以下以两台服务器localhost和xad为例,实现它们之间以root这个账号可以密钥登录。
?
?在localhost和xad上分别生成它们各自的密钥对
?
?localhost:?
?![](http://i2.51cto.com/images/blog/201801/08/d20e0aa18020dc52480f931ef7762178.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
?[[email protected]?~]$?ssh-keygen?-t?dsa???#使用ssh-keygen工具生成localhost上root账号的密钥对,-t指定加密算法为dsa?
?Generating?public/private?dsa?key?pair.?
br/>?Enter?file?in?which?to?save?the?key?(/home/root/.ssh/id_dsa):???#指定密钥对的保存位置,默认为当前用户的家目录下的./ssh目录?
?Enter?passphrase?(empty?for?no?passphrase):?#设置密钥的密码,设置以后每次使用到这个密钥都会需求输入密码,不设置就留空?
?Enter?same?passphrase?again:??
?Your?identification?has?been?saved?in?/home/root/.ssh/id_dsa.?
?Your?public?key?has?been?saved?in?/home/root/.ssh/id_dsa.pub.?
?The?key?fingerprint?is:?
?51:b1:7c:b4:78:7e:f4:fa:dd:24:04:82:bc:81:47:[email protected]??#这一指纹信息的最后部分表明了密钥的身份必须是:localhost主机上的root账户。?
??
br/>?xad:?
?![](http://i2.51cto.com/images/blog/201801/08/2647f802ca299c9e8cee157a53f1de91.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
?[[email protected]?~]$?ssh-keygen?-t?dsa?
?Generating?public/private?dsa?key?pair.?
br/>?Enter?file?in?which?to?save?the?key?(/home/root/.ssh/id_dsa):??
?Enter?passphrase?(empty?for?no?passphrase):??
?Enter?same?passphrase?again:??
?Your?identification?has?been?saved?in?/home/root/.ssh/id_dsa.?
?Your?public?key?has?been?saved?in?/home/root/.ssh/id_dsa.pub.?
?The?key?fingerprint?is:?
?c3:63:24:29:dc:7b:12:59:d5:a2:c2:e1:72:e0:ea:[email protected]??#此密钥对所有者为xad中的root账户。?
?
br/>?查看localhost和xad各自的公钥
?localhost:
?[[email protected]?.ssh]$?ll?~/.ssh
?total?16
br/>?-rw-------?1?root?root?1116?Jan??4?21:05?authorized_keys
?-rw-------?1?root?root??668?Jan??5?11:38?id_dsa??#root的私钥
?-rw-r--r--?1?root?root??603?Jan??5?11:38?id_dsa.pub?#root的公钥
?-rw-r--r--?1?root?root??395?Dec?30?23:47?known_hosts
?
?xad:
?[[email protected]?.ssh]$?ll?~/.ssh
?total?16
br/>?-rw-------.?1?root?root?1117?Jan??4?21:54?authorized_keys
?-rw-------.?1?root?root??668?Jan??5?11:57?id_dsa?????#root的私钥
?-rw-r--r--.?1?root?root??603?Jan??5?11:57?id_dsa.pub?#root的公钥
?-rw-r--r--.?1?root?root??397?Dec??6?20:33?known_hosts
?分别在localhost和xad上注册对方的公钥并进行登录验证
?[[email protected]?.ssh][email protected]??#使用这一指令实现公钥自动注册,-i指明要将哪个公钥注册到对方服务器上,root指明是要注册到对方服务器哪个账户下。
?The?authenticity?of?host?‘xad?(192.168.1.201)‘?can‘t?be?established.
br/>?RSA?key?fingerprint?is?04:2b:f5:62:4a:e6:8a:17:2b:7f:a8:c2:97:f9:d7:c6.
?Are?you?sure?you?want?to?continue?connecting?(yes/no)??yes
?/bin/ssh-copy-id:?INFO:?attempting?to?log?in?with?the?new?key(s),?to?filter?out?any?that?are?already?installed
?/bin/ssh-copy-id:?INFO:?1?key(s)?remain?to?be?installed?--?if?you?are?prompted?now?it?is?to?install?the?new?keys
[email protected]‘s?password:?????????????#需要先输入一次密码进行登录
?
br/>?Number?of?key(s)?added:?1??#显示注册了一个key文件
?
?Now?try?logging?into?the?machine,?with:???"ssh?‘[email protected]‘"
?and?check?to?make?sure?that?only?the?key(s)?you?wanted?were?added.?#提示已经注册完成,可以进行验证
br/>?[[email protected]?.ssh][email protected]
?Last?login:?Wed?Jan??4?22:58:28?2017?from?192.168.1.201?
br/>?[[email protected]?~]$???????????#成功从localhost通过密钥认证登录xad
?
br/>?xad:
?[[email protected]?.ssh][email protected]
[email protected]‘s?password:?
br/>?Now?try?logging?into?the?machine,?with?"ssh?‘[email protected]‘",?and?check?in:
?
br/>???.ssh/authorized_keys
?
?to?make?sure?we?haven‘t?added?extra?keys?that?you?weren‘t?expecting.
?
?[[email protected]?.ssh][email protected]
?Last?login:?Thu?Jan??5?12:15:30?2017?from?xad
br/>?[[email protected]?~]$???????#从xad通过密钥认证成功登录localhost
?
?xshell实现基于密钥认证
?
?
?在xshell软件中选择“工具”,然后选择“新建用户密钥生成向导”

?
?
?选择生产密钥的参数,图中选择的是DSA加密算法,密钥长度2048位。

?
?
?密钥对生成成功

?
?
?给用户私钥取名,设置密码(可以不设置密码)

?
?
?保存公钥为某文件,文件名保持默认即可

?
?
?密钥对创建完成

?
?
?确认服务器端配置文件/etc/ssh/sshd_config中启用了PubkeyAuthentication和AuthorizedKeysFile。
这里只需要确认这两项不是配置为no且未被注释掉即可,配置文件中默认为这两项即为yes,尽管其前面有#,初始状态下的/etc/ssh/sshd_config中的内容都可以理解为sshd的默认配置。

?
?
?将xshell生成的公钥添加到/home/root/.ssh/authorized_keys文件中
打开之前保存下来的公钥文件,然后复制,在vim authorized_keys时粘贴过去。
?
?
?在xshell中进行登录验证
在xshell中使用ssh [email protected]?,弹出登录框时,选择“Public key",然后选择刚生成的私钥,如果有设置私钥密码一并输入密码,然后”确定“即可建立连接。

也可在xshell中新建会话时,即指定此会话的身份验证方式为Public key,以后在打开这一会话时就会自动登录,而不用再去手动选择Public key。
?
?
?SecureCRT实现基于密钥认证
SecureCRT要实现基于密钥的认证,方法和xshell大同小异。先是生成密钥对,然后将公钥注册到SSH服务器,同时在建立的会话属性,认证方法处只选择”Public key“,如下所示:

需要注意的是SecureCRT生成密钥时,格式选择为”openssh format"。其生成公钥文件,只能从其保存的文件Identity.pub(默认文件名,可按需修改)文件中查看,只能从它复制,然后在vim authorized_keys时粘贴过去,无法在key生成工具处直接复制。
?
?简单地总结一下:要想实现A机器的a账户密钥认证登录B机器的b账户上,取决于A机器a账户是否保存的有a的私钥,并且B机器b账户中是否保存有a的公钥
?

以上是关于ssh密钥认证的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Linux 中配置基于密钥认证的 SSH

SourceTree提示ssh密钥认证失败的解决

SSH密钥认证

SSH密钥认证

转载 :配置ssh密钥认证自动登录

Xshell密钥认证