Set up OSSEC agent deployment and centralized management with automated configuration

Intermediate 45 min Apr 15, 2026 25 views
Ubuntu 24.04 Debian 12 AlmaLinux 9 Rocky Linux 9

Deploy and manage OSSEC Host-based Intrusion Detection System (HIDS) across multiple servers with centralized monitoring, automated agent configuration, and real-time security event processing for enterprise-scale infrastructure protection.

Prerequisites

  • Root or sudo access on all target servers
  • Network connectivity between OSSEC server and agents
  • SSH key authentication configured
  • Email server for alert notifications
  • Firewall configured to allow UDP port 1514

What this solves

OSSEC provides real-time security monitoring, log analysis, and intrusion detection across distributed infrastructure. This tutorial establishes a centralized OSSEC server that manages multiple agents, automates deployment processes, and provides unified security monitoring for your entire server fleet.

Step-by-step installation

Update system packages and install dependencies

Update your package manager and install required build dependencies for OSSEC compilation.

sudo apt update && sudo apt upgrade -y
sudo apt install -y build-essential gcc make libevent-dev zlib1g-dev libssl-dev libpcre2-dev wget
sudo dnf update -y
sudo dnf groupinstall -y "Development Tools"
sudo dnf install -y libevent-devel zlib-devel openssl-devel pcre2-devel wget

Download and extract OSSEC source

Download the latest OSSEC release and extract it to prepare for installation.

cd /tmp
wget https://github.com/ossec/ossec-hids/archive/3.7.0.tar.gz
tar -xzf 3.7.0.tar.gz
cd ossec-hids-3.7.0

Install OSSEC server

Run the installation script with server configuration to enable centralized management capabilities.

sudo ./install.sh

During installation, select these options:

  • Installation type: server
  • Installation directory: /var/ossec (default)
  • Email notification: y and provide admin email
  • SMTP server: provide your mail server or localhost
  • Enable firewall response: y
  • Enable system integrity check: y
  • Enable rootkit detection: y
  • Enable active response: y

Configure OSSEC server settings

Customize the main OSSEC configuration for centralized management and security monitoring.



  
    yes
    localhost
    ossec@example.com
    admin@example.com
    10
    127.0.0.1
    192.168.1.0/24
  

  
    rules_config.xml
    pam_rules.xml
    sshd_rules.xml
    telnetd_rules.xml
    syslog_rules.xml
    arpwatch_rules.xml
    symantec-av_rules.xml
    symantec-ws_rules.xml
    pix_rules.xml
    named_rules.xml
    smbd_rules.xml
    vsftpd_rules.xml
    pure-ftpd_rules.xml
    proftpd_rules.xml
    ms_ftpd_rules.xml
    ftpd_rules.xml
    hordeimp_rules.xml
    roundcube_rules.xml
    wordpress_rules.xml
    cimserver_rules.xml
    vpopmail_rules.xml
    vmpop3d_rules.xml
    courier_rules.xml
    web_rules.xml
    web_appsec_rules.xml
    apache_rules.xml
    nginx_rules.xml
    php_rules.xml
    mysql_rules.xml
    postgresql_rules.xml
    ids_rules.xml
    squid_rules.xml
    firewall_rules.xml
    cisco-ios_rules.xml
    netscreenfw_rules.xml
    sonicwall_rules.xml
    postfix_rules.xml
    sendmail_rules.xml
    imapd_rules.xml
    mailscanner_rules.xml
    dovecot_rules.xml
    ms-exchange_rules.xml
    racoon_rules.xml
    vpn_concentrator_rules.xml
    spamd_rules.xml
    msauth_rules.xml
    mcafee_av_rules.xml
    trend-osce_rules.xml
    ms-se_rules.xml
    zeus_rules.xml
    solaris_bsm_rules.xml
    vmware_rules.xml
    ms_dhcp_rules.xml
    asterisk_rules.xml
    ossec_rules.xml
    attack_rules.xml
    local_rules.xml
  

  
    7200
    /etc,/usr/bin,/usr/sbin
    /bin,/sbin
    /etc/mtab
    /etc/hosts.deny
    /etc/mail/statistics
    /etc/random-seed
    /etc/adjtime
    /etc/httpd/logs
  

  
    no
    yes
    yes
    yes
    yes
    yes
    yes
    yes
    yes
    7200
  

  
    secure
    1514
    udp
    192.168.1.0/24
  

  
    1
    7
  

  
    host-deny
    host-deny.sh
    srcip
    yes
  

  
    firewall-drop
    firewall-drop.sh
    srcip
    yes
  

  
    no
    host-deny
    local
    5720
    600
  

  
    no
    firewall-drop
    local
    5720
    600
  

  
    syslog
    /var/log/messages
  

  
    syslog
    /var/log/secure
  

  
    syslog
    /var/log/maillog
  

  
    apache
    /var/log/httpd/access_log
  

  
    apache
    /var/log/httpd/error_log
  

