Skip to content

Ansible Role linuxfabrik.lfops.sshd

This role deploys /etc/ssh/sshd_config for OpenSSH (the standard SSH server on Linux). It exposes the most commonly tuned options as variables (port, address family, password / GSSAPI / root login, log level, forwarding, session and connection limits, sftp subsystem) plus a sshd__raw escape hatch for Match blocks etc.

Note that the role templates the full sshd_config, since not all options can be overwritten in /etc/ssh/sshd_config.d/ (e.g. Subsystem 'sftp' already defined). The only exception is 00-linuxfabrik.conf on RHEL 9 and newer, which sets GSSAPIAuthentication and X11Forwarding, as the 50-redhat.conf of the package would win otherwise.

The option descriptions below are condensed from sshd_config(5).

Available since LFOps 2.0.0.

How the Role Behaves

The role ships hardened defaults for several options that differ from the OpenSSH (or distribution) defaults: root may not log in, X11Forwarding, agent forwarding and TCP keepalives are off, MaxAuthTries, MaxStartups and ClientAliveCountMax are stricter, an unresponsive client is disconnected after ClientAliveInterval × ClientAliveCountMax seconds (30 by default), and LogLevel is VERBOSE (which logs the key fingerprint used for each login). These follow the CIS Benchmarks. Each is overridable via the variables below.

PermitRootLogin defaults to no. Before it deploys sshd_config, the role checks which user Ansible logs in as over SSH. If that is root and sshd__permit_root_login does not admit root logins with a key, the run aborts before anything is changed, since the reload would lock Ansible out of the host. Log in as an unprivileged user with sudo (see the login role), or set sshd__permit_root_login: 'prohibit-password' for hosts that Ansible has to reach as root.

MaxAuthTries defaults to 3. Every public key an SSH agent offers counts as one attempt, so a client with more than three keys loaded in its agent may be rejected before the matching key is tried. In that case use IdentitiesOnly yes / an explicit IdentityFile on the client, or raise sshd__max_auth_tries.

MaxStartups defaults to 10:30:60. It limits the connections that have not authenticated yet, across all clients: from 10 such connections on, sshd refuses every new one with a probability of 30%, rising to 100% at 60. A host that many clients log in to at the same moment, such as a Git server with CI jobs, reaches this limit with legitimate logins, which then fail before authentication (Connection closed by remote host or Not allowed at this time on the client, drop connection ... past MaxStartups in the log of sshd). Raise sshd__max_startups__host_var / sshd__max_startups__group_var on such hosts. sshd uses the first value it reads, so a MaxStartups in sshd__raw has no effect.

On Debian and Ubuntu the SSH daemon unit is ssh.service and OpenSSH ships socket activation (ssh.socket) enabled by default. The role manages the daemon through its service unit like on Red Hat, so on these systems it disables and stops ssh.socket and enables ssh.service instead. Red Hat-family systems for which no version-specific template exists (in particular Fedora) fall back to the generic RedHat template.

Dependent Roles

Any LFOps playbook that installs this role runs these for you. Optional ones can be disabled via the playbook's skip variables.

Tags

sshd

  • Configures sshd.
  • Triggers: sshd: sshd -t; reload sshd.

sshd:state

  • Manages the state of the sshd systemd service.
  • Triggers: none.

Optional Role Variables

sshd__address_family

  • Specifies which address family should be used. Possible options: any, inet (use IPv4 only) or inet6 (use IPv6 only).
  • Type: String.
  • Default: 'any'

sshd__allow_agent_forwarding

  • Specifies whether ssh-agent(1) forwarding is permitted. OpenSSH defaults to true; this role disables it. For the finer-grained variants use sshd__raw.
  • Type: Bool.
  • Default: false

sshd__allow_tcp_forwarding

  • Specifies whether TCP forwarding is permitted. Rendered as yes/no; for the local / remote / all variants use sshd__raw.
  • Type: Bool.
  • Default: true

sshd__client_alive_count_max

  • Number of client alive messages sshd may send without receiving any back from the client before it disconnects the session. Unlike TCPKeepAlive, these messages go through the encrypted channel and cannot be spoofed. Only takes effect together with a non-zero sshd__client_alive_interval.
  • Type: Number.
  • Default: 2 (OpenSSH default: 3)

sshd__client_alive_interval

  • Seconds of inactivity after which sshd asks the client for a sign of life through the encrypted channel. 0 disables these messages, and with them the disconnect after sshd__client_alive_count_max unanswered ones.
  • Type: Number.
  • Default: 15
  • Deviates from the upstream default 0: without it, a session whose client vanished stays open until TCP gives up, which can take hours. The CIS Benchmarks require a non-zero value.

sshd__gssapi_authentication

  • Specifies whether user authentication based on GSSAPI is allowed.
  • Type: Bool.
  • Default: true

