Iptables How To: 5 Easy Fixes, Avoid Rule Order Errors

This iptables how to guide takes a firewall script that has circulated for years, loads it in a test container and checks what each rule really does. The script is the one from the old version of this post: a mail server with SSH limits, a SYN-flood chain and a ping limit. Result: it does not load as written, and once it loads, three of its protections never match. The test used Debian 13 with iptables v1.8.11 (the nf_tables backend) and a second container as the client.

Scope of this iptables how to test: only the filter table and IPv4 were tested. The time-based rule was loaded and printed, but not triggered at its hours.

Load the old rules: the first iptables how to error

The saved ruleset used the iptables-restore format. Checking it with iptables-restore --test < old.rules stopped with Bad argument `\' and Error occurred at line: 25, exit code 2. The cause is the line -m recent --update \ --seconds 60 --hitcount 4: the backslash is a line-continuation habit from the shell, but a restore file does not use it. After removing the backslash the test returned 0 and the rules loaded. In the listing, iptables -S shows the old -m state --state match, which the kernel handles through the connection-tracking module; the newer spelling is -m conntrack --ctstate, and the fixed ruleset below uses it.

Iptables how to read rule order: why the SSH limits never match

Rules in a chain are read from top to bottom, and the first match with a final target such as ACCEPT stops the search. In the old script the line that accepts new connections to port 22 comes before the connlimit and recent rules. To test it, a listener was started on port 22 of the server and the client opened six connections. All six were accepted, and the counters told why:

  • rule 4 (state NEW tcp dpt:22 ACCEPT) counted 6 packets;
  • rule 15 (connlimit REJECT), rule 16 (recent --set) and rule 17 (recent --update REJECT) counted 0 packets.

The same happens to the ping limit. The old script has -A INPUT -p icmp -j ACCEPT near the top, so the later rule with -m limit --limit 1/s never sees a packet. Five pings in 0.8 seconds all got an answer, with 0% packet loss. The last lines have the same flaw: the REJECT rule on line 22 matches everything before the final -j DROP on line 23, and that DROP counted 0 packets, so it is dead code.

iptables how to: restore error at line 25, the SSH limit rules with 0 packets in the old order, and the fixed order blocking the 4th connection
The restore error, the zero counters in the old order, and the result after the limits are moved above the ACCEPT.

A fixed iptables how to ruleset with the limits first

The fixed file keeps the same services but puts the limits before the accept rule, uses conntrack, merges the mail ports with multiport, and sets the default policy of INPUT and FORWARD to DROP, so the unreachable REJECT and DROP lines are not needed:

*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -m connlimit --connlimit-above 3 --connlimit-mask 32 -j REJECT --reject-with tcp-reset
-A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -m recent --name ssh --set
-A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -m recent --name ssh --update --seconds 60 --hitcount 4 -j REJECT --reject-with tcp-reset
-A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -j ACCEPT
-A INPUT -p tcp -m multiport --dports 25,80,143,443,465,993,995 -m conntrack --ctstate NEW -j ACCEPT
-A INPUT -p tcp -m multiport --dports 389,7071 -s 10.10.9.0/24 -m conntrack --ctstate NEW -j ACCEPT
-A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s --limit-burst 1 -j ACCEPT
COMMIT

With this order the same test gave: connections 1 to 3 succeeded, connections 4 to 6 failed. The counters show the cause: the recent --update rule rejected 3 packets and only 3 reached the final ACCEPT. The connlimit rule counted 0, because nc -z closes every connection at once, so there were never more than three open at the same time; it limits parallel connections, while recent limits the rate. A port that is not in the list (8080) was blocked by the default policy. The most useful iptables how to habit is to check the rules, not the intention. The command iptables -L INPUT -v -n --line-numbers is the quickest way to see whether a rule ever matches.

Time-based access and DNS rules

The old post limited SSH to weekday nights with -m time --timestart 01:00 --timestop 03:00 --days Mon,Tue,Wed,Thu,Fri. On iptables 1.8.11 this line fails with unknown option "--days" and exit code 2. The option of the time module is --weekdays, and the same rule with --weekdays loaded and was printed back with an end date that iptables adds itself (--datestop 2038-01-19T03:14:07).

The iptables-extensions manual says that the module compares the time in UTC unless you add --kerneltz. The test host showed 04:38 UTC, so convert your local hours before you copy the rule. The rule was loaded, not triggered at its hours.

The DNS rules of the old post allow replies with --sport 53 ... ESTABLISHED in INPUT and --dport 53 ... NEW,ESTABLISHED in OUTPUT. With OUTPUT set to ACCEPT, as in this ruleset, the OUTPUT lines add nothing, and the ESTABLISHED rule at the top already lets the replies in.

Iptables how to save the rules and avoid locking yourself out

The iptables how to test saved the active rules with iptables-save > /tmp/rules.v4 (20 lines), flushed them, restored them with iptables-restore < /tmp/rules.v4, and the INPUT chain was back with 11 lines of iptables -S INPUT.

How you load the file at boot depends on the distribution and was not tested. Before you set a DROP policy on a remote host, keep a second SSH session open and a console you can reach, because a wrong rule order locks you out. If you do get locked out, a console is the only way back; see how to reset a root password on CentOS 7 for an example of console-only recovery, and the Webmin install for a service whose port (10000) you should limit with a rule like the 389 and 7071 lines above.

Common mistakes in an iptables how to guide

  • Putting a broad ACCEPT above the rules that limit the same traffic.
  • Using a shell backslash in a file for iptables-restore.
  • Adding rules after a final REJECT or DROP that already matches everything.
  • Reading the time module in local time when the rule uses UTC, or using the old option name --days.

FAQ

Is this iptables how to approach still current? The test host used the nf_tables backend through the iptables command, and the rules loaded unchanged. Newer distributions may use nftables directly, which was not covered here.

Do these iptables how to rules work for IPv6? No. IPv6 needs ip6tables rules and was not tested.

Comments

  1. Why do we need specific rules for DNS in INPUT chain if we already have "-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT"?

    Thanks.

    ReplyDelete
  2. "Why do we need specific rules for DNS in INPUT chain if we already have "-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT"
    Yes, you're right! you test environment ok ?

    ReplyDelete

Post a Comment

Popular posts from this blog

Linux Desktop Frozen? 5 Proven Fixes Before a Hard Reboot

Minecraft Bedrock on Linux: 4 Proven Ways to Play in 2026

DXVK in Wine on Linux: 7 Easy Setup Steps, Tested