How to Use iptables hashlimit Module
hashlimit provides per-IP rate limiting, essential for fair traffic distribution and abuse prevention. It creates separate rate tracking per source. This guide walks through the specific troubleshooting steps to diagnose and resolve hashlimit issues.
Before You Begin
Before you begin, be sure to have the following in place:
- A Linux server with the relevant software installed
- Access to the command line interface
- Appropriate permissions (root or sudo)
Quick Fix
Wrong
iptables -m limit --limit 10/s (global rate limit)
Wrong: Global rate limit affects all IPs equally
Right
iptables -A INPUT -p tcp --dport 80 -m hashlimit --hashlimit-mode srcip --hashlimit-name http --hashlimit 10/sec --hashlimit-burst 20 -j ACCEPT
Right: Per-IP rate limit with hashlimit
Output
Hashlimit configured for port 80:\n Mode: srcip\n Rate: 10/sec per IP\n Burst: 20
Prevention
To avoid future issues, follow these best practices:
- Use hashlimit for per-IP or per-IP-port rate limiting
- hashlimit creates a hash table tracking each source
- Use --hashlimit-mode srcip for per-IP or srcip,dstport for per-flow
- Set --hashlimit-name to name the hash table for monitoring
- Check hash table usage in /proc/net/ipt_hashlimit/
DodaTech Tools
For further assistance with any of the above issues, consider using DodaTech consulting services or DodaTech tutorials for more in-depth guidance.
Common Mistakes with hashlimit
- Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
These mistakes appear frequently in real-world IPTABLES code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro