关于AS5 SSH支持key认证的完全解决方案

简介:
      SSH 为 Secure Shell 的缩写, 专为远程登录会话和其他网络服务提供安全性的协议。利用 SSH 协议可以有效防止远程管理过程中的信息泄露问题。透过 SSH 可以对所有传输的数据进行加密,也能够防止 DNS欺骗和IP欺骗.还有一个优点就是为其传输的数据是经过压缩的,所以可以加快传输的速度.
    
  在客户端来看,SSH提供两种级别的安全验证。
 
第一种级别(基于密码的安全验证),知道帐号密码,就可登录到远程主机,并且所有传输的数据都会被加密。但是,可能会有别的服务器在冒充真正的服务器,无法避免被“中间人”攻击。 
 
第二种级别(基于密匙的安全验证),需要依靠密匙,也就是你必须为自己创建一对密匙,并把公有密匙放在需要访问的服务器上。客户端软件会向服务器发出请求,请求用你的密匙进行安全验证。服务器收到请求之后,先在你在该服务器的用户根目录下寻找你的公有密匙,然后把它和你发送过来的公有密匙进行比较。如果两个密匙一致,服务器就用公有密匙加密“质询”(challenge)并把它发送给客户端软件。从而避免被“中间人”攻击。
 
在服务器端,SSH也提供两种安全验证。
 
第一种方案,主机将自己的公用密钥分发给相关的客户端,客户端在访问主机时则使用该主机的公开密钥来加密数据,主机则使用自己的私有密钥来解密数据,从而实现主机密钥认证,确定客户端的可靠身份。
 
第二种方案,存在一个密钥认证中心,所有提供服务的主机都将自己的公开密钥提交给认证中心,而任何作为客户端的主机则只要保存一份认证中心的公开密钥就可以了。在这种模式下,客户端必须访问认证中心然后才能访问服务器主机。
 
 
实验环境:
 

解决方案:
可以通过客户端或者服务器端提供ssh安全认证
 
1.客户端提供ssh安全认证,也就是上面提到第二种级别
1.客户端
利用客户端linux系统提供ssh安全认证,如客户端是XP系统,可以选择用SecureCRT自动生成ssh2的公钥与私钥.
由于本地系统是windows 7 ,这里使用SecureCRT生成ssh认证。
SecureCRT---Options---Global Options----SSH2
 

   

  

完成后会在C:\Users\Administrator\AppData\Roaming\VanDyke\目录自动生成以Identity命名公钥与私钥.
2)服务器端
为了server安全性考虑,禁止空密码与密码登录系统。
需修改/etc/ssh/sshd.conf
 
#PermitEmptyPasswords no       修改为          PermitEmptyPasswords no
PasswordAuthentication yes   =========      PasswordAuthentication no
 
 [root@viong .ssh]#  service sshd restart
需要重启 ssh 才能使配置生效
Stopping sshd:                                             [ OK ]
Starting sshd:                                              [ OK ]
[root@viong ~]#  ssh localhost
The authenticity of host 'localhost (127.0.0.1)' can't be established.
RSA key fingerprint is 05:de:f8:d8:7b:db:36:0a:1e:01:cc:06:64:0e:7e:0a.
Are you sure you want to continue connecting (yes/no)?  yes
Warning: Permanently added 'localhost' (RSA) to the list of known hosts.
root@localhost's password:  输入系统密码
Last login: Tue Feb 23 00:32:06 2010 from 192.168.15.253
[root@viong ~]#  cd ~/.ssh
 
利用RZ小工具上传刚刚客户端生成的Identity.pub到~/.ssh目录下
关于RZ 与SZ的安装请看 http://viong.blog.51cto.com/844766/260081
 
[root@viong .ssh]# rz 
 
上传 Identity.pub  
 
[root@viong .ssh]#  cat Identity.pub >>authorized_keys
 
把公钥的内容输出到 authorized_keys
 
