Iptables command

From DD-WRT Wiki

Revision as of 15:36, 25 June 2006 by (Talk)
Jump to: navigation, search


This is an infant page. Clean it up and fill it with content!

I think we should have something about firewall builder on this page, since they're kind of related....

Basic Usage

iptables -[AD] chain rule-specification [options]
iptables -[RI] chain rulenum rule-specification [options]
iptables -D chain rulenum [options]
iptables -[LFZ] [chain] [options]
iptables -[NX] chain
iptables -E old-chain-name new-chain-name
iptables -P chain target [options]
iptables -h (print this help information)


--append  -A chain            Append to chain
--delete  -D chain            Delete matching rule from chain
--delete  -D chain rulenum
                              Delete rule rulenum (1 = first) from chain
--insert  -I chain [rulenum]
                              Insert in chain as rulenum (default 1=first)
--replace -R chain rulenum
                              Replace rule rulenum (1 = first) in chain
--list    -L [chain]          List the rules in a chain or all chains
--flush   -F [chain]          Delete all rules in  chain or all chains
--zero    -Z [chain]          Zero counters in chain or all chains
--new     -N chain            Create a new user-defined chain
          -X [chain]          Delete a user-defined chain
--policy  -P chain target
                              Change policy on chain to target
          -E old-chain new-chain
                              Change chain name, (moving any references)


--proto       -p [!] proto    protocol: by number or name, eg. `tcp'
--source      -s [!] address[/mask]
                              source specification
--destination -d [!] address[/mask]
                              destination specification
--in-interface -i [!] input name[+]
                              network interface name ([+] for wildcard)
--jump        -j target
                              target for rule (may load target extension)
--match       -m match
                              extended match (may load extension)
--numeric     -n              numeric output of addresses and ports
--out-interface -o [!] output name[+]
                              network interface name ([+] for wildcard)
--table       -t table        table to manipulate (default: `filter')
--verbose     -v              verbose mode
--line-numbers                print line numbers when listing
--exact       -x              expand numbers (display exact values)
--fragment  -f                match second or further fragments only
--modprobe=<command>          try to insert modules using this command
--set-counters PKTS BYTES     set the counter during insert/append
--version   -V                print package version.


When using the -i or -o to define the physical interfaces, remember that by default:
vlan0 is the 4 LAN ports
vlan1 is the WAN port
eth1 is the WIFI
br0 is a bridge connecting the 4 LAN and the WIFI together 155123585123709614906219


I think examples are the best way to demonstrate the use of iptables.

First I want to view the rules on my INPUT chain, this is the first chain traffic coming into my router will hit.

.# iptables -L INPUT

Then I might want to add a rule so that I can ssh into my router from a specific host/address outside. So I might type the following:

.# iptables -A INPUT -p tcp -s 150.100.whatever.something --dport 22 -j logaccept

So I am saying: Append to the INPUT chain a rule allowing protocol tcp, with a source of <my external IP that i want access from> traffic destined for port 22 on my router, jump to logaccept. I could have used -j ACCEPT which simply jumps to ACCEPT, but in this case I want to log it just to keep track so I use logaccept, which is a chain we have set up for this purpose.

But why doesn't it work?

Now if I type

.# iptables -L INPUT 

I see my shiny new rule appended to the INPUT chain which is no good because in my case I have a rule blocking this traffic which occurs BEFORE the rule allowing it.

How do I change it? Simple.

First let's delete the rule we just made

.# iptables -L INPUT --line-numbers

will list the rules with their rule numbers. Let's say our rule is number 11

.# iptables -D INPUT 11   

Clearly this Deletes rule number 11 from the input chain.

Now instead of Appending I am going to Insert my rule into the number 1 position.

.# iptables -I INPUT -p tcp -s 150.100.whatever.something --dport 22 -j logaccept

So now rule number 1 is my new rule and the other rules have all shifted down a position.

If I wanted to change the IP address or any other aspect of my ssh rule I could use the -R (Replace) option and simply type in the new rule, ie

.# iptables -R INPUT 1 -p tcp -s --dport 22 -j ACCEPT

This would replace rule number 1 on the INPUT chain with the new rule which has a new source IP address and jumps to ACCEPT instead of logaccept.

One more example: I want to run a mini web server on my router. Let's assume that it is already running on port 8000 and I can access it from the LAN side, but not from the WAN side. With

.# iptables -I INPUT -p tcp -d --dport 8000 -j logaccept

the port 8000 will be opened. But I also have to setup NAT PREROUTING, so that the kernel forwards all packets on port 8000 from the outside to itsself,

.# iptables -t nat -R PREROUTING 1 -p tcp -d $(nvram get wan_ipaddr) --dport 8000 -j DNAT --to

But there's a better way to achieve this: just go to the Port Forwarding page and forward port 8000/TCP to address 118820165189844928479538

External Resources