The iptables ipset module is failing because the specified set cannot be found by the iptables kernel module. This happens when iptables rules try to match or modify entries in an ipset that hasn’t been created, has been deleted, or has a name mismatch.
Here’s a breakdown of the common causes and how to fix them:
1. The ipset Was Never Created
This is the most straightforward cause: you have an iptables rule referencing an ipset that simply doesn’t exist.
Diagnosis:
ipset list <set_name>
If the ipset doesn’t exist, this command will output an error like ipset: <set_name>: set does not exist.
Fix:
You need to create the ipset first. The command depends on the type of ipset you intend to use. For example, to create a set named my_blocked_ips that stores IPv4 addresses:
ipset create my_blocked_ips hash:ip
Why it works: This command registers a new ipset with the kernel under the specified name and type. iptables can now find it because it has been formally created and is visible to the kernel’s netfilter subsystem.
2. Typo in ipset Name (Creation or Rule)
A simple typo in the ipset name during creation or when referencing it in iptables rules will lead to this error. It’s easy to miss, especially with complex or similar-sounding names.
Diagnosis:
Carefully compare the name used in your ipset create command (or configuration file) with the name used in your iptables rule.
# Check your ipset creation command/config
# Example: ipset create my_banned_ips hash:ip
# Check your iptables rule
# Example: iptables -I INPUT -m set --match-set my_banned_ip src -j DROP
Notice the subtle difference: my_banned_ips vs. my_banned_ip.
Fix:
Correct the typo in either the ipset create command or the iptables rule to ensure both use the exact same name.
# If the ipset was created with a typo:
ipset create my_banned_ips hash:ip
# Or, if the rule has a typo:
iptables -I INPUT -m set --match-set my_banned_ips src -j DROP
Why it works: Consistency in naming ensures that iptables is looking for the ipset that actually exists. The iptables module uses the provided string to look up the registered ipset in the kernel’s ipset table.
3. The ipset Was Deleted
The ipset might have existed previously but was explicitly deleted, either manually or by a script.
Diagnosis:
Use ipset list to check if the ipset is currently present.
ipset list <set_name>
If it’s not listed, it has been deleted.
Fix:
Recreate the ipset and re-apply your iptables rules.
ipset create <set_name> <type> <options>
# e.g., ipset create my_malicious_ips hash:ip timeout 3600
# Then re-apply your iptables rules that reference it.
Why it works: This restores the ipset to the kernel’s state, making it available for iptables to reference again.
4. iptables Service Restarted Without ipset Persistence
If your ipset definitions are not saved and restored across reboots or iptables service restarts, they will be lost.
Diagnosis:
After a server reboot or iptables service restart (systemctl restart iptables), run ipset list. If the set is gone, it’s not persistent.
Fix:
Use ipset’s persistence features.
-
Save the current
ipsetdefinitions:ipset save > /etc/ipset.rules -
Configure
iptablesservice to restore them: Edit/etc/sysconfig/iptables-config(on RHEL/CentOS/Fedora) or/etc/default/iptables(on Debian/Ubuntu) and ensureIPTABLES_MODULES_UNLOAD="no"andIPTABLES_SAVE_ON_STOP="yes"or similar options are set to loadipsetrules. On systems usingsystemd, you might need to ensure a service that loadsipsetrules runs before theiptablesservice. A common approach is to create a systemd service file that runsipset restore < /etc/ipset.rulesearly in the boot process, or to have your firewall script handle bothipsetcreation andiptablesrule application.A more modern approach for systemd is to create a service for ipset persistence: Create
/etc/systemd/system/ipset-restore.service:[Unit] Description=Restore ipset rules Before=network-pre.target iptables.service Wants=network-pre.target [Service] Type=oneshot ExecStart=/sbin/ipset restore -f /etc/ipset.rules [Install] WantedBy=multi-user.targetThen enable and start it:
systemctl enable ipset-restore.service systemctl start ipset-restore.serviceThen, ensure your
iptables-persistentor firewall script saves the rules on shutdown.ipset save > /etc/ipset.rulesAnd configure
iptablesto load them on startup. If usingiptables-persistent, you might need to add a hook or ensureipsetcommands are run by your main firewall script thatiptables-persistentloads.
Why it works: Saving the ipset definitions to a file and configuring the system to restore them on boot ensures that the ipset exists in the kernel’s memory when iptables rules are loaded or evaluated.
5. Incorrect ipset Type Used in iptables Rule
While less common for a "not found" error (more common for "type mismatch"), if iptables expects a certain type of ipset and finds one with a different name but incompatible type, it might manifest as a lookup failure in some contexts. More likely, you’re referencing an ipset that was created with different parameters that make it incompatible with the match.
Diagnosis:
Check the type of the ipset using ipset list <set_name>. Then check the iptables rule to see if it’s using the correct match type (e.g., src, dst).
# Example: ipset list my_ips
# Name: my_ips
# Type: hash:ip
# ...
# Example iptables rule:
# iptables -A INPUT -m set --match-set my_ips src -j ACCEPT
If my_ips was created as hash:net (for network ranges) and you’re trying to match src (source IP), it will fail, but the error might be confusing. The prompt specifically mentions "Set Not Found," which usually implies the name isn’t registered. However, if a custom iptables module is involved or a very old kernel, this could be a factor.
Fix:
Ensure the ipset type matches what your iptables rule expects. If you need to match source IPs, hash:ip is appropriate. If you need to match source networks, hash:net is appropriate.
# If you need hash:ip but have hash:net
ipset destroy my_ips
ipset create my_ips hash:ip
# Re-add your IPs
Why it works: iptables modules are designed to work with specific ipset types. If the types don’t align, the iptables module cannot correctly interpret or use the ipset’s contents, leading to errors that can sometimes be reported as a "not found" issue if the lookup mechanism is strict about type compatibility as well as name.
6. ipset Module Not Loaded
The ipset functionality in iptables relies on kernel modules. If these modules aren’t loaded, iptables won’t be able to find or use any ipsets, even if they are technically created.
Diagnosis:
Check if the ipset kernel modules are loaded:
lsmod | grep ipset
You should see xt_set and potentially ip_set.
Fix: Load the necessary modules manually or ensure they are loaded automatically at boot.
modprobe xt_set
modprobe ip_set
On systems using systemd, you can add these to /etc/modules-load.d/ipset.conf to ensure they load on boot:
xt_set
ip_set
Why it works: Loading the kernel modules makes the ipset functionality available to the iptables framework. Without xt_set loaded, the iptables set match module cannot even attempt to query the kernel for ipsets.
The next error you’ll likely encounter after resolving ipset not found issues is a "Set type mismatch" or "No such file or directory" if iptables-restore fails due to syntax errors in your rules file, or if you attempt to add elements to an ipset without creating it first.