Implementing Rate Limiting with iptables hashlimit Module

This article explains how to use the iptables hashlimit module to create a stateful rate‑limiting chain, details the required commands, clarifies the meaning of the --hashlimit‑upto and --hashlimit‑burst parameters, and provides an example illustrating credit‑based packet acceptance.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Implementing Rate Limiting with iptables hashlimit Module

iptables processes each packet independently and is inherently stateless, so implementing a stateful rate‑limit requires the hashlimit module.

The complete set of commands to create a rate‑limiting chain is:

iptables --new-chain SOCAT-RATE-LIMIT
iptables --append SOCAT-RATE-LIMIT \
    --match hashlimit \
    --hashlimit-mode srcip \
    --hashlimit-upto 50/sec \
    --hashlimit-burst 100 \
    --hashlimit-name conn_rate_limit \
    --jump ACCEPT
iptables --append SOCAT-RATE-LIMIT --jump DROP
iptables -I INPUT -p tcp --dport 1234 --jump SOCAT-RATE-LIMIT

The first line creates a new chain for rate limiting. The second line accepts packets that are within the defined limit; packets exceeding the limit are sent to the third line, which drops them. Finally, the new chain is inserted into the INPUT chain to protect the specified port.

The rate‑limit algorithm is controlled by two parameters: --hashlimit-upto defines the maximum number of packets allowed per second (e.g., 50/sec means one packet every 20 ms). --hashlimit-burst provides an initial credit allowing a burst of packets (e.g., 20 packets can be sent instantly).

Each source IP starts with a burst credit; each incoming packet consumes one credit. When the credit is exhausted, further packets are dropped until credits are replenished at the rate specified by --hashlimit‑upto, up to the burst limit.

For example, with --hashlimit-upto 50/sec --hashlimit-burst 20, an IP sending packets at a steady 1 ms interval will have the first 20 packets accepted (burst credit). After the burst is used, one additional packet is accepted every 20 ms, resulting in a total of 70 packets accepted before dropping resumes.

The effect of the rate limit is clearly visible in the accompanying traffic graph.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

firewallLinuxrate limitingnetwork securityiptableshashlimit
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.