I use several PREROUTING rules in Jessie Debian to do port forward from WAN to LAN ip with following rule

iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 8088 -j DNAT --to-destination

ETH0 is public static IP
ETH0:0 is Local lan ip

These are NAT related IPTABLES

iptables -t nat -L -n
target     prot opt source               destination         
DNAT       tcp  -- !            tcp dpt:8088 to:
DNAT       tcp  --              tcp dpt:8091 to:
DNAT       tcp  --              tcp dpt:8092 to:
DNAT       tcp  -- !            tcp dpt:10554 to:
DNAT       udp  -- !            udp dpt:10554 to:
DNAT       tcp  --              tcp dpt:10080 to:
DNAT       tcp  --              tcp dpt:10443 to:
DNAT       tcp  --              tcp dpt:10052 to:
DNAT       tcp  --              tcp dpt:34567 to:
DNAT       tcp  --              tcp dpt:10050 to:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

target     prot opt source               destination         
MASQUERADE  all  --             
SNAT       tcp  --           tcp dpt:554 to:
SNAT       udp  --           udp dpt:554 to:
SNAT       tcp  --            tcp dpt:8081 to:
SNAT       tcp  --            tcp dpt:10443 to:
SNAT       tcp  --            tcp dpt:8080 to:
SNAT       tcp  --           tcp dpt:34567 to:
SNAT       tcp  --            tcp dpt:10050 to:

However this rule is applied also when accessing that port from LAN. As result I can enter any IP and port 8088 to browser URL and it will always reach destination. for example I can enter and it will work. this is not desired behaviour.

I'd like to exclude LAN from PREROUTING.

I tried ! -s but in prerouting "!" for source parameter does not work

I tried also another approach to use destination parameter "-d mydoman.com" in the rule, but this only works if I remove mydoman.com line from /etc/hosts because mydoman.com links to server local network IP. I'd like to keep that record too.

is there better way to avoid prerouting rule for local network?

  • What do you mean by 'for source parameter does not work'? You can use ! for source address in the PREROUTING chain without any problem. – ALex_hha Feb 23 '16 at 10:02
  • I mean that agent with LAN ip still can access service by entering any.ip.addres:8088 on port 8088 even if this rule is applied iptables -t nat -I PREROUTING ! -s -i eth0 -p tcp -m tcp --dport 8088 -j DNAT --to-destination – B.Ondrej Feb 23 '16 at 10:19
  • Shouldn't work. Could you add list of rules from nat table to the question – ALex_hha Feb 23 '16 at 10:23
  • yes, I also think it Shouldn't work. added iptables list NAT relevant portion – B.Ondrej Feb 23 '16 at 10:34
  • Is default gateway for subnet? Have you checked counter for the rule when clients get access to the Does the counter growing? – ALex_hha Feb 23 '16 at 10:46

If you intend to exclude from the DNAT rule, you shall use or as the ! -s parameter. The network range finishes at

$ ipcalc
Address:          11000000.10101000.00000000. 00000000
Netmask: = 24   11111111.11111111.11111111. 00000000
Wildcard:            00000000.00000000.00000000. 11111111
Network:       11000000.10101000.00000000. 00000000
HostMin:          11000000.10101000.00000000. 00000001
HostMax:        11000000.10101000.00000000. 11111110
Broadcast:        11000000.10101000.00000000. 11111111
Hosts/Net: 254 

You may restrict the DNAT rule by interfaces instead of using addresses. For example:

iptables -t nat -A PREROUTING '!' -i eth0 -p tcp --dport 8088 \
    -j DNAT --to-destination


iptables -t nat -A PREROUTING -i ${your_wan_interface} -p tcp --dport 8088 \
    -j DNAT --to-destination

Using the RETURN target is also a possible choice:

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 8088 -j RETURN
| improve this answer | |
  • Many thanks Anderson, how I could completely forget count proper mask... in fact I need to exclude 192.168.1-3.x - and indeed using mask /22 will fit and work also for PREROUTING. What confused me was that some people in forums says that "! -s" is not supported. indeed wrong information. I will be probably using mask approach however will also review "restrict the DNAT rule by interfaces" and RETURN options later – B.Ondrej Feb 23 '16 at 14:46
  • The IPTABLES manpage doesn't mention any restriction on using the exclamation mark in source specification nor in PREROUTING chain, so it's supposed to work. – Anderson Medeiros Gomes Feb 24 '16 at 1:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.