• 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 (167) - centos iptables config (144) - iptables block port (135) - iptables port 80 (120) - iptables allow port (113) - basic iptables config (86) - iptables port forwarding (77) - iptables drop port (73) - centos iptables configuration (70) - centos iptables apache (68) - iptables add port (68) - iptables configuration (65) - basic iptables configuration (42) - /sbin/iptables (40) - iptables mysql (39) - iptables port forward (36) - iptables apache centos (36) - iptables allow mysql (34) - iptables add port 80 (34) - iptables basic config (34) - iptables configurator (33) - iptables forward port (33) - port forwarding iptables (33) - rhel iptables config (33) - iptables allow port 80 (32) - iptables enable ping (32) - centos apache iptables (31) - iptables basic configuration (29) - edit iptables CentOS (29) - iptables ping allow (29) - iptables accept ping (28) - rhel iptables (27) - simple iptables configuration (27) - allow ping iptables (26) - add port to iptables (25) - iptables rhel (24) - iptables forward port 80 (24) - iptables allow outgoing traffic (23) - iptables drop all (22) - iptables ping port (22) - iptables allow icmp (22) - iptables allow outgoing (21) - allow port iptables (21) - iptables accept port 80 (21) - iptables accept port (20) - iptables lamp (19) - iptables examples (19) - iptables ALLOW SSH (19) - iptables port forwarding howto (17) - iptables 3306 (17) - mysql iptables (17) - iptables forward icmp (16) - iptables forward drop (16) - basic iptables (15) - iptables basic setup (15) - iptables allow outgoing port (15) - iptables ping forward (15) - iptables allow all outgoing traffic (15) - simple iptables config (15) - iptables allow 80 (14) - Iptables basic (14) - iptables port forwarding example (14) - centos iptables script (14) - iptables ping (14) - basic iptables setup (13) - iptables configuration example (13) - iptables port allow (13) - centos iptables mysql (12) - port forward iptables (12) - iptables (11) - iptables oracle (11) - iptables block port 80 (11) - portforward iptables (11) - iptables forward ping (11) - iptables forward all traffic (11) - iptables centos apache (11) - centos edit iptables (11) - iptables allow all (11) - iptables add port 8080 (11) - iptables config (11) - centos 5 iptables (11) - iptable allow port (11) - centos iptables setup (11) - enable ping iptables (11) - iptables block a port (11) - iptables port 80 accept (11) - iptables centos config (10) - iptables forward (10) - iptables allow local traffic (10) - iptables forward traffic (10) - iptables block forward (10) - sbin/iptables (10) - config iptables centos (10) - iptables simple config (10) - centos iptables port 80 (9) - iptables dport 80 (9) - lamp iptables (9) - centos 5 iptables config (9) - iptables apache ssh (9) - iptables allow outgoing ping (9) - iptables block outgoing port (9) - iptables enable port 80 (8) - iptables accept (8) - iptables apache2 (8) - apache iptables (8) - apache2 iptables (8) - iptables sample config (8) - iptables port forwarding apache (8) - iptables block portS (8) - allow ping in iptables (8) - centos iptables example (8) - centos iptables howto (8) - iptables sample (8) - iptables examples centos (8) - block port iptables (8) - centos iptables config file (8) - iptables forward example (8) - forward port iptables (8) - block port 80 iptables (8) - solaris 10 iptables (8) - iptables port 8080 (8) - simple iptables setup (8) - edit iptables (8) - centos iptable config (7) - iptables forward 80 (7) - iptable centos (7) - iptables allow 3306 (7) - iptables port forwarding ssh (7) - iptables port forward example (7) - centos configure iptables (7) - iptables allow local (7) - iptables apache (7) - add port 80 to iptables (7) - iptables block incoming traffic (7) - iptables 0/24 (7) - centos iptables configure (7) - add mysql to iptables (7) - iptables port deny (7) - iptables blocking port 80 (7) - iptables block icmp (7) - iptables allow only web (7) - iptables block outgoing traffic (7) - /sbin/iptables -F (7) - iptables INPUT FORWARD (7) - iptables add 80 (7) - iptable allow ping (7) - iptables block mysql (7) - iptables setup (7) - ping port iptables (7) - iptables allow all outgoing (7) -