iptables cheat sheet

iptables cheat sheet

Get Social!

Here are a few handy commands for using iptables. They are tailored for an OpenVZ container with a venet network interface but can easily be adapted to use your interface by replacing venet0 with your network interface.

For setting up iptables in an openVZ this blog post.

iptables console

Remove existing rules

You can easily delete all existing rules in iptables. Be careful using this command, there is no going back unless you have backed up your rules.

Backup and restore

Backup to file

Rules can easily be saved to an external file for backups or outputting for version control. This will save the rules to /etc/iptables.rules.

Restore from file

Saved settings can be restored with the following command:

Change the default policy

The default policy can be changed to specify what should happen to traffic which doesn’t have a rule to explicitly define what to do. You can specify to ACCEPT, REJECT or DROP for INPUT, FORWARD and OUTPUT.

Change OUTPUT to ACCEPT

Change INPUT to DROP

Apply the catchall rule

You can add a rule to the bottom of the rule book to choose what to do with traffic which doesn’t match any other rule. A common use would be to add a DROP as the last rule to drop any traffic which isn’t explicitly allowed by an earlier rule. You can specify to ACCEPT, REJECT or DROP for INPUT, FORWARD and OUTPUT as well as an interface.

ACCEPT INPUT on interface lo

BLOCK INPUT on interface venet0

List active rules

You can list the active rules with -L, and -v for information on packets affected.

Enable established connections rule

Already established connections will not be affected by adding this inbound rule. Traffic affected by other outbound rules will also be honoured. If you add this rule, you won’t likely need to specify inbound rules for many outbound rules.

Adding new rules

New rules can be added to control how traffic passes through an interface. If you have not used the Established connections rule then you will also need to add an outgoing rule. If you do use the Established connections rule then you will only need the top command in each of the below headings.

Add rule for port 80 – such as Apache

Add rule for port 22 – SSH outbound connections

Add rule for 53 – DNS outbound

Add rule for port 22 – SSH inbound connections

Add rule for outgoing http/ https

Add rule for ping from remote to local

 Add rule for ping from local to remote

Delete rule

The easiest way to remove a rule is to delete it by it’s rule position in the list. To find out the rules position run iptables -L with the line-number argument. The below command is to delete an INPUT command however you can easily change INPUT to OUTPUT as required.

Then run the delete command for the relevent direction. This will delete the 7th inbound rule.

Example deleting rule 1 for INPUT:

 

Let me know in the comments if you think anything is missing.


Related posts:


3 Comments

Daniel

11-Nov-2013 at 7:59 am

If relying on SSH to connect to your OpenVZ containter, wouldn’t you be locked out if you block input from your venet0 interface?

    james.coyle

    12-Nov-2013 at 7:54 am

    Yes, but if input was blocked you wouldn’t reach the OMS either.

Laurence

15-Apr-2015 at 10:49 am

Thank you for the cheatsheet, however, I suspect you’ve got redirection the wrong way with your restore code:

iptables-restore > /etc/iptables.rules

should be:

iptables-restore < /etc/iptables.rules

Leave a Reply

Visit our advertisers

Search

Quick Poll

Which type of virtualisation do you use?
  • Add your answer

Visit our advertisers