---
title: Require Re-Authentication When Using the sudo Command
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > OOTB Rules > Require Re-Authentication When Using
  the sudo Command
---

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

# Require Re-Authentication When Using the sudo Command
 
## Description{% #description %}

The sudo `timestamp_timeout` tag sets the number of minutes after which sudo asks for the password again. Make sure that every `timestamp_timeout` setting in the `/etc/sudoers` configuration file and in the sudo configuration snippets in `/etc/sudoers.d/` is between 0 and 15, and that at most one `Defaults` entry for all users sets it. Without such an entry, sudo uses the default it was built with: 5 minutes, and 15 minutes on Debian and Ubuntu. This default must not exceed 15 either. sudo skips the files of `/etc/sudoers.d/` whose names contain a `.` or end in `~`, and so does the check. If the value is set to an integer less than 0, the user's time stamp will not expire and the user will not have to re-authenticate for privileged actions until the user's session is terminated.

## Rationale{% #rationale %}

Without re-authentication, users may access resources or perform tasks for which they do not have authorization.

When operating systems provide the capability to escalate a functional capability, it is critical that the user re-authenticate.

## 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 dpkg-query --show --showformat='${db:Status-Status}' 'linux-base' 2>/dev/null | grep -q '^installed$' && { dpkg-query --show --showformat='${db:Status-Status}' 'sudo' 2>/dev/null | grep -q '^installed$'; }; then

var_sudo_timestamp_timeout='15'