[root@viong .ssh]#  rm -rf Identity.pub
 
删除公钥
 
[root@viong .ssh]#  chmod 400 authorized_keys
 
设置 authorized_keys 的权限为 400
 
[root@viong .ssh]#  ll
total 12
-r-------- 1 root root 608 Feb 23 02:38 authorized_keys
-rw-r--r-- 1 root root 391 Feb 23 02:34 known_hosts
 
重新连接服务器,提示密钥输入密码,说明设置成功了.
 
======================================================================
客户端与服务器端都是linux系统基于RSA认证无私钥保护密码情况下
Client:192.168.15.50    server:192.168.15.64
Clinet
[root@client .ssh]# pwd
/root/.ssh   如没这个目录,可以手动添加
[root@client .ssh]# ssh-keygen –t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):  直接回车
Enter passphrase (empty for no passphrase):  直接回车
Enter same passphrase again:  直接回车
Your identification has been saved in viong.
Your public key has been saved in  id_rsa  .pub.
The key fingerprint is:
f9:14:16:7f:a8:68:f2:90:46:2e:28:b8:45:cc:aa:e9 root@client
[root@client .ssh]# ll
total 8
-rw------- 1 root root 1675 Feb 23 22:21  id_rsa
-rw-r--r-- 1 root root 393 Feb 23 22:21   id_rsa  .pub
 [root@client .ssh]# ssh-copy-id -i /root/.ssh/ id_rsa .pub   root@192.168.15.64
发布公匙到服务器端,会在服务器端自动生成 .ssh/authorized_keys 这个文件
20
reverse mapping checking getaddrinfo for macbook_air failed - POSSIBLE BREAK-IN ATTEMPT!
root@192.168.15.64's password:  输入 192.168.15.64 root 密码
Now try logging into the machine, with "ssh '192.168.15.64'", and check in:
 .ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
提示: ssh-copy-id  这个工具是针对 22 号端口,如果对方 ssh 端口是 2520 ,那么就要修改为:
ssh-copy-id -i /root/.ssh/  id_rsa   .pub    -p 2520  192.168.15.64
 
Server
 
[root@server .ssh]# ll
查看是否有 authorized_keys 生成
total 16
-rw- ------ 1 root root 393 Feb 23 22:28 authorized_keys
-rw-r--r-- 1 root root 395 Feb 23 21:49 known_hosts
[root@server .ssh]# cat authorized_keys 确认是否发布成功
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAtUOq6yotBEkldfBFZalh4FrFtX6wERChbLn7igQI4aofBNThWgIlBPfaThncfhs2Vu9DSBxExyGje/evcu/TyQRVBTuejEQnF4RPB+5fNMS+Qq48pdS33Rd/yt4m+Np/eGYDYW/ch5r/MRm5nek/WCW2MPAQbqrSZ7prj++SnMlavZDlAVsgHbY3GLYDD/7UbFJvewbY099zjSrW7yqIPdF9nACIg0dN+aMRtezJwr+NvOFZwrUuuz/ek1iCl2Q6soxRAkOTWbkE5bnF5LJJWtOox+ZxDu/mAry0dSv3eH0NMs6NPfBkwE5pyE9HG47r7k6kp7qKsuKCysk+MASNaQ== root@client

在客户端输入
[root@client .ssh]# ssh root@192.168.15.64   
Last login: Wed Feb 24 00:13:09 2010 from 192.168.15.50
 
