Skip to content

Instantly share code, notes, and snippets.

@massiws
Last active July 23, 2026 14:52
Show Gist options
  • Select an option

  • Save massiws/f93f6d622cc4f06ad6236cd5ae5e8bec to your computer and use it in GitHub Desktop.

Select an option

Save massiws/f93f6d622cc4f06ad6236cd5ae5e8bec to your computer and use it in GitHub Desktop.
Configure SSH integration for the Datadog agent on servers managed by Laravel Forge
#!/bin/bash
# Configure SSH integration for the Datadog agent on servers managed by Laravel Forge.
#
# -------------------------------
# Usage:
# 1. Configure Datadog SSH integration file in /etc/datadog-agent/conf.d/ssh_check.d/conf.yaml:
#
# instances:
# - add_missing_keys: true
# host: localhost
# private_key_file: /opt/datadog-agent/.ssh/id_rsa
# username: forge
#
# 2. Download and run this script:
#
# chmod +x setup_datadog_ssh.sh
# sudo ./setup_datadog_ssh.sh
# -------------------------------
set -e # Exit on error
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${YELLOW}═══════════════════════════════════════════════════════════${NC}"
echo -e "${YELLOW}SSH Configuration for Datadog Agent${NC}"
echo -e "${YELLOW}═══════════════════════════════════════════════════════════${NC}\n"
# Variables
DD_AGENT_HOME="/opt/datadog-agent"
DD_AGENT_SSH="$DD_AGENT_HOME/.ssh"
FORGE_SSH="/home/forge/.ssh"
PRIVATE_KEY="$DD_AGENT_SSH/id_rsa"
AUTHORIZED_KEYS="$FORGE_SSH/authorized_keys"
# Function to execute commands with feedback
run_command() {
local description=$1
local command=$2
echo -e "${YELLOW}${NC} $description"
if eval "$command"; then
echo -e "${GREEN}${NC} Complete\n"
else
echo -e "${RED}${NC} Error during execution\n"
exit 1
fi
}
# Step 1: Import private key
run_command \
"Creating SSH directory for Datadog agent" \
"sudo mkdir -p $DD_AGENT_SSH"
run_command \
"Copying private key from Forge" \
"sudo cp $FORGE_SSH/id_rsa $PRIVATE_KEY"
# Step 2: Convert key to PEM format
run_command \
"Converting key to PEM format (required by Paramiko)" \
"sudo ssh-keygen -p -m PEM -f $PRIVATE_KEY -N ''"
# Step 3: Create known_hosts file
run_command \
"Creating known_hosts file" \
"sudo -u dd-agent ssh-keyscan localhost | sudo -u dd-agent tee $DD_AGENT_SSH/known_hosts > /dev/null"
# Step 4: Add public key to authorized_keys
run_command \
"Adding public key to authorized_keys" \
"sudo ssh-keygen -y -f $PRIVATE_KEY | sudo tee -a $AUTHORIZED_KEYS > /dev/null"
# Step 5: Configure permissions
run_command \
"Configuring SSH directory ownership" \
"sudo chown -R dd-agent:dd-agent $DD_AGENT_SSH"
run_command \
"Setting SSH directory permissions (700)" \
"sudo chmod 700 $DD_AGENT_SSH"
run_command \
"Setting private key permissions (600)" \
"sudo chmod 600 $PRIVATE_KEY"
# Summary
echo -e "${YELLOW}═══════════════════════════════════════════════════════════${NC}"
echo -e "${GREEN}✓ SSH configuration completed successfully!${NC}"
echo -e "${YELLOW}═══════════════════════════════════════════════════════════${NC}\n"
echo "Summary of executed operations:"
echo " • SSH directory created: $DD_AGENT_SSH"
echo " • Private key: $PRIVATE_KEY"
echo " • Key converted to PEM format"
echo " • known_hosts configured"
echo " • Public key added to authorized_keys"
echo " • Correct permissions applied"
echo ""
echo -e "${GREEN}Datadog Agent can now authenticate via SSH.${NC}\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment