ipset
ipset builds IP sets in the Linux kernel: fast lookup tables of IPs, networks, ports, MACs or interface names. One iptables rule refers to the whole set, so even a 10,000-entry blocklist costs one hash lookup per packet.
104.1 Why is it used?
- Without ipset every blocked IP needs its own rule and every packet is scanned linearly through all of them: high CPU, slow reloads.
- With ipset one rule does a near constant-time hash lookup.
- Sets can be updated without reloading the ruleset; timeout entries expire automatically.
- Linux only: needs the ipset package plus ip_set kernel modules. A type is named method:datatype, e.g. hash:ip, bitmap:port, list:set.
104.2 How it works
- Step 1
1.Attacker 192.168.124.20 sends a packet to the server
- Step 2
2.Firewall INPUT chain has one rule per blocked IP
- Step 3
3.Packet is compared rule by rule, top to bottom
- Step 4
4.Up to 10,000 comparisons before a verdict
Linear scan: costly CPU and slow reload
- Step 1
1.Firewall has one rule: source IP in set blocklist -> DROP
- Step 2
2.blocklist (hash:ip) has member 192.168.124.20, timeout 600 s
- Step 3
3.Attacker packet: hash lookup finds it -> dropped at the firewall
- Step 4
4.Client 192.168.124.30: not in the set -> next rule
- Step 5
5.Client packet reaches server 192.168.124.10
104.3 Lab check: version and kernel
Lab containers: srv 192.168.124.10, bad .20, good .30. ip_set and xt_set are loaded, but every set type (hash:ip...) is a separate module that is not loaded; creating a set would auto-load it, so only commands that need no set type are run with ipset.
# Docker VM: which ip_set modules?grep -E "^(ip_set|xt_set)" /proc/moduleszcat /proc/config.gz | grep IP_SET_# srv 192.168.124.10 (NET_ADMIN)ipset versionipset listipset test blocklist 192.168.124.20$ grep -E "^(ip_set|xt_set)" /proc/modulesxt_set 20480 0 - Live 0x0000000000000000ip_set 53248 1 xt_set, Live 0x0000000000000000$ zcat /proc/config.gz | grep IP_SET_CONFIG_IP_SET_BITMAP_IP=mCONFIG_IP_SET_BITMAP_PORT=mCONFIG_IP_SET_HASH_IP=mCONFIG_IP_SET_HASH_IPPORT=mCONFIG_IP_SET_HASH_NET=mCONFIG_IP_SET_HASH_NETIFACE=mCONFIG_IP_SET_LIST_SET=m$ ipset versionipset v7.21, protocol version: 7$ ipset list$ ipset test blocklist 192.168.124.20ipset v7.21: The set with the given name does not exist104.4 Commands and general options
| Command | What it does |
|---|---|
| create SET TYPE [opts] (n) | Make a set |
| add / del SET ENTRY | Add or remove an entry |
| test SET ENTRY | Exit code 0 if found |
| list [SET] / save [SET] | Header + members / restore format |
| restore < file | Load a saved file |
| flush [SET] / destroy [SET] (x) | Remove all entries / delete set (no name: all) |
| rename FROM TO (e) | New name |
| swap FROM TO (w) | Exchange two sets atomically |
| Option | Meaning |
|---|---|
| -exist / -! | Ignore 'already exists' errors (restore scripts) |
| -output / -o | plain | save | xml | json |
| -quiet / -q | Silent, use exit code |
| -resolve / -r | IP to hostname (slow) |
| -sorted / -s, -name / -n, -terse / -t | Sorted output, names only, names + headers |
| -file / -f | Read/write a file |
- Long option names cannot be shortened; long command names can (n, x, e, w).
- ipset help ran in the good container without NET_ADMIN: help lives only in userspace.
104.5 Entry syntax and create/add options
ENTRY = type order, comma separated 192.168.1.10,80,10.0.0.1 [test-host],[ftp-data] names with dash 192.168.1.10 or 192.168.1.0/28 192.168.1.10-192.168.1.20 rangetimeout N default/per-entry, 0 = forevercounters packets N bytes Ncomment comment "blocked by admin"skbinfo skbmark skbprio skbqueuehashsize 1024 maxelem 65536bucketsize 2-14 family inet|inet6nomatch exception in *net* setsforceadd full set: random remove entrywildcard iface prefix: eth = eth0..# type help: no kernel neededipset help hash:ipcreate SETNAME hash:ip [family inet|inet6]|[-4|-6] [hashsize VALUE] [maxelem VALUE] [netmask CIDR] [bitmask bitmask] [timeout VALUE] [counters] [comment] [forceadd] [skbinfo] [bucketsize VALUE] [initval VALUE]add SETNAME hash:ip IP [timeout VALUE] [packets VALUE] [bytes VALUE] [comment "string"] [skbmark VALUE] [skbprio VALUE] [skbqueue VALUE]del SETNAME hash:ip IPtest SETNAME hash:ip IPType hash:ip supports inet and inet6.104.6 Set types
| Type | Stores | Example entry |
|---|---|---|
| hash:ip | Hosts (or networks with netmask/bitmask) | add foo 192.168.1.0/24 |
| bitmap:ip | IPv4 in a fixed range (max 65536), range required | create nets bitmap:ip range 10.0.0.0/8 netmask 24 |
| bitmap:ip,mac | IP + MAC; MAC learned from first packet if omitted | add foo 192.168.1.1,12:34:56:78:9A:BC |
| bitmap:port | Ports in a range | add foo 8000-8080 |
| hash:mac / hash:ip,mac | MAC / IP + MAC pair | add foo 192.168.1.10,01:02:03:04:05:06 |
| hash:net | Networks of any size (no /0), nomatch exceptions | add foo 192.168.0.0/30 nomatch |
| hash:net,net | Source + destination subnet pairs | add foo 192.168.0.0/24,10.0.1.0/24 |
| hash:ip,port / hash:net,port | IP or net + [proto:]port (default TCP) | 192.168.1.10,udp:53 |
| hash:ip,port,ip / ip,port,net / net,port,net | Three-part tuples | add foo 192.168.1.10,80,10.0.0.1 |
| hash:ip,mark | IP + firewall mark (markmask) | add foo 192.168.1.10,0x63 |
| hash:net,iface | Network + interface (physdev:, wildcard) | add foo 192.168.0/24,eth0 |
| list:set | Ordered union of other sets | add all blocklist6 after blocklist |
ipset help hash:ip,portadd SETNAME hash:ip,port IP,[PROTO:]PORT[PROTO:]PORT is a valid pattern of the following: PORTNAME TCP port name from /etc/services PORTNUMBER TCP port number identifier tcp|sctp|udp|udplite:PORTNAME|PORTNUMBER icmp:CODENAME supported ICMP codename icmp:TYPE/CODE ICMP type/code value icmpv6:CODENAME supported ICMPv6 codename PROTO:0 all other protocols104.7 Using sets from iptables
- -m set --match-set NAME src|dst checks a whole set in one rule; two-dimension sets take src,dst.
- -j SET --add-set NAME src adds the packet's address to a set straight from a rule.
- A set used by a rule cannot be destroyed; a rule with a missing set is not added.
iptables -I INPUT -m set \ --match-set blocklist src -j DROP# -A FORWARD -m set \# --match-set foo src,dst -j ACCEPT# -p tcp --dport 22 -j SET \# --add-set ssh_seen srciptables -j SET -h$ iptables -j SET -hSET target options: --add-set name flags [--exist] [--timeout n] --del-set name flags --map-set name flags [--map-mark] [--map-prio] [--map-queue]$ iptables -I INPUT -m set --match-set blocklist src -j DROPiptables v1.8.10 (nf_tables): Set blocklist doesn't exist.$ iptables -S INPUT-P INPUT ACCEPT104.8 Real-world example: the same blocklist as an nftables set
# srv 192.168.124.10nft add table inet labnft add set inet lab blocklist \ '{ type ipv4_addr; flags timeout; timeout 1h; counter; }'nft add element inet lab blocklist \ '{ 192.168.124.20 timeout 10m }'nft add chain inet lab input \ '{ type filter hook input priority 0; }'nft add rule inet lab input \ ip saddr @blocklist drop# from bad .20 and good .30ping -c 2 -W 1 192.168.124.10nft list set inet lab blocklist[n104-bad]$ ping -c 2 -W 1 192.168.124.10 | tail -22 packets transmitted, 0 received, 100% packet loss, time 1020ms[n104-good]$ ping -c 2 -W 1 192.168.124.10 | tail -22 packets transmitted, 2 received, 0% packet loss, time 1022ms[n104-srv]$ nft list set inet lab blocklisttable inet lab { set blocklist { type ipv4_addr counter timeout 1h elements = { 192.168.124.20 counter packets 2 bytes 168 timeout 10m expires 9m37s96ms } }[n104-srv]$ nft delete element inet lab blocklist '{ 192.168.124.20 }'[n104-bad]$ ping -c 2 -W 1 192.168.124.10 | tail -22 packets transmitted, 2 received, 0% packet loss, time 1018ms104.9 Comparison: hash vs bitmap vs list
| Point | bitmap:* | hash:* | list:set |
|---|---|---|---|
| Memory | Fixed range preallocated | Grows as needed | Just set names |
| Size limit | Max 65536, range required | maxelem (default 65536) | - |
| Family | IPv4 only | inet or inet6 | Mix of sets |
| Best for | Small dense ranges (one /16, ports) | Blocklists, country subnets, tuples | One rule over IPv4 + IPv6 sets |
104.10 Common mistakes
- Assuming sets survive a reboot: they live only in kernel memory. Save with ipset save > /etc/ipset.conf, load at boot with ipset restore -exist.
- Loading iptables rules before the sets: a rule with a missing set does not load.
- Flushing and refilling a live set for a daily update: leaves a gap; fill a new set and swap.
- Trying to destroy a set still referenced by a rule.
104.11 Interview tips
- Why ipset: N rules = linear scan; ipset = 1 rule + hash lookup, update without ruleset reload, timeout auto-expire.
- 10,000 country subnets updated daily: hash:net + fill a new set + ipset swap (no gap, rule unchanged).
- Type = method:datatype (hash:ip, bitmap:port, list:set).
- Persist with save/restore, restore before the firewall rules.
- In nftables the same idea is a native set: ip saddr @blocklist drop.