注意: Enter file in which to save the key (/root/.ssh/id_rsa):  直接回车---这个地方不能输入名称,不然无密码登陆会报以下错误:
Permission denied (publickey,gssapi-with-mic
 
可以等生成后再做修改
===================================================================
 
[root@server ~]#      连接成功
 
提示 :由于生成密钥的口令的密码为空,可以利用 scp 远程加密备份, scp 是有 Security 的文件 copy ,基于 ssh 登录。操作起来比较方便,比如要把当前一个文件 copy 到远程另外一台主机上,可以如下命令。
[root@clinet .ssh]# scp id_dsa.pub root@192.168.15.64:/home/  
reverse mapping checking getaddrinfo for macbook_air failed - POSSIBLE BREAK-IN ATTEMPT!
id_dsa.pub                                    100% 601     0.6KB/s   00:00
[root@server .ssh]# ll /home/
total 4
-rw-r--r-- 1 root root 601 Feb 24 00:25 id_dsa.pub    说明拷贝成功
     如果想反过来操作,把文件从远程主机 copy 到当前系统,也很简单。
[root@ server home]#  scp id_dsa.pub root@192.168.15.50:/home/
The authenticity of host '192.168.15.50 (192.168.15.50)' can't be established.
RSA key fingerprint is 05:de:f8:d8:7b:db:36:0a:1e:01:cc:06:64:0e:7e:0a.
Are you sure you want to continue connecting (yes/no)?  yes
Warning: Permanently added '192.168.15.50' (RSA) to the list of known hosts.
Address 192.168.15.50 maps to ysgame2-pc, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
root@192.168.15.50's password:  输入 client 密码
id_dsa.pub                    100% 601     0.6KB/s   00:00 
 
[root@ clinet .ssh]#  ll /home/
total 4
-rw-r--r-- 1 root root 601 Feb 24 00:29 id_dsa.pub      说明拷贝成功
 
2.服务器端提供ssh安全认证,也就上面提到第一种方案
1)服务器端(AS5)
同样修改/etc/ssh/sshd.conf
 
#PermitEmptyPasswords no       修改为          PermitEmptyPasswords no
PasswordAuthentication yes   =========      PasswordAuthentication no
 
 [root@viong .ssh]#  service sshd restart
需要重启 ssh 才能使配置生效
Stopping sshd:                                             [ OK ]
Starting sshd:                                              [ OK ]
 
[root@viong ~]#  ssh localhost
第一次通过 ssh 命令登入服务器时,要输入 “yes” ,再输入本地服务器的密码,登入信息记入到用户主目录下 .ssh known_host 文件中。
The authenticity of host 'localhost (127.0.0.1)' can't be established.
RSA key fingerprint is 05:de:f8:d8:7b:db:36:0a:1e:01:cc:06:64:0e:7e:0a.
Are you sure you want to continue connecting (yes/no)?  yes
Warning: Permanently added 'localhost' (RSA) to the list of known hosts.
root@localhost's password:     输入系统密码
Last login: Tue Feb 23 00:32:06 2010 from 192.168.1.253
[root@viong ~]# cat known_hosts
localhost ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAy1OKMCYaKdT544qv0hRBBC6rFZviEJ6ECwnBV/mNkwahgkPp5TTHyHDUeBPtYmmKVdcxgjItUtTBHFPQjN2aKwNmPfikqf0WoaN7yf2F6IVAyAIaIi5jXwkJzadW61izmCEkdi/33r4oj5uoNfTjMBfghZZzrMwGZHEaEg2nWbbhMJrzuMU6v31WhjPm6MlZ+xBoR7T2iqiDyUczzSJZ2KG2rPMTG6Zdxm+WLrioOoeIn25wY+4gsrpPUGHxQsxVNIpUP6UtGZA/Y9uksgN5OSIkSxRwGPDTCp65mYQ7pDo9Pvbxg1yQFs54+zA2XcsKz55mbn0MMucPrOxbfGtIEQ==
 
以上连接是基于口令认证,相对telnet 还是很安全. 但还是会受到“中间人”攻击
 
[root@viong ~]#  cd ~/.ssh
[root@viong ~]#  ssh-keygen -t dsa 生成公钥与私钥
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):  viong  密钥文件名
Enter passphrase (empty for no passphrase):  输入口令
Enter same passphrase again:  再次输入口令
Your identification has been saved in viong.
Your public key has been saved in viong.pub.
The key fingerprint is:
cf:fd:14:c1:7c:eb:de:e4:5e:bb:91:6a:70:57:1c:8e root@viong
 
