SSH命令与ssh免密登陆

目录

what is SSH?

Secure Shell(缩写为SSH),由IETF的网络工作小组(Network Working Group)所制定;SSH为一项创建在应用层和传输层基础上的安全协议,为计算机上的Shell(壳层)提供安全的传输和使用环境。使用Tcp 22端口。

ssh协议目前有SSH1和SSH2,SSH2协议兼容SSH1。目前实现SSH1和SSH2协议的主要软件有OpenSSH和SSH Communications Security Corporation 公司的SSH Communications 软件。前者是OpenBSD组织开发的一款免费的SSH软件,后者是商业软件,因此在linux、FreeBSD、OpenBSD、NetBSD等免费类UNIX系统种,通畅都使用OpenSSH作为SSH协议的实现软件。

两种方式的用户登录认证:
  • 基于password:知道帐号和密码,就可以登录到远程主机,并且所有传输的数据都会被加密。但是,可能会有别的服务器在冒充真正的服务器,无法避免被“中间人”攻击。
  • 基于key:需要依靠密钥,也就是你必须为自己创建一对密钥,并把公有密钥放在需要访问的服务器上。客户端软件会向服务器发出请求,请求用你的密钥进行安全验证。服务器收到请求之后,先在你在该服务器的用户根目录下寻找你的公有密钥,然后把它和你发送过来的公有密钥进行比较。如果两个密钥一致,服务器就用公有密钥加密“质询”(challenge)并把它发送给客户端软件。从而避免被“中间人”攻击。

what is OpenSSH?

OpenSSHOpenBSD Secure Shell)是使用SSH通过计算机网络加密通信的实现。

Centos默认都会装有SSH,默认就是OpenSSH,可以通过 ssh -V 命令来查看安装的ssh版本信息:

[root@centos7 ~]# ssh -V
OpenSSH_6.6.1p1, OpenSSL 1.0.1e-fips 11 Feb 2013

SSH的客户端工具:

SSH是基于C/S(客户端/服务器端)架构的。

  • Linux Client: ssh, scp, sftp, slogin
  • Windows客户端:
    • xshell, putty, securecrt, sshsecureshellclient
  • Server: sshd

ssh客户端

配置文件:

系统配置文件:(/etc/ssh/ssh_config)

用户配置文件: (~/.ssh/config)

下面介绍一些常用的配置项:

Host 别名
HostName 主机名
Port 端口
User 用户名
IdentityFile 密钥文件的路径
IdentitiesOnly 只接受SSH key 登录
PreferredAuthentications 强制使用Public Key验证
StrictHostKeyChecking ask 首次登陆系统是否显示检查提示
ssh命令

两种格式:

  • ssh [user@]host [COMMAND]

  • ssh [-l user] host [COMMAND]

    • -p port:远程服务器监听的端口
    • -b:指定连接的源IP
    • -v:调试模式
    • -C:压缩方式
    • -X: 支持x11转发
    • -Y:支持信任x11转发;ForwardX11Trusted yes(需要修改配置文件)

    • -t: 强制伪tty分配

    ssh -t remoteserver1 ssh remoteserver2
    [root@centos7 .ssh]# ssh -t 192.168.8.128 ssh 192.168.8.141     #<==通过192.168.8.128 做跳转机去连接192.168.8.141
  • 允许实现对远程系统经验证地加密安全访问

  • 当用户远程连接ssh服务器时,会复制ssh服务器/etc/ssh/ssh_hostkey.pub(CentOS7默认是ssh_host_ecdsa_key.pub)文件中的公钥到客户机的~./ssh/know_hosts中。下次连接时,会自动匹配相应私钥,不能匹配,将拒绝连接

ssh服务登陆验证
基于用户和口令登录验证
  • 1 客户端发起ssh请求,服务器会把自己的公钥发送给用户
  • 2 用户会根据服务器发来的公钥对密码进行加密
  • 3 加密后的信息回传给服务器,服务器用自己的私钥解密,如果密码正确,则用户登录成功