sshd__log_level__group_var / sshd__log_level__host_var

  • Verbosity level used when logging messages from sshd. Possible values: QUIET, FATAL, ERROR, INFO, VERBOSE, DEBUG, DEBUG1, DEBUG2, DEBUG3. VERBOSE additionally logs the key fingerprint used for each login. DEBUG* levels violate user privacy and are not recommended.
  • Type: String.
  • Default: 'VERBOSE' (OpenSSH default: INFO)

sshd__max_auth_tries

  • Maximum number of authentication attempts permitted per connection. Once the number of failures reaches half this value, additional failures are logged. Note that each key an SSH agent offers counts as one attempt (see "How the Role Behaves").
  • Type: Number.
  • Default: 3 (OpenSSH default: 6)

sshd__max_sessions

  • Maximum number of open shell, login or subsystem (e.g. sftp) sessions permitted per network connection. 1 disables session multiplexing; 0 prevents shell, login and subsystem sessions while still permitting forwarding.
  • Type: Number.
  • Default: 10

sshd__max_startups__host_var / sshd__max_startups__group_var

  • Maximum number of concurrent connections that have not authenticated yet, as start:rate:full: from start such connections on, sshd refuses a new one with a probability of rate percent, which rises linearly to 100% at full. A single number refuses every connection above it. See "How the Role Behaves".
  • Type: String.
  • Default: '10:30:60'
  • Deviates from the upstream default 10:30:100: fewer pending logins can keep sshd busy before it turns new ones away. The CIS Benchmarks require 10:30:60 or stricter.

sshd__password_authentication

  • Specifies whether password authentication is allowed.
  • Type: Bool.
  • Default: false

sshd__permit_root_login

  • Specifies whether root can log in using ssh. Possible options: yes, prohibit-password, forced-commands-only, no. The role refuses to set a value that locks out the user Ansible logs in as (see "How the Role Behaves").
  • Type: String.
  • Default: 'no'
  • Deviates from the upstream default prohibit-password (some distributions ship yes): administrators log in with a personal account and use sudo, so every privileged action can be traced to a person. The CIS Benchmarks require no.

sshd__port

  • Which port the sshd server should use.
  • Type: Number.
  • Default: 22

sshd__raw

  • Raw (user-defined) SSH-Config. Will be placed at the end of the /etc/ssh/sshd_config file. Useful for Match directives.
  • Type: String.
  • Default: unset

sshd__service_enabled

  • Enables or disables the sshd service, analogous to systemctl enable/disable.
  • Type: Bool.
  • Default: true

sshd__service_state

  • Changes the state of the sshd service, analogous to systemctl start/stop/restart/reload. Possible options: started, stopped, restarted, reloaded.
  • Type: String.
  • Default: 'started'

sshd__sftp_subsystem

  • Which command should be used for the sftp subsystem.
  • Type: String.
  • Default: RHEL: '/usr/libexec/openssh/sftp-server', Debian: '/usr/lib/openssh/sftp-server'

sshd__tcp_keep_alive

  • Specifies whether the system sends TCP keepalive messages to the other side. The TCP keepalive is spoofable; sshd__client_alive_count_max (through the encrypted channel) is the non-spoofable alternative. Disabling it avoids dropping sessions on temporary route outages.
  • Type: Bool.
  • Default: false (OpenSSH default: yes)

sshd__use_dns

  • Specifies whether sshd should look up the remote hostname, and to check that the resolved host name for the remote IP address maps back to the very same IP address.
  • Type: Bool.
  • Default: false

sshd__x11_forwarding

  • Specifies whether X11 forwarding is permitted. OpenSSH defaults to no, but the stock RHEL and Debian sshd_config enable it; this role disables it again.
  • Type: Bool.
  • Default: false

Example:

# optional
sshd__address_family: 'inet'
sshd__allow_agent_forwarding: false
sshd__allow_tcp_forwarding: true
sshd__client_alive_count_max: 2
sshd__client_alive_interval: 15
sshd__gssapi_authentication: false
sshd__log_level__host_var: 'VERBOSE'
sshd__max_auth_tries: 3
sshd__max_sessions: 10
sshd__max_startups__host_var: '100:30:200'
sshd__password_authentication: false
sshd__permit_root_login: 'prohibit-password'
sshd__port: 22
sshd__raw: |-
  Match Group sftpusers
    ChrootDirectory /data
    DisableForwarding yes
    ForceCommand internal-sftp
sshd__service_enabled: true
sshd__service_state: 'started'
sshd__sftp_subsystem: '/usr/libexec/openssh/sftp-server'
sshd__tcp_keep_alive: false
sshd__use_dns: false
sshd__x11_forwarding: false

License

The Unlicense

Author Information

Linuxfabrik GmbH, Zurich