Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hi I'm using Ubuntu 9.04 on a VPS. I'm getting an error if I apply a iptables rule. Here is what I have done.

1.Saved the existing rules

iptables-save > /etc/iptables.up.rules

Created iptables.test.rules and add some rules to it

nano /etc/iptables.test.rulesnano /etc/iptables.test.rules

This is the rules I added


 #  Allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT -i ! lo -d -j REJECT

#  Accepts all established inbound connections

#  Allows all outbound traffic
#  You can modify this to only allow certain traffic

# Allows HTTP and HTTPS connections from anywhere (the normal ports for websites)
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT

#  Allows SSH connections
-A INPUT -p tcp -m state --state NEW --dport 22- j ACCEPT

# Allow ping
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

# log iptables denied calls
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

# Reject all other inbound - default deny unless explicitly allowed policy


After editing when I try to apply the rules by

iptables-restore < /etc/iptables.test.rules

I get the following error

iptables-restore: line 42 failed

Line 42 is COMMIT and I comment that out I get

iptables-restore: COMMIT expected at line 43

I'm not sure what is the problem, it is expecting COMMIT but if COMMIT is there it's giving error. Could it be due to the fact i'm usin a VPS?My provider using OpenVZ for virtualizaton.

share|improve this question
I tried entering each line of the above script manually at the prompt. Most of them resulted in this following error "iptables: No chain/target/match by that name" – John Franic Jan 9 '10 at 4:42

Due to the way iptables-restore works, almost all errors will be reported as being at the COMMIT point. On the odd occasion I have these errors, I go putting COMMITs after each significant line (or, if I'm feeling suspicious, after just the lines I think might be the problem) and seeing which one barfs.

However, a brief inspection of your rules indicates this is your likely problem:

-A INPUT -p tcp -m state --state NEW --dport 22-j ACCEPT

The lack of a space between the 22 and the -j is probably the cause of the difficulty. "Attention to detail fail", as the cool kids say.

EDIT: With the added information, I'm going to go out on a limb and say that it's OpenVZ's problem (your VPS provider hasn't given you any iptables quota to add your own rules). I'd find a new VPS provider anyway, myself; VZ is like the Fisher Price toy of virtualisation. It has it's place, in the corporate data centre and the $0.89/decade "price-sensitive" end of the market, but for professional VPS hosting it's an absolute dog.

share|improve this answer
I'm sorry that space I accidentally put while typing here. This is the script I used as reference. – John Franic Jan 9 '10 at 4:37
I tried adding the rules to iptables directly at the prompt. When I ran the second line i.e iptables -A INPUT -i ! lo -d -j REJECT I got the following error "iptables: No chain/target/match by that name" – John Franic Jan 9 '10 at 4:39
Did you seriously type that whole thing out by hand? I'm speechless. Who knows how many extra typos you added (I notice that your SSH port typo has morphed). Given that what's in that URL isn't anything like your script, I'd ask that you put exactly what you've got into your question so that we can actually help you. Oh, and you might want to escape that ! on the command line. Bash gets shirty otherwise. – womble Jan 9 '10 at 6:51
No I didn't type the whole thing, I made some extra spaces while only putting the thing up in, i actually downloaded the script using wget and then made some changes. But can you tell me why this line "iptables -A INPUT -i ! lo -d -j REJECT" is giving the error "No chain/target/match by that name" – John Franic Jan 9 '10 at 8:30
Because you're using an OpenVZ VPS, as per my edited answer. – womble Jan 9 '10 at 9:53

Perhaps it's failing due to the space character you have before COMMIT?

share|improve this answer
Nah it's not that, the space accidentally came when I entered the code here.What else could this be due to? – John Franic Jan 8 '10 at 23:54
How do you manage to get a space in the middle of a block of pasted code? If what's in your question isn't what you really have, then edit your question to make it match reality. – womble Jan 9 '10 at 2:26

I know this is an old post, but it could solve someones problem.

I had the same error, and it was because I have a typo.

The error that was thrown was in "COMMIT", the last line, but in fact was this one:

-A RH-Firewall-1-INPUT -p tcp -m udp --dport 161 -j ACCEPT

The error was that it says in the same line "tcp" and then "udp". So changing that line for:

-A RH-Firewall-1-INPUT -p udp -m udp --dport 161 -j ACCEPT

Solved my problem

share|improve this answer

Very old post at this point, but its the top google result so I thought I would update with my solution...

There must be a blank line after COMMIT or iptables-restore will fail with a "no command specified" error at the line COMMIT is on.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.