• Basic iptables Configuration

    It’s always a good idea to setup a local firewall on hosts that are on unprotected networks. The internet “winds” blow harder and harder each day, and it’s only a matter of time before some daemon has an exploit that gets taken advantage of. I use CentOS 5 for all my web servers, and here is an example of the script I use to create a DEFAULT TO DENY set of firewall rules. This script generates a file called iptables in /etc/sysconfig.

    I used to create a special rule for MySQL that only allowed connections from my own network, but lately I have been omitting this rule and tunneling the connection through ssh instead. That is why it is commented out in the script below.


    ### SCRIPT ###
    #!/bin/sh
    # Drop all incoming traffic
    /sbin/iptables -P INPUT DROP
    # Drop all forwarded traffic
    /sbin/iptables -P FORWARD DROP
    # Allow all outgoing traffic
    /sbin/iptables -P OUTPUT ACCEPT
    # Allow returning packets
    /sbin/iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
    # Allow incoming traffic on port 80 for web server
    /sbin/iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
    # Allow MySQL only from a certain network
    /sbin/iptables -A INPUT -p tcp -m tcp -s XXX.XXX.XXX.0/24 --dport 3306 -j ACCEPT
    # Allow local traffic
    /sbin/iptables -A INPUT -i lo -j ACCEPT
    # Allow incoming SSH on port 22
    #/sbin/iptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
    # Allow ping
    /sbin/iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
    /sbin/iptables-save > /etc/sysconfig/iptables
    chmod go-r /etc/sysconfig/iptables
    /sbin/sudo service iptables restart
    ### /SCRIPT ###

    Here is what /etc/sysconfig/iptables looks like after running this script:


    # Generated by iptables-save v1.3.5 on Wed Dec 31 13:47:40 2008
    *filter
    :INPUT DROP [0:0]
    :FORWARD DROP [0:0]
    :OUTPUT ACCEPT [12:8972]
    -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
    -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
    -A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT
    -A INPUT -i lo -j ACCEPT
    -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
    -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
    -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
    -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
    -A INPUT -i lo -j ACCEPT
    -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
    COMMIT
    # Completed on Wed Dec 31 13:47:40 2008

    After you are done, make sure you have iptables setup to start when the system boots.


    # /sbin/chkconfig --list | grep iptables

    iptables       	0:off	1:off	2:on	3:on	4:on	5:on	6:off

    If it’s not on, just run:

    /sbin/chkconfig iptables on

    This entry was posted on Wednesday, December 31st, 2008 at 8:05 pm and is filed under Data and Technology. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
  • 2 Comments

    Take a look at some of the responses we've had to this article.

    1. cybervegan
      Jun 12th
      Reply

      Your script would cut off SSH on port 22, where it says:

      # Allow incoming SSH on port 22
      #/sbin/iptables -A INPUT -p tcp -m tcp –dport 22 -j ACCEPT

      In your article, you said that you had the MySQL port rule commented out, but that seems to be still active:

      # Allow MySQL only from a certain network
      /sbin/iptables -A INPUT -p tcp -m tcp -s XXX.XXX.XXX.0/24 –dport 3306 -j ACCEPT

      Did you comment out the wrong line?

    2. [...] on Solaris 10 sonia hamilton – life on the digital bikepath – sonia@snowfrog.net and Basic iptables Configuration|spiralbound.net [...]

  • Leave a Reply

    Let us know what you thought.

  • Name (required):

    Email (required):

    Website:

    Message:

Visitors have tagged this post: iptables allow ping (78) - centos iptables config (63) - iptables configuration (52) - iptables allow port (45) - basic iptables config (41) - iptables port 80 (38) - iptables port forwarding (34) - centos iptables configuration (33) - iptables drop port (31) - port forwarding iptables (26) - iptables block port (25) - iptables configurator (23) - centos iptables apache (23) - iptables port forward (22) - basic iptables configuration (20) - iptables add port (19) - iptables forward port (18) - iptables allow mysql (17) - iptables enable ping (17) - rhel iptables config (17) - /sbin/iptables (17) - iptables basic config (16) - iptables mysql (15) - iptables ALLOW SSH (15) - centos apache iptables (14) - iptables allow port 80 (13) - mysql iptables (13) - iptables allow icmp (13) - rhel iptables (12) - iptables rhel (12) - iptables forward port 80 (12) - allow port iptables (12) - iptables port forwarding howto (11) - iptables basic setup (11) - iptables ping port (11) - iptables (10) - basic iptables (10) - iptables add port 80 (10) - add port to iptables (10) - iptables allow outgoing (10) - edit iptables CentOS (10) - iptables apache centos (10) - iptables config (10) - iptables lamp (10) - iptables examples (10) - iptables accept ping (9) - iptables port forwarding example (9) - iptables basic configuration (9) - centos iptables script (9) - allow ping iptables (9) - iptables ping allow (9) - basic iptables setup (8) - Iptables basic (8) - centos iptables mysql (8) - iptables oracle (8) - iptables forward (8) - iptables ping forward (8) - iptables forward ping (8) - iptables ping (8) - iptables apache ssh (8) - iptables forward icmp (8) - centos 5 iptables (8) - iptables allow 80 (7) - iptables 3306 (7) - iptables allow outgoing traffic (7) - iptables apache2 (7) - portforward iptables (7) - apache2 iptables (7) - iptables allow outgoing port (7) - iptables allow only web (7) - iptables forward drop (7) - iptables setup (7) - enable ping iptables (7) - iptables enable port 80 (6) - port forward iptables (6) - iptables forward 80 (6) - iptable centos (6) - centos configure iptables (6) - iptables drop all (6) - add port 80 to iptables (6) - apache iptables (6) - iptables port deny (6) - iptables enable port (6) - iptables allow all (6) - centos iptables example (6) - centos iptables howto (6) - iptables sample (6) - centos iptables setup (6) - iptables block a port (6) - iptables accept port 80 (6) - centos iptable config (5) - iptables allow all local traffic (5) - iptables allow incoming ssh (5) - iptables on CentOS (5) - iptables accept (5) - iptables forward all (5) - iptables centos config (5) - iptables accept port (5) - iptables port forward example (5) - drop port iptables (5) - iptables apache (5) - save iptables configuration (5) - iptables sample configuration (5) - iptables forward examples (5) - centos iptables configure (5) - iptables icmp forward (5) - configure iptables for apache (5) - iptables block icmp (5) - forward sur iptables (5) - iptables allow ip (5) - iptables allow outgoing ping (5) - iptables in centos (5) - centos iptable (5) - iptable allow ping (5) - iptables block forward (5) - sbin/iptables (5) - iptables forward 3306 (4) - port forwarding in centos (4) - centos iptables port 80 (4) - iptables allow 3306 (4) - iptables port forwarding ssh (4) - adding port to iptables (4) - iptables allow local (4) - iptables port forward howto (4) - iptables block tcp (4) - centos 5 iptables config (4) - rhel 5 iptables (4) - centos edit iptables (4) - iptables allow incoming port (4) - iptables block outgoing traffic (4) - ping iptables (4) - iptable sample (4) - iptable port forwarding (4) - iptables allow all outgoing traffic (4) - enabled the mysql port in iptables (4) - iptables-config centos firewall (4) - block port iptables (4) - iptable allow port (4) - iptables port allow (4) - iptables port forwarding mysql (4) - ping port iptables (4) - iptables forward example (4) - iptables allow all outgoing (4) - mysql iptables forward (4) - forward port iptables (4) - centos iptables lamp (4) - iptables block forwarding (4) - iptables port forwarding howto -nat (4) - iptables config example (4) - iptables add a port (4) -