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: 
![](https://s1.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=)
[root@localhost ~]$ 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:a4 root@localhost  #这一指纹信息的最后部分表明了密钥的身份必须是:localhost主机上的root账户。 
 
br/>xad: 
![](https://s1.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=)
[root@xad ~]$ 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:9e root@xad  #此密钥对所有者为xad中的root账户。 
br/>查看localhost和xad各自的公钥
localhost:
![](https://s1.51cto.com/images/blog/201801/09/2489f2ebe31706b84640f5d822caaeb3.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
[root@localhost .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:
![](https://s1.51cto.com/images/blog/201801/09/ecb89b4638b22ece4a1be5daf8cb546a.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
[root@xad .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上注册对方的公钥并进行登录验证

[root@localhost .ssh]$ ssh-copy-id -i id_dsa.pub root@xad  #使用这一指令实现公钥自动注册,-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
root@xad's password:             #需要先输入一次密码进行登录
br/>Number of key(s) added: 1  #显示注册了一个key文件

Now try logging into the machine, with:   "ssh 'root@xad'"
and check to make sure that only the key(s) you wanted were added. #提示已经注册完成,可以进行验证
br/>[root@localhost .ssh]$ ssh root@xad
Last login: Wed Jan  4 22:58:28 2017 from 192.168.1.201 
br/>[root@xad ~]$           #成功从localhost通过密钥认证登录xad
br/>xad:
[root@xad .ssh]$ ssh-copy-id -i id_dsa.pub root@localhost
root@localhost's password: 
br/>Now try logging into the machine, with "ssh 'root@localhost'", and check in:
br/>  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[root@xad .ssh]$ ssh root@localhost
Last login: Thu Jan  5 12:15:30 2017 from xad
br/>[root@localhost ~]$       #从xad通过密钥认证成功登录localhost

xshell实现基于密钥认证


在xshell软件中选择“工具”,然后选择“新建用户密钥生成向导”
ssh密钥认证
选择生产密钥的参数,图中选择的是DSA加密算法,密钥长度2048位。
ssh密钥认证
密钥对生成成功
ssh密钥认证
给用户私钥取名,设置密码(可以不设置密码)
ssh密钥认证
保存公钥为某文件,文件名保持默认即可
ssh密钥认证
密钥对创建完成
ssh密钥认证
确认服务器端配置文件/etc/ssh/sshd_config中启用了PubkeyAuthentication和AuthorizedKeysFile。
这里只需要确认这两项不是配置为no且未被注释掉即可,配置文件中默认为这两项即为yes,尽管其前面有#,初始状态下的/etc/ssh/sshd_config中的内容都可以理解为sshd的默认配置。
ssh密钥认证
将xshell生成的公钥添加到/home/root/.ssh/authorized_keys文件中
打开之前保存下来的公钥文件,然后复制,在vim authorized_keys时粘贴过去。


在xshell中进行登录验证
在xshell中使用ssh root@ ,弹出登录框时,选择“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的公钥

转载于:https://blog.51cto.com/13489177/2058792

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值