SkillPilotAI

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

Without ipset: 10,000 rules
  1. 1.Attacker 192.168.124.20 sends a packet to the server

  2. 2.Firewall INPUT chain has one rule per blocked IP

  3. 3.Packet is compared rule by rule, top to bottom

  4. 4.Up to 10,000 comparisons before a verdict

Linear scan: costly CPU and slow reload

With ipset: 1 rule + hash set
  1. 1.Firewall has one rule: source IP in set blocklist -> DROP

  2. 2.blocklist (hash:ip) has member 192.168.124.20, timeout 600 s

  3. 3.Attacker packet: hash lookup finds it -> dropped at the firewall

  4. 4.Client 192.168.124.30: not in the set -> next rule

  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.

Terminal
# 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
Output
$ 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 exist

104.4 Commands and general options

CommandWhat it does
create SET TYPE [opts] (n)Make a set
add / del SET ENTRYAdd or remove an entry
test SET ENTRYExit code 0 if found
list [SET] / save [SET]Header + members / restore format
restore < fileLoad 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
OptionMeaning
-exist / -!Ignore 'already exists' errors (restore scripts)
-output / -oplain | save | xml | json
-quiet / -qSilent, use exit code
-resolve / -rIP to hostname (slow)
-sorted / -s, -name / -n, -terse / -tSorted output, names only, names + headers
-file / -fRead/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

Output
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..
Terminal
# type help: no kernel neededipset help hash:ip
Output
create 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

TypeStoresExample entry
hash:ipHosts (or networks with netmask/bitmask)add foo 192.168.1.0/24
bitmap:ipIPv4 in a fixed range (max 65536), range requiredcreate nets bitmap:ip range 10.0.0.0/8 netmask 24
bitmap:ip,macIP + MAC; MAC learned from first packet if omittedadd foo 192.168.1.1,12:34:56:78:9A:BC
bitmap:portPorts in a rangeadd foo 8000-8080
hash:mac / hash:ip,macMAC / IP + MAC pairadd foo 192.168.1.10,01:02:03:04:05:06
hash:netNetworks of any size (no /0), nomatch exceptionsadd foo 192.168.0.0/30 nomatch
hash:net,netSource + destination subnet pairsadd foo 192.168.0.0/24,10.0.1.0/24
hash:ip,port / hash:net,portIP or net + [proto:]port (default TCP)192.168.1.10,udp:53
hash:ip,port,ip / ip,port,net / net,port,netThree-part tuplesadd foo 192.168.1.10,80,10.0.0.1
hash:ip,markIP + firewall mark (markmask)add foo 192.168.1.10,0x63
hash:net,ifaceNetwork + interface (physdev:, wildcard)add foo 192.168.0/24,eth0
list:setOrdered union of other setsadd all blocklist6 after blocklist
Terminal
ipset help hash:ip,port
Output
add    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 protocols

104.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.
Terminal
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
Output
$ 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 ACCEPT

104.8 Real-world example: the same blocklist as an nftables set

Terminal
# 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
Output
[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 1018ms

104.9 Comparison: hash vs bitmap vs list

Pointbitmap:*hash:*list:set
MemoryFixed range preallocatedGrows as neededJust set names
Size limitMax 65536, range requiredmaxelem (default 65536)-
FamilyIPv4 onlyinet or inet6Mix of sets
Best forSmall dense ranges (one /16, ports)Blocklists, country subnets, tuplesOne 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.