openssh

1.使用SSH 访问远程命令行

1.1OpenSSH 简介

  • OpenSSH这一术语指系统中使用的Secure Shell软件的软件实施。用于在远程系统上安全运行shell。如果您在可提供ssh服务的远程Linux系统中拥有用户帐户,则ssh是通常用来远程登录到该系统的命令。ssh命令也可用于在远程系统中运行命令。

常用的远程登录工具有:

  • telnet
  • ssh
  • dropbear
telnet      //远程登录协议,23/TCP
    认证明文
    数据传输明文

ssh         //Secure SHell,应用层协议,22/TCP
    通信过程及认证过程是加密的,主机认证
    用户认证过程加密
    数据传输过程加密
    
dropbear    //嵌入式系统专用的SSH服务器端和客户端工具

  • Linux系统中,OpenSSH是目前最流行的远程系统登录与文件传输应用,也是传统TeleneFTPR系列等网络应用的换代产品。其中,sshSecure Shell)可以替代telnetrloginrsh ; scpSecure Copy)与sftp(Secure FTP)能够替代ftp

1.2 SSH版本

openssh有两个版本,分别为v1和v2,其特点如下:

v1:基于CRC-32做MAC,无法防范中间人(man-in-middle)攻击

v2:双方主机协议选择安全的MAC方式。基于DH算法做密钥交换,基于RSA或DSA算法实现身份认证

1.3 SSH 认证方式

