Operating System Linux

Limit Concurrent Connections per IP on Linux with iptables and nftables

Limit how many connections one IP address may hold open or start per minute, with iptables connlimit and recent, or nftables ct count, on current Linux releases.

One client that opens hundreds of connections to your web server can use up worker processes and slow the service for everyone else. The kernel firewall can stop this before the connection reaches the application. This guide shows how to cap the number of simultaneous connections per source address, and the number of new connections per minute, with iptables and with nftables. It applies to Ubuntu 26.04 and 24.04 LTS, Debian 13 and 12, and AlmaLinux 9 / Rocky Linux 9 / CentOS Stream 9.

On all of these releases the packet filter in the kernel is nftables. The iptables command still exists as a compatibility front end (iptables-nft) and is fine on Debian and Ubuntu; on the RHEL 9 family it is deprecated and firewalld is the default, so use the nftables method there.

Two different limits

  • Concurrent connections: how many connections one address has open at the same moment. This is the connlimit match in iptables and ct count in nftables. Both read the kernel's connection tracking table.
  • Connection rate: how many new connections one address starts within a period. This is the recent (or hashlimit) match in iptables and limit rate in nftables. Many tutorials titled "concurrent connections" actually configure this kind of limit.

You can use either or both. The examples protect TCP port 80; change the port, or list several, to suit your service.

