---
title: Verify Ownership on SSH Server Private *_key Key Files
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > OOTB Rules > Verify Ownership on SSH Server Private
  *_key Key Files
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# Verify Ownership on SSH Server Private *_key Key Files
 
## Description{% #description %}

SSH server private keys, files that match the `/etc/ssh/*_key` glob, must be owned by `root` user.

## Rationale{% #rationale %}

If an unauthorized user obtains the private SSH host key file, the host could be impersonated.

## Remediation{% #remediation %}

### Shell script{% #shell-script %}

The following script can be run on the host to remediate the issue.

```bash
#!/bin/bash

# Remediation is applicable only in certain platforms
if rpm --quiet -q kernel-core; then

newown=""
if id "0" >/dev/null 2>&1; then
  newown="0"
fi

if [[ -z "$newown" ]]; then
  >&2 echo "0 is not a defined user on the system"
else

find -P /etc/ssh/ -maxdepth 1 -type f  ! -user 0 -regextype posix-extended -regex '^.*_key$' -exec chown --no-dereference "$newown" {} \;

fi

else
    >&2 echo 'Remediation is not applicable, nothing was done'
fi
```

### Ansible playbook{% #ansible-playbook %}

The following playbook can be run with Ansible to remediate the issue.

```gdscript3
- name: Gather the package facts
  package_facts:
    manager: auto
  tags:
  - CCE-86118-7
  - NIST-800-171-03.01.02
  - NIST-800-171-03.04.02
  - configure_strategy
  - file_ownership_sshd_private_key
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed

- name: Set the file_ownership_sshd_private_key_newown variable if represented by
    uid
  ansible.builtin.set_fact:
    file_ownership_sshd_private_key_newown: '0'
  when: '"kernel-core" in ansible_facts.packages'
  tags:
  - CCE-86118-7
  - NIST-800-171-03.01.02
  - NIST-800-171-03.04.02
  - configure_strategy
  - file_ownership_sshd_private_key
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed

- name: Find /etc/ssh/ file(s) matching ^.*_key$
  ansible.builtin.command: find -P /etc/ssh/ -maxdepth 1 -type f  ! -user 0 -regextype
    posix-extended -regex "^.*_key$"
  register: files_found
  changed_when: false
  failed_when: false
  check_mode: false
  when: '"kernel-core" in ansible_facts.packages'
  tags:
  - CCE-86118-7
  - NIST-800-171-03.01.02
  - NIST-800-171-03.04.02
  - configure_strategy
  - file_ownership_sshd_private_key
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed

- name: Ensure owner on /etc/ssh/ file(s) matching ^.*_key$
  ansible.builtin.file:
    path: '{{ item }}'
    follow: false
    owner: '{{ file_ownership_sshd_private_key_newown }}'
    state: file
  with_items:
  - '{{ files_found.stdout_lines }}'
  when: '"kernel-core" in ansible_facts.packages'
  tags:
  - CCE-86118-7
  - NIST-800-171-03.01.02
  - NIST-800-171-03.04.02
  - configure_strategy
  - file_ownership_sshd_private_key
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
```

## Warning{% #warning %}

Remediation is not possible at bootable container build time because SSH host keys are generated post-deployment.