openssh有两种认证方式,分别是:

  • 基于口令认证(v1

    (1) 当客户端发起ssh请求,服务器会把自己的公钥发送给用户;

  (2) 用户会根据服务器发来的公钥对密码进行加密;

  (3) 加密后的信息回传给服务器,服务器用自己的私钥解密,如果密码正确,则用户登录成功。

  • 基于密钥认证(v2)

    (1) 首先在客户端生成一个密钥对(ssh-keygen);

  (2) 并将客户端的公钥ssh-copy-id 拷贝到服务端;

  (3) 当客户端再次发送一个连接请求,包括ip、用户名;

  (4) 服务端得到客户端的请求后,会到authorized_keys中查找,如果有响应的IP和用户,就会随机生成一个字符串,例如:qwer;

  (5) 服务端将使用客户端拷贝过来的公钥进行加密,然后发送给客户端;

  (6) 得到服务端发来的消息后,客户端会使用私钥进行解密,然后将解密后的字符串发送给服务端;

  (7) 服务端接受到客户端发来的字符串后,跟之前的字符串进行对比,如果一致,就允许免密码登录。

1.4openSSH 的工作模式

openSSH是基于C/S架构工作的。

服务器端    //sshd,配置文件在/etc/ssh/sshd_config
客户端     //ssh,配置文件在/etc/ssh/ssh_config
    ssh-keygen      //密钥生成器
    ssh-copy-id     //将公钥传输至远程服务器
    scp             //跨主机安全复制工具

1.51.4 Secure Shell 示例

//以当前用户身份创建远程交互式shell,然后在结束时使用exit命令返回到之前的shell
[root@cl129 ~]# ssh 192.168.163.128
The authenticity of host '192.168.163.128 (192.168.163.128)' can't be established.
ECDSA key fingerprint is SHA256:JBwdPVKLaBKBGBBEst1+9wAfcsQaVoPcBOP9ioV4C8M.
ECDSA key fingerprint is MD5:91:7c:18:cf:a3:ee:34:ad:48:9e:fd:3a:c8:0e:bc:89.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.163.128' (ECDSA) to the list of known hosts.
root@192.168.163.128's password: 
Last login: Mon Apr  1 14:01:47 2019 from 192.168.163.1
[root@cl128 ~]# exit
登出
Connection to 192.168.163.128 closed.
[root@cl129 ~]# 

//以其他用户身份(remoteuser)在选定主机(remotehost)上连接到远程`shell`
[root@cl129 ~]# ssh chen@192.168.163.128
chen@192.168.163.128's password: 
[chen@cl128 ~]$ exit
登出
Connection to 192.168.163.128 closed.

//以远程用户身份(remoteuser)在远程主机(remotehost)上通过将输出返回到本地显示器的方式来执行单一命令
[root@cl128 ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.163.128  netmask 255.255.255.0  broadcast 192.168.163.255
        inet6 fe80::20c:29ff:fe02:e223  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:02:e2:23  txqueuelen 1000  (Ethernet)
        RX packets 21329  bytes 10347716 (9.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16979  bytes 29799178 (28.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@cl129 ~]# ssh 192.168.163.128 'ifconfig eth0'
root@192.168.163.128's password: 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.163.128  netmask 255.255.255.0  broadcast 192.168.163.255
        inet6 fe80::20c:29ff:fe02:e223  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:02:e2:23  txqueuelen 1000  (Ethernet)
        RX packets 21374  bytes 10352581 (9.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 17000  bytes 29803599 (28.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

/w命令可以显示当前登录到计算机的用户列表。这对于显示哪些用户使用ssh从哪些远程位置进行了登录以及执行了何种操作等内容特别有用
[root@cl128 ~]# w
 14:33:04 up  3:34,  3 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      10:47   32:40   0.46s  0.01s bash
root     pts/0    192.168.163.129  14:33    0.00s  0.04s  0.02s w
root     pts/1    192.168.163.1    14:01    1:52   0.09s  0.07s bash


1.5 SSH 主机密钥

ssh通过公钥加密的方式保持通信安全。当某一ssh客户端连接到ssh服务器时,在该客户端登录之前,服务器会向其发送公钥副本。这可用于为通信渠道设置安全加密,并可验证客户端的服务器。

当用户第一次使用ssh连接到特定服务器时,ssh命令可在用户的~/.ssh/known_hosts文件中存储该服务器的公钥。在此之后每当用户进行连接时,客户端都会通过对比~/.ssh/known_hosts文件中的服务器条目和服务器发送的公钥,确保从服务器获得相同的公钥。如果公钥不匹配,客户端会假定网络通信已遭劫持或服务器已被入侵,并且中断连接。

这意味着,如果服务器的公钥发生更改(由于硬盘出现故障导致公钥丢失,或者出于某些正当理由替换公钥),用户则需要更新其~/.ssh/known_hosts文件并删除旧的条目才能够进行登录。

//主机ID存储在本地客户端系统上的 ~/.ssh/known_hosts 中
[root@cl129 ~]# cat ~/.ssh/known_hosts 
192.168.163.128 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBK3kA+B6BQymVnyiwSAsRmOAuyeMT1FKhdyQ1j7W2J2bu+oi25dYvmjq62LgYeFI2Cwan8vgt2d8lc8v41pdno8=

//主机密钥存储在SSH服务器上的 /etc/ssh/ssh_host_key* 中

[root@cl129 ~]# ls /etc/ssh/*key*
/etc/ssh/ssh_host_ecdsa_key      /etc/ssh/ssh_host_ed25519_key.pub
/etc/ssh/ssh_host_ecdsa_key.pub  /etc/ssh/ssh_host_rsa_key
/etc/ssh/ssh_host_ed25519_key    /etc/ssh/ssh_host_rsa_key.pub

2. 配置基于 SSH 密钥的身份验证

用户可通过使用公钥身份验证进行ssh登录身份验证。ssh允许用户使用私钥-公钥方案进行身份验证。这意味着将生成私钥和公钥这两个密钥。私钥文件用作身份验证凭据,像密码一样,必须妥善保管。公钥复制到用户希望登录的系统,用于验证私钥。公钥并不需要保密。拥有公钥的ssh服务器可以发布仅持有您私钥的系统才可解答的问题。因此,可以根据所持有的密钥进行验证。如此一来,就不必在每次访问系统时键入密码,但安全性仍能得到保证。

使用ssh-keygen命令生成密码。将会生成私钥~/.ssh/id_rsa和公钥~/.ssh/id_rsa.pub

注意:

生成密钥时,系统将提供指定密码的选项,在访问私钥时必须提供该密码。如果私钥被偷,除颁发者之外的其他任何人很难使用该私钥,因为已使用密码对其进行保护。这样,在攻击者破解并使用私钥前,会有足够的时间生成新的密钥对并删除所有涉及旧密钥的内容。

  • 生成ssh密钥后,密钥将默认存储在家目录下的.ssh/目录中。私钥和公钥的权限就分别为600644.ssh目录权限必须是700

在可以使用基于密钥的身份验证前,需要将公钥复制到目标系统上。可以使用ssh-copy-id完成这一操作

//语法  : ssh-copy-id  remoteuser@remotehos

通过ssh-copy-id将密钥复制到另一系统时,它默认复制~/.ssh/id_rsa.pub文件

ssh-keygen -t rsa //指定秘钥的算法

[root@cl129 ~]# 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:
SHA256:itJuNDECzeeARChm9RtJYZrMbJu3J9fwMX9eIcEixrQ root@cl129
The key's randomart image is:
+---[RSA 2048]----+
|+*.. +.  .       |
|=oB B . o . .    |
|+. @ +   E . o   |
|  o * o . . . .  |
|   + = .So   . . |
|   .+...+ +   . .|
|  ..o+.o o . . . |
|   o. +     o .  |
|   ..        .   |
+----[SHA256]-----+
[root@cl129 ~]# ls ~/.ssh/
id_rsa  id_rsa.pub  known_hosts

// 通过`ssh-copy-id`将密钥复制到另一系统时,它默认复制`~/.ssh/id_rsa.pub`文件
[root@cl129 ~]# ssh-copy-id root@192.168.163.128
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/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@192.168.163.128's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.163.128'"
and check to make sure that only the key(s) you wanted were added.

// 会在目标主机的 ~/.ssh/目录下生成authorized_keys文件
[root@cl128 ~]# ls ~/.ssh/
authorized_keys  id_rsa  id_rsa.pub

//使用 ssh 命令无远程主机密码登录远程主机
[root@cl129 ~]# ssh root@192.168.163.128
Enter passphrase for key '/root/.ssh/id_rsa': // 在设置ssh-keygen 时自己设的密码
Last login: Mon Apr  1 14:33:02 2019 from 192.168.163.129

//使用 scp 命令传送文件到远程主机
//语法:scp [选项] <上传文件的绝对路径> <remoteuser@remotehos:存放在哪个目录下>
//scp命令常用选项
    -r      //递归复制  常用选项,用来上传和下载目录
    -p      //保持权限
    -P      //端口
    -q      //静默模式
    -a      //全部复制

[root@cl129 ~]# mkdir cljhfy 
[root@cl129 ~]# touch cljhfy/{a,s,d,f}
[root@cl129 ~]# ls cljhfy/
a  d  f  s
[root@cl129 ~]# scp -r /root/cljhfy/ root@192.168.163.128:/root/
Enter passphrase for key '/root/.ssh/id_rsa': 
Enter passphrase for key '/root/.ssh/id_rsa': 
a                                                              100%    0     0.0KB/s   00:00    
s                                                              100%    0     0.0KB/s   00:00    
d                                                              100%    0     0.0KB/s   00:00    
f                                                              100%    0     0.0KB/s   00:00    


//目标主机
[root@cl128 ~]# ls
anaconda-ks.cfg  backup.tar.gz  cljhfy
[root@cl128 ~]# ls cljhfy/
a  d  f  s
//从目标主机下载文件是将顺序颠倒,将要下载的文件或目录放在前面
//语法:scp [选项]  <remoteuser@remotehos:目标文件的绝对路径> <存放文件的绝对路径>

3. 自定义 SSH 服务配置

虽然OpenSSH服务器通常无需修改,但会提供其他安全措施,可以在配置文件/etc/ssh/sshd_config中修改OpenSSH服务器的各个方面。

PermitRootLogin {yes|no}    //是否允许root用户远程登录系统
PermitRootLogin without-password    //仅允许root用户基于密钥方式远程登录
PasswordAuthentication {yes|no}     //是否启用密码身份验证,默认开启

4. SSH 安全注意事项

  • 密码应该经常换且足够复杂
[root@cl129 ~]# openssl rand 30 -base64
5zyry+zxkgloNk1W/np8Wj3WBAAKikE26rjNxbrB

  • 使用非默认端口
  • 限制登录客户端地址
  • 仅监听特定的IP地址
  • 禁止管理员直接登录
  • 仅允许有限制用户登录
    • AllowUsers
    • AllowGroups
  • 使用基于密钥的认证
  • 禁止使用空密码
  • 禁止使用SSHv1版本
  • 设定空闲会话超时时长
  • 利用防火墙设置ssh访问策略
  • 限制ssh的访问频度和并发在线数
  • 做好日志的备份,经常分析(集中于某台服务器)

5.示例

5.1说明密钥认证的过程

1:使用系统自带或工具利用密钥生成器制作一对密钥——一只公钥(id_rsa.pub)和一只私钥(id_rsa
2:把公钥添加到服务器 /root/.ssh/authorized_keys
3:客户端(服务器、或SecureCRT puttyxShell)利用私钥即可完成认证并登录

5.2实例

[root@cl129 ~]# 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:
SHA256:itJuNDECzeeARChm9RtJYZrMbJu3J9fwMX9eIcEixrQ root@cl129
The key's randomart image is:
+---[RSA 2048]----+
|+*.. +.  .       |
|=oB B . o . .    |
|+. @ +   E . o   |
|  o * o . . . .  |
|   + = .So   . . |
|   .+...+ +   . .|
|  ..o+.o o . . . |
|   o. +     o .  |
|   ..        .   |
+----[SHA256]-----+
[root@cl129 ~]# ls ~/.ssh/
id_rsa  id_rsa.pub  known_hosts
[root@cl129 ~]# ssh-copy-id root@192.168.163.128
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/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@192.168.163.128's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.163.128'"
and check to make sure that only the key(s) you wanted were added.

// 会在目标主机的 ~/.ssh/目录下生成authorized_keys文件
[root@cl128 ~]# ls ~/.ssh/
authorized_keys  id_rsa  id_rsa.pub

//使用 ssh 命令无远程主机密码登录远程主机
[root@cl129 ~]# ssh root@192.168.163.128
Enter passphrase for key '/root/.ssh/id_rsa': // 在设置ssh-keygen 时自己设的密码
Last login: Mon Apr  1 14:33:02 2019 from 192.168.163.129


原文地址:https://www.cnblogs.com/cljhfy/p/10635857.html