for f in /etc/sudoers.d/*; do
    [ -f "$f" ] || continue
    grep -qP '^[\s]*Defaults.*timestamp_timeout[\s]*=' "$f" || continue
    # A Defaults entry for some users, hosts, commands or runas users gets
    # the required value and keeps its other options
    sed -Ei "/^[[:blank:]]*Defaults[^[:blank:]]/ s/(timestamp_timeout[[:blank:]]*=)[[:blank:]]*\"?[-+]?[0-9.]+\"?/\1${var_sudo_timestamp_timeout}/" "$f"
    # An entry for all users loses timestamp_timeout, which /etc/sudoers sets
    # once below, and keeps its other options
    sed -Ei -e '/^[[:blank:]]*Defaults[[:blank:]]/ s/[[:blank:]]*,[[:blank:]]*timestamp_timeout[[:blank:]]*=[[:blank:]]*[^,#[:blank:]]*//g' \
        -e '/^[[:blank:]]*Defaults[[:blank:]]/ s/^([[:blank:]]*Defaults[[:blank:]]+)timestamp_timeout[[:blank:]]*=[[:blank:]]*[^,#[:blank:]]*[[:blank:]]*,[[:blank:]]*/\1/' \
        -e '/^[[:blank:]]*Defaults[[:blank:]]+timestamp_timeout[[:blank:]]*=[^,#]*(#.*)?$/d' "$f"
done

if /usr/sbin/visudo -qcf /etc/sudoers; then
    cp /etc/sudoers /etc/sudoers.bak
    chmod 0440 /etc/sudoers.bak
    # Set every timestamp_timeout value, signed, fractional or quoted, to the required one
    sed -Ei "s/(^[[:blank:]]*Defaults.*timestamp_timeout[[:blank:]]*=)[[:blank:]]*\"?[-+]?[0-9.]+\"?/\1${var_sudo_timestamp_timeout}/" /etc/sudoers
    # Keep one Defaults entry for all users: take timestamp_timeout out of
    # every entry, drop the entries left empty, and add it back below
    if [ "$(grep -cP '^[\s]*Defaults[\s]+(.*[\s,])?timestamp_timeout[\s]*=' /etc/sudoers)" -gt 1 ]; then
        sed -Ei -e '/^[[:blank:]]*Defaults[[:blank:]]/ s/[[:blank:]]*,[[:blank:]]*timestamp_timeout[[:blank:]]*=[[:blank:]]*[^,#[:blank:]]*//g' \
            -e '/^[[:blank:]]*Defaults[[:blank:]]/ s/^([[:blank:]]*Defaults[[:blank:]]+)timestamp_timeout[[:blank:]]*=[[:blank:]]*[^,#[:blank:]]*[[:blank:]]*,[[:blank:]]*/\1/' \
            -e '/^[[:blank:]]*Defaults[[:blank:]]+timestamp_timeout[[:blank:]]*=[^,#]*(#.*)?$/d' /etc/sudoers
    fi
    if ! grep -qP '^[\s]*Defaults[\s]+(.*[\s,])?timestamp_timeout[\s]*=' /etc/sudoers; then
        # sudoers file doesn't define Option timestamp_timeout for all users
        echo "Defaults timestamp_timeout=${var_sudo_timestamp_timeout}" >> /etc/sudoers
    fi

    # Check validity of sudoers and cleanup bak
    if /usr/sbin/visudo -qcf /etc/sudoers; then
        rm -f /etc/sudoers.bak
    else
        echo "Fail to validate remediated /etc/sudoers, reverting to original file."
        mv /etc/sudoers.bak /etc/sudoers
        false
    fi
else
    echo "Skipping remediation, /etc/sudoers failed to validate"
    false
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:
  - NIST-800-53-IA-11
  - PCI-DSSv4-2.2
  - PCI-DSSv4-2.2.6
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - restrict_strategy
  - sudo_require_reauthentication
- name: XCCDF Value var_sudo_timestamp_timeout # promote to variable
  set_fact:
    var_sudo_timestamp_timeout: !!str 15
  tags:
    - always

- name: Require Re-Authentication When Using the sudo Command - Find /etc/sudoers.d/*
    files containing 'Defaults timestamp_timeout'
  ansible.builtin.find:
    path: /etc/sudoers.d
    patterns: '*'
    contains: ^[\s]*Defaults\S*\s.*\btimestamp_timeout[\s]*=.*
  register: sudoers_d_defaults_timestamp_timeout
  when:
  - '"linux-base" in ansible_facts.packages'
  - '"sudo" in ansible_facts.packages'
  tags:
  - NIST-800-53-IA-11
  - PCI-DSSv4-2.2
  - PCI-DSSv4-2.2.6
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - restrict_strategy
  - sudo_require_reauthentication

- name: Require Re-Authentication When Using the sudo Command - Set timestamp_timeout
    to the required value in the Defaults entries for some users of /etc/sudoers.d/*
    files
  ansible.builtin.replace:
    path: '{{ item.path }}'
    regexp: ^([\s]*Defaults[^\s]\S*\s(?:.*[\s,])?timestamp_timeout[\s]*=)[\s]*"?[-+]?[\d.]+"?
    replace: \g<1>{{ var_sudo_timestamp_timeout }}
  with_items: '{{ sudoers_d_defaults_timestamp_timeout.files }}'
  when:
  - '"linux-base" in ansible_facts.packages'
  - '"sudo" in ansible_facts.packages'
  tags:
  - NIST-800-53-IA-11
  - PCI-DSSv4-2.2
  - PCI-DSSv4-2.2.6
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - restrict_strategy
  - sudo_require_reauthentication

- name: Require Re-Authentication When Using the sudo Command - Remove timestamp_timeout
    from the Defaults entries for all users of /etc/sudoers.d/* files where it follows
    other options
  ansible.builtin.replace:
    path: '{{ item.path }}'
    regexp: ^([ \t]*Defaults[ \t]+[^#\n]*?)[ \t]*,[ \t]*timestamp_timeout[ \t]*=[
      \t]*[^,#\s]*
    replace: \1
  with_items: '{{ sudoers_d_defaults_timestamp_timeout.files }}'
  when:
  - '"linux-base" in ansible_facts.packages'
  - '"sudo" in ansible_facts.packages'
  tags:
  - NIST-800-53-IA-11
  - PCI-DSSv4-2.2
  - PCI-DSSv4-2.2.6
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - restrict_strategy
  - sudo_require_reauthentication

- name: Require Re-Authentication When Using the sudo Command - Remove timestamp_timeout
    from the Defaults entries for all users of /etc/sudoers.d/* files where other
    options follow it
  ansible.builtin.replace:
    path: '{{ item.path }}'
    regexp: ^([ \t]*Defaults[ \t]+)timestamp_timeout[ \t]*=[ \t]*[^,#\s]*[ \t]*,[
      \t]*
    replace: \1
  with_items: '{{ sudoers_d_defaults_timestamp_timeout.files }}'
  when:
  - '"linux-base" in ansible_facts.packages'
  - '"sudo" in ansible_facts.packages'
  tags:
  - NIST-800-53-IA-11
  - PCI-DSSv4-2.2
  - PCI-DSSv4-2.2.6
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - restrict_strategy
  - sudo_require_reauthentication

- name: Require Re-Authentication When Using the sudo Command - Drop the Defaults
    lines of /etc/sudoers.d/* files that set only timestamp_timeout for all users
  ansible.builtin.lineinfile:
    path: '{{ item.path }}'
    regexp: ^[\s]*Defaults[\s]+timestamp_timeout[\s]*=[^,#]*(#.*)?$
    state: absent
  with_items: '{{ sudoers_d_defaults_timestamp_timeout.files }}'
  when:
  - '"linux-base" in ansible_facts.packages'
  - '"sudo" in ansible_facts.packages'
  tags:
  - NIST-800-53-IA-11
  - PCI-DSSv4-2.2
  - PCI-DSSv4-2.2.6
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - restrict_strategy
  - sudo_require_reauthentication

- name: Require Re-Authentication When Using the sudo Command - Set every timestamp_timeout
    value in /etc/sudoers to the required one
  ansible.builtin.replace:
    path: /etc/sudoers
    regexp: ^([\s]*Defaults\S*\s(?:.*[\s,])?timestamp_timeout[\s]*=)[\s]*"?[-+]?[\d.]+"?
    replace: \g<1>{{ var_sudo_timestamp_timeout }}
    validate: /usr/sbin/visudo -cf %s
  when:
  - '"linux-base" in ansible_facts.packages'
  - '"sudo" in ansible_facts.packages'
  tags:
  - NIST-800-53-IA-11
  - PCI-DSSv4-2.2
  - PCI-DSSv4-2.2.6
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - restrict_strategy
  - sudo_require_reauthentication

- name: Require Re-Authentication When Using the sudo Command - Count the Defaults
    entries for all users that set timestamp_timeout
  ansible.builtin.command: grep -cP '^[\s]*Defaults[\s]+(.*[\s,])?timestamp_timeout[\s]*='
    /etc/sudoers
  register: sudoers_timestamp_timeout_entries
  changed_when: false
  failed_when: false
  check_mode: false
  when:
  - '"linux-base" in ansible_facts.packages'
  - '"sudo" in ansible_facts.packages'
  tags:
  - NIST-800-53-IA-11
  - PCI-DSSv4-2.2
  - PCI-DSSv4-2.2.6
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - restrict_strategy
  - sudo_require_reauthentication

- name: 'Require Re-Authentication When Using the sudo Command - Keep one Defaults
    entry for all users: remove timestamp_timeout from every entry'
  block:

  - name: Require Re-Authentication When Using the sudo Command - Remove timestamp_timeout
      where it follows other options
    ansible.builtin.replace:
      path: /etc/sudoers
      regexp: ^([ \t]*Defaults[ \t]+[^#\n]*?)[ \t]*,[ \t]*timestamp_timeout[ \t]*=[
        \t]*[^,#\s]*
      replace: \1
      validate: /usr/sbin/visudo -cf %s

  - name: Require Re-Authentication When Using the sudo Command - Remove timestamp_timeout
      where other options follow it
    ansible.builtin.replace:
      path: /etc/sudoers
      regexp: ^([ \t]*Defaults[ \t]+)timestamp_timeout[ \t]*=[ \t]*[^,#\s]*[ \t]*,[
        \t]*
      replace: \1
      validate: /usr/sbin/visudo -cf %s

  - name: Require Re-Authentication When Using the sudo Command - Drop the lines that
      set only timestamp_timeout
    ansible.builtin.lineinfile:
      path: /etc/sudoers
      regexp: ^[\s]*Defaults[\s]+timestamp_timeout[\s]*=[^,#]*(#.*)?$
      state: absent
      validate: /usr/sbin/visudo -cf %s
  when:
  - '"linux-base" in ansible_facts.packages'
  - '"sudo" in ansible_facts.packages'
  - sudoers_timestamp_timeout_entries.stdout | int > 1
  tags:
  - NIST-800-53-IA-11
  - PCI-DSSv4-2.2
  - PCI-DSSv4-2.2.6
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - restrict_strategy
  - sudo_require_reauthentication

- name: Require Re-Authentication When Using the sudo Command - Count the Defaults
    entries for all users that set timestamp_timeout again
  ansible.builtin.command: grep -cP '^[\s]*Defaults[\s]+(.*[\s,])?timestamp_timeout[\s]*='
    /etc/sudoers
  register: sudoers_timestamp_timeout_entries_left
  changed_when: false
  failed_when: false
  check_mode: false
  when:
  - '"linux-base" in ansible_facts.packages'
  - '"sudo" in ansible_facts.packages'
  tags:
  - NIST-800-53-IA-11
  - PCI-DSSv4-2.2
  - PCI-DSSv4-2.2.6
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - restrict_strategy
  - sudo_require_reauthentication

- name: Require Re-Authentication When Using the sudo Command - Enable timestamp_timeout
    option with correct value in /etc/sudoers
  ansible.builtin.lineinfile:
    path: /etc/sudoers
    line: Defaults timestamp_timeout={{ var_sudo_timestamp_timeout }}
    validate: /usr/sbin/visudo -cf %s
  when:
  - '"linux-base" in ansible_facts.packages'
  - '"sudo" in ansible_facts.packages'
  - sudoers_timestamp_timeout_entries_left.stdout | int == 0
  tags:
  - NIST-800-53-IA-11
  - PCI-DSSv4-2.2
  - PCI-DSSv4-2.2.6
  - low_complexity
  - low_disruption
  - medium_severity
  - no_reboot_needed
  - restrict_strategy
  - sudo_require_reauthentication
```
