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.
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.
Tables group rules based on the type of packet processing decisions being made:
filter (Default): Used for standard packet filtering (deciding whether a packet should reach its destination).nat (Network Address Translation): Used to redirect packets or modify source/destination IP addresses and ports (e.g., port forwarding or masquerading).mangle: Used for specialized packet alteration (modifying IP header fields like TTL or TOS).raw: Used to configure exceptions for packet connection tracking (conntrack).Each table contains built-in chains that represent checkpoints in the network stack where packets are processed:
INPUT: Processes incoming packets destined for local sockets (entering the server).OUTPUT: Processes outgoing packets generated locally on the server and leaving.FORWARD: Processes packets that are routed through the server (not destined for the server itself, e.g., in a router or gateway).PREROUTING: Processes packets immediately as they arrive at the network interface (before routing decisions are made).POSTROUTING: Processes packets just before they leave the network interface (after routing decisions are made).When a packet matches a rule, it is directed to a specific target:
ACCEPT: Allows the packet to pass through.DROP: Silently discards the packet (the sender receives no response and timeouts occur).REJECT: Discards the packet and sends an error response back to the sender (e.g., an ICMP destination port unreachable message).LOG: Logs details about the matching packet to syslog for audit purposes, then passes the packet to the next rule.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 (REJECTorDROP) 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.
stapp01, stapp02, stapp03)tony, steve, banner)172.16.238.14 (or your designated LBR IP)8083 (or your designated Apache port)8083 from the Load Balancer. Reject all other traffic on this port.Apply these configuration steps to each application server:
SSH into the assigned application server from the Jump Host:
# Example for App Server 1
ssh tony@stapp01
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
Configure the rules sequentially in the INPUT chain of the default filter table:
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.sudo iptables -A INPUT -p tcp --dport 8083 -j REJECT
8083 from any IP address other than the Load Balancer will be rejected.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
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
From the Load Balancer host, test connection to the App Server:
curl -I http://stapp01:8083
Expected output: Successful HTTP headers (200 OK).
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