[root@viong .ssh]#  ll
total 12
-rw-r--r-- 1 root root 391 Feb 23 02:34 known_hosts
-rw------- 1 root root 736 Feb 23 03:08 viong
-rw-r--r-- 1 root root 600 Feb 23 03:08 viong.pub
 
[root@viong .ssh]#  cat  viong.pub >>authorized_keys
把公钥的内容输出到 authorized_keys
[root@viong .ssh]#  chmod 400 authorized_keys
设置 authorized_keys 的权限为 400
2)客户端(WIN 7-SecureCRT)
把viong  私钥和viong.pub 公钥用sz 小工具下载到本地同一个目录
[root@viong .ssh]# sz viong
[root@viong .ssh]# sz viong.pub
[root@viong .ssh]#  rm -rf viong.pub
[root@viong .ssh]#  rm -rf viong
删除公钥与密钥
 
用SecureCRT测试连接,如下图,说明连接成功。

 
关于大批量服务器更改key的脚本,后面会更新上去.
 
备注: 关于 /etc/ssh/sshd.conf详解,转载了鸟哥的文章给大家参考。
 





    本文转自viong 51CTO博客,原文链接:http://blog.51cto.com/viong/278062,如需转载请自行联系原作者



相关文章
|
6月前
|
Linux 网络安全 开发工具
百度搜索:蓝易云【Git安装 + 多站点SSH Key配置教程。】
现在,你已经成功安装了Git,并配置了多站点的SSH Key。你可以使用Git命令进行版本控制,并通过SSH Key进行身份验证来访问不同的Git仓库。
153 0
|
1月前
|
存储 弹性计算 算法
SSH学习(三)- SSH协议中的Public Key Algorithm
在SSH协议中,有两个地方涉及到公钥算法,分别是: 1. 服务端认证:服务端在进行密钥协商的时候证明自己身份,防止中间人攻击,此时为SSH-TRANS协议发生的事情; 2. 客户端认证:客户端通过PublicKey方式证明自己身份,完成SSH登录认证,此时SSH-USERAUTH发生的事情; 这两种情况下的公钥算法使用的是同一个概念,接下来本文将主要基于PublicKey公钥认证方式,学习对应的内容。
46 1
SSH学习(三)- SSH协议中的Public Key Algorithm
|
2月前
|
网络安全 数据安全/隐私保护
如何使用ssh key免密码登录服务器?
如何使用ssh key免密码登录服务器?
|
6月前
|
Shell Linux 网络安全
git实战—Gerrit配置SSH key & 下载代码到本地 & 使用VScode编辑器编辑提交代码——2023.07
git实战—Gerrit配置SSH key & 下载代码到本地 & 使用VScode编辑器编辑提交代码——2023.07
302 0
|
7月前
|
存储 Linux 网络安全
Git 配置SSH,多个 Github 账号配置不同的SSH KEY(二)
Git 配置SSH,多个 Github 账号配置不同的SSH KEY
|
7月前
|
Shell 网络安全 开发工具
Git 配置SSH,多个 Github 账号配置不同的SSH KEY(一)
Git 配置SSH,多个 Github 账号配置不同的SSH KEY
|
8月前
|
网络安全 Windows
GitLab配置ssh key
GitLab配置ssh key
|
8月前
|
缓存 网络安全 开发工具
GitLab上传文件 和 GitLab配置ssh key
GitLab上传文件 和 GitLab配置ssh key
160 0
|
9月前
|
Shell 网络安全 开发工具
windows下GitHub的SSH key配置
windows下GitHub的SSH key配置
204 0
|
9月前
|
Linux 网络安全 数据安全/隐私保护
Linux 使用ssh密钥认证方式登陆另一台linux服务器
Linux 使用ssh密钥认证方式登陆另一台linux服务器