[comment]: # translation:outdated

[comment]: # ({26d9bc44-26d9bc44})
# 9 SSH检查

[comment]: # ({/26d9bc44-26d9bc44})

[comment]: # ({a37eff2e-4cdff2c1})
### 概述

SSH检查以agent无代理监控方式执行。SSH检查不需要Zabbix agent。

执行SSH检查时，Zabbix server必须预先
[4-配置源代码](/manual/installation/install#4-配置源代码)支持SSH2
（libssh或libssh2）。另请参阅：
[serverproxy](/manual/installation/requirements#serverproxy)。

::: noteimportant
从RHEL 8开始，仅支持libssh。对于其他发行版，建议使用libssh而非libssh2。

:::

[comment]: # ({/a37eff2e-4cdff2c1})

[comment]: # ({76b2e481-5d32b87c})
### 配置

[comment]: # ({/76b2e481-5d32b87c})

[comment]: # ({69e9c4a5-753f5c30})
##### 口令认证

SSH检查提供两种认证方式 - 用户名/密码组合和基于file的认证

若不打算使用密钥认证 除在源码编译时将libssh或libssh2链接至Zabbix外 无需额外配置

[comment]: # ({/69e9c4a5-753f5c30})

[comment]: # ({8e89bf18-44fd07da})
##### 密钥文件认证

要为SSH 监控项启用基于密钥的认证，需要对服务器配置进行特定修改。

打开Zabbix server的file配置文件
([*zabbix\_server.conf*](/manual/appendix/config/zabbix_server))，即`root`
找到以下行：

```ini
# SSHKeyLocation=
```
取消注释并设置公钥/私钥存储目录的完整路径：

```ini
SSHKeyLocation=/home/zabbix/.ssh
```
保存file后重启Zabbix server服务。

示例中的*/home/zabbix*是*zabbix*用户的主目录，
*.ssh*是默认通过[ssh-keygen](http://en.wikipedia.org/wiki/Ssh-keygen)命令
在主目录下生成公私钥的目录。

不同操作系统发行版的Zabbix server安装包
create通常将*zabbix*用户的主目录设为其他路径，
例如系统账户常用的*/var/lib/zabbix*。

生成密钥前，建议将主目录调整为*/home/zabbix*以匹配
上文提到的`SSHKeyLocation` Zabbix server
配置参数。

::: noteclassic
若已按[installation
section](/manual/installation/install#create_user_account)手动添加*zabbix*账户，
可跳过后续步骤。此类情况下用户主目录通常已是*/home/zabbix*。

:::

修改*zabbix*用户主目录前需停止相关进程：

```bash
systemctl stop zabbix-agent
systemctl stop zabbix-server
```
执行以下命令迁移现有主目录（若存在）：

```bash
usermod -m -d /home/zabbix zabbix
```
若原位置无主目录，则在新位置创建：

```bash
test -d /home/zabbix || mkdir /home/zabbix
```
为确保安全，可执行以下权限设置命令：

```bash
chown zabbix:zabbix /home/zabbix
chmod 700 /home/zabbix
```
重启之前停止的进程：

```bash
systemctl start zabbix-agent
systemctl start zabbix-server
```
现在可通过以下命令generate公私钥（为便于阅读，已注释命令提示符）：

```bash
sudo -u zabbix ssh-keygen -t rsa
# Generating public/private rsa key pair.
# Enter file in which to save the key (/home/zabbix/.ssh/id_rsa):
/home/zabbix/.ssh/id_rsa
# Enter passphrase (empty for no passphrase):
<Leave empty>
# Enter same passphrase again: 
<Leave empty>
# Your identification has been saved in /home/zabbix/.ssh/id_rsa.
# Your public key has been saved in /home/zabbix/.ssh/id_rsa.pub.
# The key fingerprint is:
# 90:af:e4:c7:e3:f0:2e:5a:8d:ab:48:a2:0c:92:30:b9 zabbix@it0
# The key's randomart image is:
# +--[ RSA 2048]----+
# |                 |
# |       .         |
# |      o          |
# | .     o         |
# |+     . S        |
# |.+   o =         |
# |E .   * =        |
# |=o . ..* .       |
# |... oo.o+        |
# +-----------------+
```
::: noteclassic
默认生成的公钥(*id\_rsa.pub*)和私钥(*id\_rsa*)
存储在*/home/zabbix/.ssh*目录，
与Zabbix server的`SSHKeyLocation`配置参数相符。

:::

::: noteimportant
虽然ssh-keygen工具和SSH服务器可能支持"rsa"以外的密钥类型，
但Zabbix使用的libssh2库可能不支持其他类型。

:::

[comment]: # ({/8e89bf18-44fd07da})

[comment]: # ({b84f1792-7e6b0273})
##### Shell 配置表单

此步骤仅需对每个要通过SSH检查监控的主机执行一次。

通过以下命令，可将**公钥**file安装到远程主机 *10.10.10.10*上，从而使用*root*账户执行SSH检查（为提高可读性，命令提示符已被注释）：

```bash
sudo -u zabbix ssh-copy-id root@10.10.10.10
# The authenticity of host '10.10.10.10 (10.10.10.10)' can't be established.
# RSA key fingerprint is 38:ba:f2:a4:b5:d9:8f:52:00:09:f7:1f:75:cc:0b:46.
# Are you sure you want to continue connecting (yes/no)?
yes
# Warning: Permanently added '10.10.10.10' (RSA) to the list of known hosts.
# root@10.10.10.10's password:
<Enter root password>
# Now try logging into the machine, with "ssh 'root@10.10.10.10'",
# and check to make sure that only the key(s) you wanted were added.
```
现在可以使用*zabbix*用户账户的默认私钥（*/home/zabbix/.ssh/id\_rsa*）来测试SSHlogin：

```bash
sudo -u zabbix ssh root@10.10.10.10
```
若login成功，则表明shell中的配置部分已完成，可关闭远程SSH会话。

[comment]: # ({/b84f1792-7e6b0273})

[comment]: # ({14e0e4a9-fe23daa8})
##### 监控项配置

实际执行的命令必须放置在监控项配置的*Executed script*字段中。
通过将多个命令放置在新行上可以依次执行多个命令。这种情况下返回值也将以多行格式呈现。

![](../../../../../assets/en/manual/config/items/itemtypes/ssh_item.png){width="600"}

所有必填字段均以红色星号标记。

SSH 监控项需要特定信息的字段包括：

| 参数 | 描述 | 备注 |
|--|------|------|
| *Type* | 在此选择**SSH agent**。 |          |
| *Key* | 格式为**ssh.run\[唯一简短描述,<ip>,<端口>,<编码>,<ssh选项>\]**的唯一（每个主机）监控项键 | **唯一简短描述**是必填项，且每个主机的SSH 监控项应保持唯一。<br><br>默认端口为22，而非此监控项所分配接口中指定的端口。<br><br>**ssh选项**（自version 6.0.25起支持；要求version的libssh版本0.9.0及以上或libssh2）允许以*key1=value1;key2=value2,value3*格式传递额外SSH选项。单个键的多个值可用逗号分隔传递（此时参数必须[参数引用字符串](/manual/config/items/item/key#参数引用字符串)）；多个选项键可用分号分隔。<br><br>支持的选项键包括：`KexAlgorithms`、`HostkeyAlgorithms`、`Ciphers`、`MACs`。选项键和值的支持取决于SSH库；若选项不受支持将返回错误，且监控项将变为不支持状态。<br><br>注意：不支持用于追加密码设置的"+"符号和禁用特定密码设置的"!"符号（如GnuTLS和OpenSSL中的用法）。<br><br>示例：<br>=> `ssh.run[KexAlgorithms,127.0.0.1,,,Ciphers=aes128-ctr]`<br>=> `ssh.run[KexAlgorithms,,,,"KexAlgorithms=diffie-hellman-group1-sha1;HostkeyAlgorithms=ssh-rsa,ssh-dss,ecdh-sha2-nistp256"]` |
| *Authentication method* | 选择"Password"或"Public key"之一。 |          |
| *User name* | 用于远程主机认证的用户名。必填。 |          |
| *Public key file* | 当*Authentication method*为"Public key"时的公钥文件名。必填。 | 示例：*id\_rsa.pub* - 由[ssh-keygen](http://en.wikipedia.org/wiki/Ssh-keygen)命令生成的默认公钥file名称。 |
| *Private key file* | 当*Authentication method*为"Public key"时的私钥文件名。必填。 | 示例：*id\_rsa* - 默认私钥file名称。 |
| *Password* or<br>*Key passphrase* | 用于认证的密码或<br>**若**私钥使用时的密钥口令。 | 若未使用口令，请保持*Key passphrase*字段为空。<br>另见[ssh检查](/manual/installation/known_issues#ssh检查)关于口令使用的说明。 |
| *Executed script* | 通过SSH远程会话执行的shell命令。 | 执行的shell命令返回值限制为16MB（包括被截断的尾部空格）；[文本数据限制](/manual/config/items/item#文本数据限制)同样适用。<br><br>注意：libssh2库可能将可执行脚本截断至约32kB。<br><br>示例：<br>*date +%s*<br>*systemctl status mysql-server*<br>*ps auxww \ | grep httpd \ | wc -l* |

[comment]: # ({/14e0e4a9-fe23daa8})