基于密钥的登录方式
  • 1 首先在客户端生成一对密钥(ssh-keygen)
  • 2 并将客户端的公钥ssh-copy-id 拷贝到服务端
  • 3 当客户端再次发送一个连接请求,包括ip、用户名
  • 4 服务端得到客户端的请求后,会到authorized_keys中查找,如果有响应的IP和用户,就会随机生成一个字符串,例如: acdf
  • 5 服务端将使用客户端拷贝过来的公钥进行加密,然后发送给客户端
  • 6 得到服务端发来的消息后,客户端会使用私钥进行解密,然后将解密后的字符串发送给服务端
  • 7服务端接受到客户端发来的字符串后,跟之前的字符串进行对比,如果一致,就允许免密码登录

基于密钥的认证的实现:
- (1) 在客户端生成密钥对

```shell
ssh-keygen -t rsa [-P ''] [-f “~/.ssh/id_rsa"]  (-P 指定密码)
[root@centos6 ~]# 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 /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
68:ce:fb:1b:e3:ee:ad:d0:06:d4:cd:2f:de:42:26:d3 root@centos6.haiyun.com
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|       . o       |
|      . . o      |
|     . . . .     |
|      + S E .    |
|     + o * o     |
|      + = o .    |
|       = + .     |
|      .+Bo.      |
+-----------------+

```

​
  • (2) 把公钥文件传输至远程服务器对应用户的家目录

    `ssh-copy-id [-i [identity_file]] [user@]host`      #<==注:后面不要加路径
    [root@centos7 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.8.128
    root@192.168.8.128's password: 
    Now try logging into the machine, with "ssh 'root@192.168.8.128'", and check in:
    
      .ssh/authorized_keys
    
    to make sure we haven't added extra keys that you weren't expecting.
    [root@centos6 ~]# ls -l .ssh/authorized_keys       #<==查看复制过去的密钥,已改名为authorized_keys
    -rw-------. 1 root root 405 Sep 10 16:22 .ssh/authorized_keys
  • (3) 测试

    [root@centos7 ~]# ssh 192.168.8.128            #<==已实现免密登陆
    Last login: Sun Sep 10 16:09:15 2017 from 192.168.8.128
     
      .--,       .--,
     ( (  \.---./  ) )
      '.__/o   o\__.'
         {=  ^  =}
          >  -  <
         /       \
        //       \\
       //|   .   |\\
       "'\       /'"_.-~^`'-.
          \  _  /--'         `
        ___)( )(___
       (((__) (__)))    高山仰止,景行行止.虽不能至,心向往之。
    

  • (4) 在SecureCRT或Xshell实现基于key验证在SecureCRT工具—>创建公钥—>生成Identity.pub文件转化为openssh兼容格式(适合SecureCRT, Xshell不需要转化格式),并复制到需登录主机上相应文件authorized_keys中,注意权限必须为600,在需登录的ssh主机上执行:

    在xshell中生成密钥:

    将xshell生成的公钥传到服务器中,传到服务器应该不是问题。
    [root@centos6 ~]# cat id_rsa_2048.pub >> .ssh/authorized_keys      #<==将生成的公钥导入到.ssh/authorized_keys文件中

    新建会话:

  • (5)重设私钥口令:

    如果认为这样不够安全,我们可以对私钥设置口令:ssh-keygen –p

    [root@centos6 ~]# ssh-keygen -p        #<==会自动找到私钥文件
    Enter file in which the key is (/root/.ssh/id_rsa): 
    Key has comment '/root/.ssh/id_rsa'
    Enter new passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved with the new passphrase.
    [root@centos7 ~]# ssh 192.168.8.128            #<==加密后只需要输入一次口令即可
    Enter passphrase for key '/root/.ssh/id_rsa': 
    Last login: Sun Sep 10 16:55:33 2017 from 192.168.8.1

  • (6)验证代理(authentication agent)保密解密后的密钥,这样口令就只需要输入一次在GNOME中,代理被自动提供给root用户,否则运行ssh-agent bash

    [root@centos7 ~]# ssh-agent bash           #<==ssh验证代理

  • (7)钥匙通过命令添加给代理

    代理只会一次性生效,当退出这个bash 代理将失效。添加代理命令:ssh-add

[root@centos7 ~]# ssh-add                  #<==添加代理
Enter passphrase for /root/.ssh/id_rsa: 
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)
[root@centos7 ~]# ssh 192.168.8.128            #<==代理成功
Last login: Sun Sep 10 17:11:24 2017 from 192.168.8.128
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值