Configure firewall for OSSEC communication

Open the required port for agent-server communication and configure firewall rules.

sudo ufw allow 1514/udp comment "OSSEC agent communication"
sudo ufw reload
sudo firewall-cmd --permanent --add-port=1514/udp --zone=public
sudo firewall-cmd --reload

Start and enable OSSEC server

Start the OSSEC server and configure it to start automatically on boot.

sudo /var/ossec/bin/ossec-control start
sudo systemctl enable ossec

Create agent deployment automation script

Create a script to automate OSSEC agent installation and key management across multiple hosts.

#!/bin/bash

OSSEC Agent Deployment Script

Usage: ./deploy-ossec-agent.sh

set -euo pipefail

Configuration

OSSEC_SERVER="203.0.113.10" # Replace with your OSSEC server IP OSSEC_VERSION="3.7.0" SSH_KEY="/root/.ssh/id_rsa" # Path to SSH private key

Validate arguments

if [ $# -ne 3 ]; then echo "Usage: $0 " echo "Example: $0 web01 203.0.113.20 root" exit 1 fi AGENT_NAME="$1" AGENT_IP="$2" SSH_USER="$3" echo "[INFO] Deploying OSSEC agent to $AGENT_NAME ($AGENT_IP)"

Step 1: Generate agent key on server

echo "[INFO] Generating agent key on OSSEC server" sudo /var/ossec/bin/manage_agents -a "$AGENT_NAME" "$AGENT_IP" "$(openssl rand -hex 32)" -f

Extract the generated key

AGENT_KEY=$(sudo /var/ossec/bin/manage_agents -e "$AGENT_NAME") if [ -z "$AGENT_KEY" ]; then echo "[ERROR] Failed to generate or extract agent key" exit 1 fi echo "[INFO] Generated key for agent $AGENT_NAME"

Step 2: Deploy agent installation script

cat > /tmp/install-ossec-agent.sh << 'AGENT_SCRIPT' #!/bin/bash set -euo pipefail

Detect distribution

if command -v apt-get >/dev/null 2>&1; then DISTRO="debian" sudo apt update sudo apt install -y build-essential gcc make libevent-dev zlib1g-dev libssl-dev libpcre2-dev wget elif command -v dnf >/dev/null 2>&1; then DISTRO="rhel" sudo dnf update -y sudo dnf groupinstall -y "Development Tools" sudo dnf install -y libevent-devel zlib-devel openssl-devel pcre2-devel wget else echo "[ERROR] Unsupported distribution" exit 1 fi

Download and compile OSSEC agent

cd /tmp wget -q https://github.com/ossec/ossec-hids/archive/3.7.0.tar.gz tar -xzf 3.7.0.tar.gz cd ossec-hids-3.7.0

Automated installation responses

cat > /tmp/ossec-answers << EOF en agent /var/ossec SERVER_IP_PLACEHOLDER y y y y EOF

Install OSSEC agent

sudo ./install.sh < /tmp/ossec-answers

Import agent key

echo "KEY_PLACEHOLDER" | sudo /var/ossec/bin/manage_agents -i

Configure agent

sudo sed -i "s|.*|SERVER_IP_PLACEHOLDER|" /var/ossec/etc/ossec.conf

Start OSSEC agent

sudo /var/ossec/bin/ossec-control start

Enable on boot

if [ "$DISTRO" = "debian" ]; then sudo systemctl enable ossec || true else sudo chkconfig ossec on || true fi echo "[INFO] OSSEC agent installation completed" AGENT_SCRIPT

Replace placeholders in script

sed -i "s|SERVER_IP_PLACEHOLDER|$OSSEC_SERVER|g" /tmp/install-ossec-agent.sh sed -i "s|KEY_PLACEHOLDER|$AGENT_KEY|g" /tmp/install-ossec-agent.sh

Step 3: Copy and execute installation script on remote host

echo "[INFO] Copying installation script to $AGENT_IP" scp -i "$SSH_KEY" -o StrictHostKeyChecking=no /tmp/install-ossec-agent.sh "$SSH_USER@$AGENT_IP:/tmp/" echo "[INFO] Executing installation on remote host" ssh -i "$SSH_KEY" -o StrictHostKeyChecking=no "$SSH_USER@$AGENT_IP" "chmod +x /tmp/install-ossec-agent.sh && /tmp/install-ossec-agent.sh"

Step 4: Verify agent connection

echo "[INFO] Waiting for agent connection..." sleep 10 if sudo /var/ossec/bin/list_agents | grep -q "$AGENT_NAME"; then echo "[SUCCESS] Agent $AGENT_NAME deployed and connected successfully" # Restart OSSEC server to recognize new agent sudo /var/ossec/bin/ossec-control restart echo "[INFO] OSSEC server restarted to recognize new agent" else echo "[WARNING] Agent may not be connected yet. Check logs with: sudo tail -f /var/ossec/logs/ossec.log" fi

Cleanup

rm -f /tmp/install-ossec-agent.sh echo "[INFO] Deployment completed for agent $AGENT_NAME"

Make deployment script executable and configure SSH

Set proper permissions for the deployment script and configure SSH key authentication.

sudo chmod 755 /usr/local/bin/deploy-ossec-agent.sh

Generate SSH key if not exists

if [ ! -f /root/.ssh/id_rsa ]; then sudo ssh-keygen -t rsa -b 4096 -f /root/.ssh/id_rsa -N "" fi

Display public key for distribution to target hosts

echo "Copy this public key to target servers:" sudo cat /root/.ssh/id_rsa.pub
Note: Copy the displayed SSH public key to the ~/.ssh/authorized_keys file on each target server you want to deploy agents to.

Create centralized management script

Create a script to manage multiple agents from the central server.

#!/bin/bash

OSSEC Agent Management Script

Usage: ./manage-ossec-agents.sh [agent_name]

Actions: list, status, restart-all, remove

set -euo pipefail ACTION="${1:-}" AGENT_NAME="${2:-}" case "$ACTION" in "list") echo "=== Active OSSEC Agents ===" sudo /var/ossec/bin/list_agents ;; "status") echo "=== OSSEC Server Status ===" sudo /var/ossec/bin/ossec-control status echo "" echo "=== Recent Alerts ===" sudo tail -n 20 /var/ossec/logs/alerts/alerts.log ;; "restart-all") echo "[INFO] Restarting OSSEC server" sudo /var/ossec/bin/ossec-control restart echo "[INFO] OSSEC server restarted" ;; "remove") if [ -z "$AGENT_NAME" ]; then echo "Usage: $0 remove " exit 1 fi echo "[INFO] Removing agent: $AGENT_NAME" echo "$AGENT_NAME" | sudo /var/ossec/bin/manage_agents -r sudo /var/ossec/bin/ossec-control restart echo "[INFO] Agent $AGENT_NAME removed and server restarted" ;; "logs") echo "=== OSSEC Server Logs ===" sudo tail -f /var/ossec/logs/ossec.log ;; "stats") echo "=== OSSEC Statistics ===" echo "Active agents: $(sudo /var/ossec/bin/list_agents | grep -c 'is available')" echo "Total alerts today: $(sudo grep $(date +"%Y %b %d") /var/ossec/logs/alerts/alerts.log | wc -l)" echo "Server uptime: $(sudo /var/ossec/bin/ossec-control status | grep 'is running' | wc -l) processes running" ;; *) echo "OSSEC Agent Management" echo "Usage: $0 [agent_name]" echo "" echo "Actions:" echo " list - List all registered agents" echo " status - Show server status and recent alerts" echo " restart-all - Restart OSSEC server" echo " remove - Remove an agent" echo " logs - Follow server logs (Ctrl+C to exit)" echo " stats - Show server statistics" echo "" echo "Examples:" echo " $0 list" echo " $0 status" echo " $0 remove web01" ;; esac