Requirements and a warning

  • A user with sudo rights.
  • A second way into the server (your provider's web console) in case a rule locks you out. Do not apply these limits to your SSH port until you have tested them elsewhere.
  • Know where your clients really come from. If the service sits behind a reverse proxy, load balancer or CDN, every connection arrives from the proxy's addresses and a per-IP limit in the firewall would throttle all visitors at once. Limit in the proxy instead (for example nginx limit_conn).

Method 1: iptables (Debian and Ubuntu)

Install the command and the package that saves rules across reboots:

sudo apt update
sudo apt install iptables iptables-persistent

Limit simultaneous connections

Reject a new connection to port 80 when the same source address already has 20 open:

sudo iptables -I INPUT 1 -p tcp --syn --dport 80 -m connlimit --connlimit-above 20 -j REJECT --reject-with tcp-reset
  • -I INPUT 1 puts the rule at the top of the chain, so it is evaluated before any rule that accepts port 80.
  • --syn matches only the packet that opens a connection; established connections are not touched.
  • --connlimit-above 20 matches when the client already has more than 20 connections.
  • REJECT --reject-with tcp-reset refuses at once. Use -j DROP to stay silent instead.

To count a whole network as one client, add a prefix length. This allows 20 connections per /24:

sudo iptables -I INPUT 1 -p tcp --syn --dport 80 -m connlimit --connlimit-above 20 --connlimit-mask 24 -j REJECT --reject-with tcp-reset

IPv6 has its own rule set. Counting per /64 is sensible there, because one customer usually controls an entire /64:

sudo ip6tables -I INPUT 1 -p tcp --syn --dport 80 -m connlimit --connlimit-above 20 --connlimit-mask 64 -j REJECT --reject-with tcp-reset

Limit new connections per minute

The recent match keeps a list of source addresses with timestamps. Add these two rules in this order:

sudo iptables -I INPUT 1 -p tcp --dport 80 -m conntrack --ctstate NEW -m recent --name http --set
sudo iptables -I INPUT 1 -p tcp --dport 80 -m conntrack --ctstate NEW -m recent --name http --update --seconds 60 --hitcount 10 -j DROP

Because each command inserts at position 1, the second rule ends up above the first. A new connection is first checked against the list: if the address has started 10 or more connections in the last 60 seconds, the packet is dropped (and --update refreshes its timestamp, so a client that keeps hammering stays blocked). Otherwise the next rule records the address with --set and the packet continues down the chain.

Check and save

List the rules with packet counters:

sudo iptables -L INPUT -n -v --line-numbers

Save the running IPv4 and IPv6 rules so they are restored at boot:

sudo netfilter-persistent save

To remove a rule, delete it by its line number and save again:

sudo iptables -D INPUT 1
sudo netfilter-persistent save

Method 2: nftables (all distributions, recommended on the RHEL 9 family)

The rules below live in their own table, so they work next to firewalld, ufw or an existing nftables rule set without editing it: a packet rejected in any table is gone, and a packet this table lets through is still subject to your normal firewall.

The nft command is installed by default on AlmaLinux and Rocky Linux. On Debian and Ubuntu install it if needed:

sudo apt install nftables

Write the rules

Create the directory if it does not exist, then create /etc/nftables/connlimit.nft:

sudo mkdir -p /etc/nftables
sudo nano /etc/nftables/connlimit.nft
table inet connlimit
delete table inet connlimit

table inet connlimit {
    set conns4 {
        type ipv4_addr
        size 65535
        flags dynamic
    }

    set conns6 {
        type ipv6_addr
        size 65535
        flags dynamic
    }

    set rate4 {
        type ipv4_addr
        size 65535
        flags dynamic
        timeout 1m
    }

    chain input {
        type filter hook input priority -5; policy accept;

        tcp dport 80 ct state new add @conns4 { ip saddr ct count over 20 } counter reject with tcp reset
        tcp dport 80 ct state new add @conns6 { ip6 saddr ct count over 20 } counter reject with tcp reset
        tcp dport 80 ct state new update @rate4 { ip saddr limit rate over 10/minute } counter drop
    }
}
  • The first two lines make the file safe to load repeatedly: they ensure the table exists, delete it, and the rest recreates it.
  • ct count over 20 is the concurrent limit: each source address gets an entry in the set, and a new connection is rejected when that address already has more than 20 tracked connections. The sets used with ct count must not have a timeout; entries follow the connection tracking timers.
  • limit rate over 10/minute is the rate limit for IPv4: new connections beyond 10 per minute from one address are dropped. Delete that line and the rate4 set if you only want the concurrent limit.
  • For several ports write tcp dport { 80, 443 }.

Check the syntax without loading anything, then load it:

sudo nft -c -f /etc/nftables/connlimit.nft
sudo nft -f /etc/nftables/connlimit.nft

Load the rules at boot

On a server that runs firewalld or ufw, do not enable the distribution's nftables.service for this: it manages the complete rule set and can flush the rules of the other firewall. Use a small dedicated unit instead. Create /etc/systemd/system/connlimit.service:

[Unit]
Description=Per-IP connection limits (nftables)
After=network-pre.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/sbin/nft -f /etc/nftables/connlimit.nft
ExecStop=/usr/sbin/nft delete table inet connlimit

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now connlimit.service

After editing the file, apply it with sudo systemctl restart connlimit.service. To remove the limits, run sudo systemctl disable --now connlimit.service.

Check that it works

Show the table with its counters and the addresses currently tracked:

sudo nft list table inet connlimit

The counter on each rule increases whenever a connection is refused. If legitimate users hit the limit, raise the numbers: a modern browser opens several parallel connections per site, and offices, schools and mobile carriers put many users behind one address.

Frequently asked questions

Will this stop a DDoS attack?

No. It restrains a single abusive client, a misbehaving crawler or a simple script. A distributed attack comes from thousands of addresses that each stay under the limit, and a volumetric flood fills your uplink before the firewall sees a packet. That has to be filtered upstream by your provider or a protection service.

Can firewalld do this with a rich rule?

Not for concurrent connections. The firewalld rich language has a limit value="rate/duration" element, but it is an overall rate for the rule, not a count per client address, and there is no equivalent of connlimit. A separate nftables table as shown above runs alongside firewalld without conflict.

Should the limit be in the firewall or in the web server?

The firewall limit is cheap and protects any TCP service, but it only sees addresses and ports. A web server or proxy can limit per URL, per virtual host or per real client address taken from a trusted proxy header, and can answer with a proper error page. For HTTP behind a proxy, limit in the proxy; for directly exposed services such as game or mail servers, the firewall is the right place.

Why does the hit count in the recent rule not go above 20?

The recent module remembers a limited number of packets per address (20 by default, the ip_pkt_list_tot module parameter), so --hitcount cannot exceed that value unless the module is loaded with a larger one. For higher thresholds use hashlimit or the nftables method.

Share story:

Continue reading other guides on How VPS.