9 SSH checks

Overview

SSH checks are performed as agent-less monitoring. Zabbix agent is not needed for SSH checks.

To perform SSH checks Zabbix server must be initially configured with SSH2 support (libssh or libssh2). See also: Requirements.

Starting with RHEL 8, only libssh is supported. For other distributions, libssh is suggested over libssh2.

Configuration

Passphrase authentication

SSH checks provide two authentication methods - a user/password pair and key-file based.

If you do not intend to use keys, no additional configuration is required, besides linking libssh or libssh2 to Zabbix, if you’re building from source.

Key file authentication

To use key based authentication for SSH items, certain changes to the server configuration are required.

Open the Zabbix server configuration file (zabbix_server.conf) as root and look for the following line:

  1. # SSHKeyLocation=

Uncomment it and set the full path to the folder where the public and private keys will be located:

  1. SSHKeyLocation=/home/zabbix/.ssh

Save the file and restart Zabbix server afterwards.

The path /home/zabbix here is the home directory for the zabbix user account, and .ssh is a directory where by default public and private keys will be generated by an ssh-keygen command inside the home directory.

Usually installation packages of Zabbix server from different OS distributions create the zabbix user account with a home directory elsewhere, for example, /var/lib/zabbix (as for system accounts).

Before generating the keys, you could reallocate the home directory to /home/zabbix, so that it corresponds with the SSHKeyLocation Zabbix server configuration parameter mentioned above.

The following steps can be skipped if zabbix account has been added manually according to the installation section. In such a case the home directory for the zabbix account is most likely already /home/zabbix.

To change the home directory of the zabbix user account, all working processes which are using it have to be stopped:

  1. service zabbix-agent stop
  2. service zabbix-server stop

To change the home directory location with an attempt to move it (if it exists) the following command should be executed:

  1. usermod -m -d /home/zabbix zabbix

It is also possible that a home directory did not exist in the old location, so it should be created at the new location. A safe attempt to do that is:

  1. test -d /home/zabbix || mkdir /home/zabbix

To be sure that all is secure, additional commands could be executed to set permissions to the home directory:

  1. chown zabbix:zabbix /home/zabbix
  2. chmod 700 /home/zabbix

Previously stopped processes can now be started again:

  1. service zabbix-agent start
  2. service zabbix-server start

Now, the steps to generate the public and private keys can be performed with the following commands (for better readability, command prompts are commented out):

  1. sudo -u zabbix ssh-keygen -t rsa
  2. # Generating public/private rsa key pair.
  3. # Enter file in which to save the key (/home/zabbix/.ssh/id_rsa):
  4. /home/zabbix/.ssh/id_rsa
  5. # Enter passphrase (empty for no passphrase):
  6. <Leave empty>
  7. # Enter same passphrase again:
  8. <Leave empty>
  9. # Your identification has been saved in /home/zabbix/.ssh/id_rsa.
  10. # Your public key has been saved in /home/zabbix/.ssh/id_rsa.pub.
  11. # The key fingerprint is:
  12. # 90:af:e4:c7:e3:f0:2e:5a:8d:ab:48:a2:0c:92:30:b9 [email protected]
  13. # The key's randomart image is:
  14. # +--[ RSA 2048]----+
  15. # | |
  16. # | . |
  17. # | o |
  18. # | . o |
  19. # |+ . S |
  20. # |.+ o = |
  21. # |E . * = |
  22. # |=o . ..* . |
  23. # |... oo.o+ |
  24. # +-----------------+

The public and private keys (id_rsa.pub and id_rsa) have been generated by default in the /home/zabbix/.ssh directory, which corresponds to the Zabbix server SSHKeyLocation configuration parameter.

Key types other than “rsa” may be supported by the ssh-keygen tool and SSH servers but they may not be supported by libssh2 used by Zabbix.

Shell configuration form

This step should be performed only once for every host that will be monitored by SSH checks.

By using the following commands, the public key file can be installed on a remote host 10.10.10.10, so that the SSH checks can be performed with a root account (for better readability, command prompts are commented out):

  1. sudo -u zabbix ssh-copy-id [email protected]
  2. # The authenticity of host '10.10.10.10 (10.10.10.10)' can't be established.
  3. # RSA key fingerprint is 38:ba:f2:a4:b5:d9:8f:52:00:09:f7:1f:75:cc:0b:46.
  4. # Are you sure you want to continue connecting (yes/no)?
  5. yes
  6. # Warning: Permanently added '10.10.10.10' (RSA) to the list of known hosts.
  7. # [email protected]'s password:
  8. <Enter root password>
  9. # Now try logging into the machine, with "ssh '[email protected]'",
  10. # and check to make sure that only the key(s) you wanted were added.

Now it is possible to check the SSH login using the default private key (/home/zabbix/.ssh/id_rsa) for the zabbix user account:

  1. sudo -u zabbix ssh [email protected]

If the login is successful, then the configuration part in the shell is finished and the remote SSH session can be closed.

Item configuration

Actual command(s) to be executed must be placed in the Executed script field in the item configuration. Multiple commands can be executed one after another by placing them on a new line. In this case returned values will also be formatted as multilined.

9 SSH checks - 图1

All mandatory input fields are marked with a red asterisk.

The fields that require specific information for SSH items are:

ParameterDescriptionComments
TypeSelect SSH agent here.
KeyUnique (per host) item key in the format ssh.run[unique short description,<ip>,<port>,<encoding>,<ssh options>]unique short description is required and should be unique for each SSH item per host.

Default port is 22, not the port specified in the interface to which this item is assigned.

ssh options (supported since version 6.4.0) allow to pass additional SSH options in the format key1=value1;key2=value2,value3. Multiple values for one key can be passed separated by comma (in this case, the parameter must be quoted); multiple option keys can be passed separated by semicolon. Supported option keys and values depend on the SSH library. Note that “+” sign for appending cipher settings and “!” for disabling specific cipher settings (as in GnuTLS and OpenSSL) are not supported.

Examples:
=> ssh.run[KexAlgorithms,127.0.0.1,,,Ciphers=aes128-ctr]
=> ssh.run[KexAlgorithms,,,,”KexAlgorithms=diffie-hellman-group1-sha1;HostkeyAlgorithms=ssh-rsa,ssh-dss,ecdh-sha2-nistp256”]
Authentication methodOne of the “Password” or “Public key”.
User nameUser name to authenticate on remote host. Required.
Public key fileFile name of public key if Authentication method is “Public key”. Required.Example: id_rsa.pub - default public key file name generated by a command ssh-keygen.
Private key fileFile name of private key if Authentication method is “Public key”. Required.Example: id_rsa - default private key file name.
Password or
Key passphrase
Password to authenticate or
Passphrase if it was used for the private key.
Leave the Key passphrase field empty if passphrase was not used.
See also known issues regarding passphrase usage.
Executed scriptExecuted shell command(s) using SSH remote session.Examples:
date +%s
service mysql-server status
ps auxww | grep httpd | wc -l

libssh2 library may truncate executable scripts to ~32kB.