dev-ops-challenges

IPtables Installation and Configuration

Technical Overview

What is a Firewall?

A firewall is a network security system that monitors, filters, and controls incoming and outgoing network traffic based on predetermined security rules. It acts as a barrier between a trusted internal network (e.g., your private subnet) and an untrusted external network (e.g., the internet), preventing unauthorized access and mitigating network-based attacks.


What is IPtables?

IPtables is a user-space command-line utility used to configure the IP packet filter rules of the Linux kernel firewall, implemented via the Netfilter framework. In Netfilter, packets passing through the network stack are inspected, altered, or routed based on rules grouped into tables and chains.

1. IPtables Tables

Tables group rules based on the type of packet processing decisions being made:

2. IPtables Chains

Each table contains built-in chains that represent checkpoints in the network stack where packets are processed:

3. Targets (Actions)

When a packet matches a rule, it is directed to a specific target:

4. Rule Ordering (Top-Down Evaluation)

IPtables rules inside a chain are evaluated sequentially from top to bottom. When a packet matches a rule, the designated action is applied immediately, and further rules in the chain are ignored.

[!IMPORTANT] Because rules are evaluated sequentially, you must place specific allow (ACCEPT) rules before broad deny (REJECT or DROP) rules. For example, if you place a generic block-all rule at the top, a later rule allowing a specific IP will never be reached.

This guide outlines the steps to install iptables, configure rules to restrict access to a web port so that only the Load Balancer IP can connect, and persist those settings across reboots.


Infrastructure & Configuration Requirements


Step-by-Step Implementation

Apply these configuration steps to each application server:

Step 1: Connect to the Application Server

SSH into the assigned application server from the Jump Host:

# Example for App Server 1
ssh tony@stapp01

Step 2: Install IPtables Services

CentOS/RHEL systems default to firewalld as the front-end manager. To use native iptables rules, install the service management package:

sudo yum install -y iptables-services

Start and enable the iptables service:

sudo systemctl enable --now iptables

Verify the service status:

sudo systemctl status iptables

Step 3: Configure Firewall Rules

Configure the rules sequentially in the INPUT chain of the default filter table:

  1. Allow incoming traffic from the Load Balancer IP to the application port:
    sudo iptables -A INPUT -p tcp -s 172.16.238.14 --dport 8083 -j ACCEPT
    
    • -A INPUT: Append to the INPUT chain.
    • -p tcp: Filter TCP packets.
    • -s 172.16.238.14: Match traffic originating from this source IP.
    • --dport 8083: Match traffic destined for this port.
    • -j ACCEPT: Jump to the ACCEPT target.
  2. Reject all other incoming traffic on the application port:
    sudo iptables -A INPUT -p tcp --dport 8083 -j REJECT
    
    • This rule ensures that any packet attempting to connect to port 8083 from any IP address other than the Load Balancer will be rejected.

Step 4: Persist the Rules Across Boots

By default, rules configured in memory will be lost when the server reboots. Save the active rule configuration to the system persistence file:

sudo service iptables save

Expected output:

iptables: Saving firewall rules to /etc/sysconfig/iptables: [  OK  ]

Inspect the saved file to ensure the rules are recorded:

sudo cat /etc/sysconfig/iptables

Post-Deployment Verification

1. Inspect Active Rules List

List the rules in the INPUT chain with numeric formats to verify ordering:

sudo iptables -L INPUT -n --line-numbers

Expected output snippet:

Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    ACCEPT     tcp  --  172.16.238.14        0.0.0.0/0            tcp dpt:8083
2    REJECT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:8083 reject-with icmp-port-unreachable

2. Verify Connectivity (Acceptance)

From the Load Balancer host, test connection to the App Server:

curl -I http://stapp01:8083

Expected output: Successful HTTP headers (200 OK).

3. Verify Connectivity (Rejection)

From any other node (e.g., the Jump Host or a different database server), try to connect to the port:

curl -I http://stapp01:8083

Expected output:

curl: (7) Failed connect to stapp01:8083; Connection refused

Log out of the Application Server:

exit