Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

This question already has an answer here:

I have a small script like this to configure the iptables:

#!/bin/bash

PRE_STR="iptables -t nat -A PREROUTING -p tcp -j DNAT"
FOR_STR="iptables -A FORWARD -p tcp -j ACCEPT"


#####################################
# instances
CM="10.0.1.137"
MASTER="10.0.1.149"
MYSQL="10.0.1.83"
REPORTING="10.0.1.85"

#####################################
# Clear Iptables
iptables -F
iptables -t nat -F

#####################################
# Forward to enable Internet on private nodes
iptables -t nat -A POSTROUTING -j MASQUERADE


#####################################
# Port forwarding

forward()
{
        $PRE_STR --dport $1 --to $2:$3
        $FOR_STR --dport $3 -d $2
}

#what   from    to ip           to port
forward 3222    $CM             22
forward 7183    $CM             7183
forward 7180    $CM             7180

forward 3122    $MASTER         22
forward 8888    $MASTER         8888
forward 11000   $MASTER         11000

forward 2122    $MYSQL          22
forward 13306   $MYSQL          3306

iptables-save > /etc/firewall.conf

The question is, how to load the /etc/firwall.conf with the current iptables settings on the next startup?

On a normal Debian machine I would put a script that fires iptables-restore < /etc/firewall.conf it into the folder /etc/network/if-up.d/iptables. But this isn't available in this image.

So what is the correct why to load this /etc/firewall.conf?

AMI ID: ami-1de2d969

Update:

Is it ok to fire it in iptables-restore < /etc/firewall.conf in /etc/rc.local?

Source: http://www.cyberciti.biz/faq/how-do-i-save-iptables-rules-or-settings/

share|improve this question

marked as duplicate by Ward, mdpc, kce, Magellan, Tim Brigham Aug 7 '13 at 14:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
This question comes up first for a Google query for "Amazon Linux AMI persist iptables", and it has two good answers that tell me exactly what I need to know. The question that this is a duplicate of has neither. Can we un-dupe this one? –  Dirk Groeneveld Aug 12 at 18:09

2 Answers 2

up vote 3 down vote accepted

Debian (and derivatives) use the iptables-persistent package for this task.

Define your rules in /etc/iptables/rules.4 and/or /etc/iptables/rules.6 and activate the service (using update-rc.d, chkconfig or your tool of choice.

On RHEL and derivatives, the startup script /etc/init.d/iptables reads /etc/sysconfig/iptables, so you need to define your rules there, and ensure that the iptables service is activated (chkconfig iptables on) and started (service iptables start).

share|improve this answer
    
Ty for this tipp. Do you know how to do it with Red Hat derivates as well? This Amazon VPC Image is based on red hat –  Christian Schneider Aug 5 '13 at 8:20
service iptables save

or

/etc/init.d/iptables save
share|improve this answer
    
Thats nice as well. Thanks :) –  Christian Schneider Aug 6 '13 at 8:09
    
did the trick on my EC2 instance! –  Sam Vloeberghs Feb 18 at 13:53

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