zoukankan      html  css  js  c++  java
  • [SSH服务]——SSH详解、常用的远程连接工具

    在总结ssh原理前,我先做了一个ssh过程的实验

    首先我搭建了这样一个实验环境:

    (1) SSH Server:10.0.10.198

    (2) SSH Client:10.0.10.158

    在Server端 10.0.10.198,我执行了如下操作:

    (1)清空了服务器端提供的公钥与自己的密钥:

    [root@localhost ssh]# rm /etc/ssh/ssh_host*
    rm:是否删除普通文件 "/etc/ssh/ssh_host_dsa_key"?y
    rm:是否删除普通文件 "/etc/ssh/ssh_host_dsa_key.pub"?y
    rm:是否删除普通文件 "/etc/ssh/ssh_host_key"?y
    rm:是否删除普通文件 "/etc/ssh/ssh_host_key.pub"?y
    rm:是否删除普通文件 "/etc/ssh/ssh_host_rsa_key"?y
    rm:是否删除普通文件 "/etc/ssh/ssh_host_rsa_key.pub"?y
    

     (2)重启了SSH服务之后,它会重新生成这些的公钥和密钥:

    [root@localhost ssh]# service sshd restart
    停止 sshd:                                                [确定]
    生成 SSH1 RSA 主机键:                                      [确定]
    生成 SSH2 RSA 主机键:                                      [确定]
    正在生成 SSH2 DSA 主机键:                                  [确定]
    正在启动 sshd:                                            [确定]
    [root@localhost ssh]# date; ll /etc/ssh/ssh_host*
    2016年 06月 23日 星期四 11:58:38 CST
    -rw------- 1 root root  668 6月  23 11:58 /etc/ssh/ssh_host_dsa_key
    -rw-r--r-- 1 root root  590 6月  23 11:58 /etc/ssh/ssh_host_dsa_key.pub
    -rw------- 1 root root  963 6月  23 11:58 /etc/ssh/ssh_host_key
    -rw-r--r-- 1 root root  627 6月  23 11:58 /etc/ssh/ssh_host_key.pub
    -rw------- 1 root root 1671 6月  23 11:58 /etc/ssh/ssh_host_rsa_key
    -rw-r--r-- 1 root root  382 6月  23 11:58 /etc/ssh/ssh_host_rsa_key.pub
    

     (3)并且,我清空了服务器root用户下的/root/.ssh/known_hosts:

    [root@localhost .ssh]# pwd
    /root/.ssh
    [root@localhost .ssh]# cat known_hosts   ##已经清空了这个文件
    

    在Client端 10.0.10.158,我执行了如下操作:

    (1)同样清空了客户端机子上root用户下的/root/.ssh/known_hosts:

    [root@lyj1 .ssh]# pwd
    /root/.ssh
    [root@lyj1 .ssh]# cat known_hosts 
    

     (2)然后尝试着远程登录(以root)登录到Server 10.0.10.198。

         此时Server接收到了Client的这个请求后,会把自己的公钥传送给客户端。

         客户端记录这个服务器公钥的文件就是 ~/.ssh/known_hosts。  ——>这里记录的服务器公钥,即是服务端中产生的/etc/ssh/ssh_host_rsa_key.pub

         因为这是这台Client第一次连接这个Server,所以会询问你,是否要接收这个服务器发给你的这个服务器公钥呢?

    [root@lyj1 .ssh]# ssh  root@10.0.10.198
    The authenticity of host '10.0.10.198 (10.0.10.198)' can't be established.
    RSA key fingerprint is b3:bb:53:c0:3c:6a:f6:08:8b:a1:e3:b1:70:2b:7b:2a.
    Are you sure you want to continue connecting (yes/no)?
    

     (3)选择yes,则提示你输入密码(我们想要以root用户登录进Server,所以应输入正确的Server的root用户密码)

    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '10.0.10.198' (RSA) to the list of known hosts.
    root@10.0.10.198's password:                            #输入正确密码后
    Last login: Thu Jun 23 12:00:49 2016 from 10.0.10.198   #登录成功
    

      (4)然后我们回到Client端,看一下~/.ssh/known_hosts下的内容

    [root@localhost ~]# exit
    logout
    Connection to 10.0.10.198 closed.
    [root@lyj1 .ssh]# cat known_hosts  #是的这就是服务器传给客户端的那个公钥
    10.0.10.198 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA8RC2xIKGNKJwcHbEyTD7nRr0FE7yTfTxc3fgDMBE8ZHEv9ojSxKDuXP1w5tPF5UxCGKx0B5TmozKpRU+nqGLtL1p7n3PC1RxjXxov5/yElIrdPzRMhynrYsQJTp3M9WdojzWmZgoLJ+rvcfR1makDujhhkR/zz0MQltDT3NFCG73pgM+vTk7Z9vlohZIwmRNmth/OQvWA7MHya4WjkghcbLSYazFXmbatEBdF+1hUvQxUdWW4MqAv7cOZ96Zb7KQecmotbCbB/Nnas9tAmd55NpZ25TmtGaCR/ThUmfysjoDfBf40st9YZGJXDhgawSx0xn5as8mXpELr18h7ydPEQ==
    

     (5)当Client再次请求远程登录到Server,因为~/.ssh/known_hosts已经记录过该Server的公钥了,一比对发现发现也没有差异,就可以直接输密码了。

    [root@lyj1 .ssh]# ssh 10.0.10.198
    root@10.0.10.198's password: 
    Last login: Thu Jun 23 11:49:12 2016 from 10.0.10.158
    

     (6)当然,如果Server端重新生成了新的公钥和私钥,一比对发现和我们原来记录的有差异,会出现如下提示:

    [root@lyj1 .ssh]# ssh root@10.0.10.198
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    @    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
    Someone could be eavesdropping on you right now (man-in-the-middle attack)!
    It is also possible that the RSA host key has just been changed.
    The fingerprint for the RSA key sent by the remote host is
    c3:13:f9:67:33:e3:4d:75:77:11:db:4d:44:c3:3e:5d.
    Please contact your system administrator.
    Add correct host key in /root/.ssh/known_hosts to get rid of this message.
    Offending key in /root/.ssh/known_hosts:1    ##这是告诉你:这一行的记录(原来那个公钥)跟这一次接收到的公钥不一样
    RSA host key for 10.0.10.198 has changed and you have requested strict checking.
    Host key verification failed.
    

     (7)解决这个问题很简单,只要把~/.ssh/known_hosts的那一行删掉,重新ssh即可

    [root@lyj1 .ssh]# sed -i '1d' known_hosts 
    [root@lyj1 .ssh]# ssh root@10.0.10.198
    The authenticity of host '10.0.10.198 (10.0.10.198)' can't be established.
    RSA key fingerprint is c3:13:f9:67:33:e3:4d:75:77:11:db:4d:44:c3:3e:5d.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '10.0.10.198' (RSA) to the list of known hosts.
    root@10.0.10.198's password: 
    Last login: Thu Jun 23 15:25:07 2016 from 10.0.10.158
    

    回到Server 10.0.10.198端看看:

     (1)看看这个它的~/.ssh/known_hosts文件:

    [root@localhost .ssh]# cat known_hosts #没有东西
    [root@localhost .ssh]# 
    

     (按原理来说,Client也会把自己的公钥传送给Server,不过我不知道Server究竟将它记录在了哪个文件里)


     总结一下SSH的工作原理

    一. SSH服务简介
           ssh为Secure Shell的缩写,由IETF的网络工作开发的,SSH建立在应用层和传输层的安全协议上.
           ssh为系统安全和用户提供了有力的安全保障

    二. SSH认证原理
        (1)ssh链接的验证、加密方式:
            ssh链接CS模型(客户端-服务端),客户端发起链接,服务端对客户端进行验证,再考虑是否要链接
        (2) 加密体系:
             一种公钥加密:对称的密码加密体系
             一种私钥加密: 非对称密码加密体系 

    三. SSH工作过程
    1) 服务器建立自己的公钥文件、计算自己的私钥文件

    • 每启动一次SSH服务,服务会去找 /etc/ssh/ssh_host*
    • 这些文件记录的就是服务器自己的公钥和私钥
    • 如果删除掉这些文件,重启SSH服务时,它会重新计算公钥密钥(即重新生成这些文件)
    # ll /etc/ssh/ssh_host*
    -rw------- 1 root root  672 6月  23 15:25 /etc/ssh/ssh_host_dsa_key
    -rw-r--r-- 1 root root  590 6月  23 15:25 /etc/ssh/ssh_host_dsa_key.pub
    -rw------- 1 root root  963 6月  23 15:25 /etc/ssh/ssh_host_key
    -rw-r--r-- 1 root root  627 6月  23 15:25 /etc/ssh/ssh_host_key.pub
    -rw------- 1 root root 1675 6月  23 15:25 /etc/ssh/ssh_host_rsa_key
    -rw-r--r-- 1 root root  382 6月  23 15:25 /etc/ssh/ssh_host_rsa_key.pub
    # service sshd restart
    停止 sshd:                                                [确定]
    生成 SSH1 RSA 主机键:                                      [确定]
    生成 SSH2 RSA 主机键:                                      [确定]
    正在生成 SSH2 DSA 主机键:                                   [确定]
    正在启动 sshd:                                             [确定]
    

    2) 客户端主动请求建立连接,服务器会将自己的公钥传送给客户端:

    • 公钥的传送是明码传送(本来公钥就是要给大家用的)
    • 客户端的~/.ssh/known_hosts文件记录着这些服务器公钥,每次连接时会将服务器传送过来的公钥和这个文件的内容比对一下
    • 如果文件中没有相关记录,则提示问你要不要接收服务器传送给你的这个公钥,接收后记录在文件中
    • 如果文件中有过相关记录,则比对一下这次传送的公钥是否和文件中记录的那条公钥相同。

    3) 客户端接收好服务器公钥后,开始随机计算客户端自己的公钥和私钥:

    4) 客户端将自己的公钥传送给服务器:

        (我想知道服务器存放这个客户端公钥的文件是哪个,知道的盆友可以交流一下)

    5) 服务器接收到了客户端传给它的公钥后,表示服务器"信任"了这个客户端:

    6) 既然信任了,那么服务器就允许客户端远程连接自己了(当然啦还要把用户登录密码输对):

    7) 我们要知道,在这些过程之后,Server端和Client端都有了些什么?

                         

    四. SSH无密码登录原理

          从SSH的工作过程其实我们可以理解出SSH无密码登录的原理。

          当A要SSH到B,A要把A的公钥给B,B才会信任A,进而让A来远程自己。

          所以要实现A无密码登录到B,A得把产生的公钥给B,得到B的信任,才可实现无密码登录。

          可查看我的SSH无密码登录实验(http://www.cnblogs.com/snsdzjlz320/p/5612389.html)


    常用的远程连接工具:Xshell、SecureCRT、Putty、vncviewer

     

  • 相关阅读:
    HDU 1028 简单动态规划
    poj2250 动态规划+路径标记
    计算机网络你还懵逼吗?第二弹biubiubiu
    几款值得推荐的android(安卓)开源框架简介
    Android Studio安装更新终极解决方式
    个人开发者做一款Android App需要知道的事情
    Android webservice的用法详细讲解
    Android图片加载与缓存开源框架:Android Glide
    进入社会看到的一片总结,若有感慨
    Android 开源组件 ----- Android LoopView无限自动轮转控件
  • 原文地址:https://www.cnblogs.com/snsdzjlz320/p/5610927.html
Copyright © 2011-2022 走看看