Make management script executable

Set proper permissions for the management script.

sudo chmod 755 /usr/local/bin/manage-ossec-agents.sh

Deploy agents to target servers

Use the deployment script to install agents on your target servers.

# Deploy to a web server
sudo /usr/local/bin/deploy-ossec-agent.sh web01 203.0.113.20 root

Deploy to a database server

sudo /usr/local/bin/deploy-ossec-agent.sh db01 203.0.113.21 root

Deploy to an application server

sudo /usr/local/bin/deploy-ossec-agent.sh app01 203.0.113.22 ubuntu

Configure log monitoring and alerting

Enhance the OSSEC configuration to monitor additional log sources and configure custom alerting rules.




  
  
    syslog,sshd,
    Failed password
    5
    300
    SSH brute force attack detected.
  

  
  
    web,apache,
     404 
    20
    120
    High number of 404 errors detected.
  

  
  
    mysql,postgresql,
    connection refused|Access denied
    10
    600
    Database connection issues detected.
  

  
  
    syslog,
    ERROR|CRITICAL|FATAL
    Application error detected in logs.
  

Configure email alerting

Set up email notifications for critical security events.

  
    security@example.com
    7
    5720,100001,100002
    full
  

  
    admin@example.com
    10
    sms
  

Add this configuration within the main ossec_config block and restart OSSEC.

