When iptables and UFW Are No Longer Enough
The moment you expose ports 80, 443, or 22 to the public internet, your VPS becomes an immediate target. Inspecting /var/log/auth.log the next morning will routinely reveal anywhere between 5,000 and 10,000 automated scans and brute-force attempts from botnets. Layer 3/4 firewalls such as UFW or iptables are great at filtering traffic by IP and port; however, they remain completely blind to the actual packet payload.
If an attacker injects a SQL Injection payload like ' OR 1=1 -- or a Log4j exploit over a legitimate port 443 HTTPS connection, UFW lets it through without blinking. You need Deep Packet Inspection (DPI) to address this vulnerability. Suricata inspects every single byte within the payload to identify and neutralize threats on the wire in real time.
IDS vs. IPS: Which Deployment Model Should You Choose?
Understanding these two operational modes helps avoid network outages and CPU bottlenecks:
- IDS (Intrusion Detection System): Suricata analyzes a copy of network traffic received via port mirroring or passive capture (AF_PACKET). It evaluates packets against loaded rule signatures and writes alerts when anomalies are found. The original network flow traversing your switch or network interface experiences zero latency. This passive setup is ideal for production testing and validation.
- IPS (Intrusion Prevention System): Suricata operates directly inline with the traffic flow (via iptables NFQueue or AF_PACKET inline mode). Malicious packets are immediately hit with a
droporrejectaction. Attacks are neutralized before they ever hit Nginx or your database. The trade-off is a slight processing latency overhead on the host CPU (a few microseconds per packet). - Rule and Signature Engine: Suricata evaluates packets against tens of thousands of attack signatures. Each rule defines protocols, source/destination IPs and ports, hex or string payload matches, and a unique identifier (SID).
- The
eve.jsonLog File: Everything that happens on your system—from security alerts and DNS queries to TLS handshakes and HTTP transaction metadata—is unified into this single JSON log file. You can easily parse it usingjqor ship it directly to Grafana Loki or an ELK stack.
Step-by-Step Suricata Installation and Configuration
Step 1: Install Suricata from the Official OISF PPA
Default Ubuntu package repositories often ship outdated Suricata releases. To leverage the latest multi-threaded inspection engine, install it directly from the official OISF repository:
# Install software repository management tools
sudo apt update && sudo apt install -y software-properties-common jq
# Add the official stable PPA
sudo add-apt-repository -y ppa:oisf/suricata-stable
# Install Suricata
sudo apt update
sudo apt install -y suricata
Verify the build information to ensure packet capture modules are enabled:
suricata --build-info | grep -E "AF_PACKET|NFQueue"
Step 2: Configure Network Interface and Environment Variables
First, identify the network interface receiving internet traffic:
ip -br addr
Assuming your network interface is eth0 and the public IP is 103.20.100.5, open the main configuration file:
sudo nano /etc/suricata/suricata.yaml
Define the protected server subnets and addresses under the HOME_NET variable:
vars:
address-groups:
HOME_NET: "[192.168.1.0/24,10.0.0.0/8,103.20.100.5/32]"
EXTERNAL_NET: "!$HOME_NET"
Locate the af-packet block and set your interface name accordingly:
af-packet:
- interface: eth0
cluster-id: 99
cluster-type: cluster_flow
defrag: yes
use-mmap: yes
tpacket-v3: yes
Step 3: Update Emerging Threats (ET) Open Signatures
Suricata includes the built-in suricata-update utility to automatically fetch over 30,000 free community rules:
# Download the latest ET Open ruleset
sudo suricata-update
# Test the configuration file syntax for errors
sudo suricata -t -c /etc/suricata/suricata.yaml -v
If the command outputs Configuration provided was successfully parsed, your setup is verified and ready.
Step 4: Enable and Start Suricata in IDS Mode
Run Suricata as a background daemon and configure it to launch at boot:
sudo systemctl restart suricata
sudo systemctl enable suricata
sudo systemctl status suricata
Step 5: Write Custom Rules and Test Real-Time Alerts
Create a simple custom rule to flag ICMP Ping requests. Open your local rules file:
sudo nano /var/lib/suricata/rules/local.rules
Append the following rule definition:
alert icmp any any -> $HOME_NET any (msg:"TEST: ICMP ping detected by Suricata"; sid:1000001; rev:1;)
Verify that /var/lib/suricata/rules/local.rules is referenced under the rule-files: directive in suricata.yaml. Then reload rules without restarting the daemon:
sudo suricatasc -c reload-rules
Stream security alerts in real time with jq:
sudo tail -f /var/log/suricata/eve.json | jq -c 'select(.event_type=="alert") | {time: .timestamp, src: .src_ip, alert: .alert.signature}'
From a remote workstation, ping the server IP:
ping 103.20.100.5 -c 2
Your server terminal will instantly display a JSON log entry matching signature sid:1000001.
Step 6: Enable IPS Mode to Automatically Drop Malicious Packets
To convert Suricata from a passive monitoring tool into an active prevention system, change the rule action from alert to drop.
Edit the rule in /var/lib/suricata/rules/local.rules:
drop icmp any any -> $HOME_NET any (msg:"IPS DROP: Ping packet dropped"; sid:1000001; rev:2;)
Save your changes and reload the rule engine:
sudo suricatasc -c reload-rules
Stop the standard IDS service to test inline packet processing with Netfilter queues:
sudo systemctl stop suricata
# Direct all incoming ICMP packets to Netfilter queue 0
sudo iptables -I INPUT -p icmp -j NFQUEUE --queue-num 0
# Run Suricata inline listening on queue 0
sudo suricata -q 0 -c /etc/suricata/suricata.yaml -D
Send another ping request from your external machine. The ping command will hang and report 100% packet loss. Meanwhile, eve.json records the action explicitly: "action": "blocked" (or drop). The packet was discarded at the kernel level before reaching any user-space application.
Once testing is complete, remove the experimental iptables rule and restore normal service operation:
sudo iptables -D INPUT -p icmp -j NFQUEUE --queue-num 0
sudo killall suricata
sudo systemctl start suricata
Production Best Practices and Performance Tuning
Suricata is extremely capable, but misconfigurations can quickly consume server resources. Loading the complete ET Open ruleset (30,000+ rules) typically requires between 1.2 GB and 1.8 GB of system memory.
Keep your system responsive and minimize false positives with these production tips:
- Automate rule updates: Set up a daily cron job to run
suricata-updateat 3:00 AM, coupled with an automatic rule reload command. - Prune unneeded rulesets: If your host only runs Nginx and PostgreSQL, use
disable.confto strip out irrelevant signatures targeting Windows, Active Directory, or game servers. - Tune thread allocation: Align packet inspection threads with your physical CPU core count under the
threadingsection of your yaml configuration.