sudo /var/ossec/bin/ossec-control restart

Verify your setup

Confirm that your OSSEC server is running and agents are connected properly.

# Check OSSEC server status
sudo /var/ossec/bin/ossec-control status

List connected agents

sudo /usr/local/bin/manage-ossec-agents.sh list

View recent alerts

sudo tail -n 20 /var/ossec/logs/alerts/alerts.log

Check server statistics

sudo /usr/local/bin/manage-ossec-agents.sh stats

Test agent connectivity

sudo /var/ossec/bin/agent_control -lc

Monitor real-time alerts

sudo tail -f /var/ossec/logs/alerts/alerts.log

Configure advanced monitoring rules

Create custom monitoring profiles

Configure specific monitoring rules for different server types.

# Web server specific monitoring

  
    apache
    /var/log/apache2/access.log
  
  
    apache
    /var/log/apache2/error.log
  
  
    apache
    /var/log/nginx/access.log
  
  
    apache
    /var/log/nginx/error.log
  

Configure database server monitoring

Set up specialized monitoring for database servers.

# Database server specific monitoring

  
    mysql_log
    /var/log/mysql/error.log
  
  
    postgresql_log
    /var/log/postgresql/postgresql-*.log
  
  
    /etc/mysql,/etc/postgresql
    3600
  

Apply configurations to specific agents

Assign monitoring profiles to specific agents based on their roles.

# Copy web server profile to specific agent
sudo cp /var/ossec/etc/shared/web-servers.conf /var/ossec/etc/shared/web01.conf

Copy database profile to specific agent

sudo cp /var/ossec/etc/shared/db-servers.conf /var/ossec/etc/shared/db01.conf

Restart OSSEC to apply changes

sudo /var/ossec/bin/ossec-control restart

Common issues

SymptomCauseFix
Agent not connectingFirewall blocking port 1514sudo ufw allow 1514/udp or sudo firewall-cmd --add-port=1514/udp --permanent
Permission denied errorsIncorrect OSSEC file ownershipsudo chown -R ossec:ossec /var/ossec
No email alertsSMTP server not configuredConfigure valid SMTP settings in /var/ossec/etc/ossec.conf
High CPU usageToo frequent integrity checksIncrease syscheck frequency to 21600 (6 hours)
Agent key import failsKey format corruptionRegenerate agent key with manage_agents
Log parsing errorsIncorrect log format specifiedVerify log format matches actual log structure
Active response not workingScripts not executablesudo chmod +x /var/ossec/active-response/bin/*.sh
Database full errorsAlert log rotation not configuredConfigure log rotation in /etc/logrotate.d/ossec

Next steps

Automated install script

Run this to automate the entire setup

Need help?

Don't want to manage this yourself?

We handle infrastructure security hardening for businesses that depend on uptime. From initial setup to ongoing operations.