ThisCalculator

Everyday

iptables Firewall Setup for Server-to-Server Traffic

Updated · Standard formula, shown below

This guide walks you through setting up an iptables firewall to control traffic between two or more servers. You’ll learn how to restrict access to specific ports and IP addresses, allowing only the connections you intend. This is essential for securing internal services like databases, APIs, or backup systems from unauthorized access.

What You’ll Need

  • Root or sudo access to the server(s) where you’ll apply the rules.
  • iptables installed (it’s preinstalled on most Linux distributions).
  • A clear list of which services (ports) need to be open and which source IPs should be allowed.
  • Knowledge of the network interfaces (e.g., eth0, ens3) used for traffic between the servers.

Step-by-Step

  1. Check current iptables rules
    Before making changes, see what’s already in place. Run sudo iptables -L -n -v to list all rules with line numbers. This shows existing rules and helps you avoid conflicts.
  2. Set default policies
    Decide the default behavior for traffic that doesn’t match any rule. For a secure setup, set the default policy to DROP for INPUT and FORWARD chains, and ACCEPT for OUTPUT. Use these commands:
    sudo iptables -P INPUT DROP
    sudo iptables -P FORWARD DROP
    sudo iptables -P OUTPUT ACCEPT
    This means all incoming and forwarded packets are blocked unless explicitly allowed.
  3. Allow established and related connections
    To avoid breaking existing connections, allow established and related traffic. This is crucial for responses to outgoing requests (like SSH from your workstation).
    sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
  4. Allow loopback traffic
    Local processes communicate via the loopback interface (lo). Allow it to prevent issues with local services.
    sudo iptables -A INPUT -i lo -j ACCEPT
  5. Add rules for specific server-to-server services
    For each service you want to expose between servers, add a rule that allows traffic from the specific source IP (or subnet) to the destination port. For example, to allow MySQL (port 3306) from server B (192.168.1.20) to server A (your current server):
    sudo iptables -A INPUT -p tcp --dport 3306 -s 192.168.1.20 -j ACCEPT
    Repeat this for each service and source IP. If you need to allow a range, use CIDR notation like 192.168.1.0/24.
  6. Allow SSH (optional but recommended)
    If you manage the server remotely, make sure SSH (port 22) is allowed from your admin IP(s). For example:
    sudo iptables -A INPUT -p tcp --dport 22 -s 203.0.113.5 -j ACCEPT
    If you skip this and your default policy is DROP, you’ll lock yourself out.
  7. Review and test the rules
    After adding rules, list them again: sudo iptables -L -n -v. Verify that the rules appear as expected. Test connectivity from the other server(s) to ensure the intended services are reachable and that unauthorized traffic is blocked.
  8. Make rules persistent
    iptables rules are lost on reboot unless saved. Use the appropriate method for your distribution:
    On Ubuntu/Debian: sudo apt install iptables-persistent then sudo netfilter-persistent save
    On CentOS/RHEL: sudo service iptables save (or sudo iptables-save > /etc/sysconfig/iptables)
    Alternatively, you can use iptables-save and restore on boot via a script.

Common Mistakes

  • Locking yourself out: Forgetting to allow SSH before setting the default policy to DROP. Always add an SSH rule first.
  • Using broad source IPs: Allowing traffic from any IP (e.g., -s 0.0.0.0/0) defeats the purpose. Be specific.
  • Forgetting to save rules: After a reboot, all rules vanish. Always save after testing.
  • Blocking established traffic: Without the conntrack rule, responses to your outgoing connections may be dropped, causing timeouts.
  • Ignoring IPv6: iptables only affects IPv4. If your servers use IPv6, you need to configure ip6tables separately.

Tips

  • Use a script: Write your rules in a shell script for easy reapplication and version control.
  • Comment your rules: Add comments using -m comment --comment "description" to make future maintenance easier.
  • Test in a staging environment: Before applying to production, test the rules on a non-critical server.
  • Monitor logs: Use sudo iptables -A INPUT -j LOG --log-prefix "IPTables-Dropped: " (placed before the DROP policy) to log dropped packets. This helps in troubleshooting.
  • Keep rules minimal: The fewer rules, the easier to manage. Only allow what’s necessary.

Troubleshooting

  • Cannot connect to the service: Check if the rule is present: sudo iptables -L -n -v | grep port. Verify the source IP is correct. Check if the service is listening on the expected interface (e.g., ss -tlnp).
  • SSH connection drops after applying rules: You likely didn’t allow established connections. Reconnect via console (if available) and add the conntrack rule.
  • Rules disappear after reboot: Reinstall iptables-persistent or re-save the rules. Check that the save command ran without errors.
  • Traffic from allowed IP still blocked: Ensure there’s no earlier rule that drops it. Rules are evaluated in order; the first match wins. Check for any DROP rules before your ACCEPT rule.
  • IPv6 traffic not filtered: Use ip6tables for IPv6 rules. If you don’t need IPv6, consider disabling it or setting default policy to DROP.

Frequently Asked Questions

Q: How do I allow a range of IPs?

Use CIDR notation in the source, e.g., -s 192.168.1.0/24 allows the entire 192.168.1.x subnet.

Q: Can I use hostnames instead of IPs in rules?

Yes, but it’s not recommended. iptables resolves hostnames at rule insertion time, and if DNS changes, the rule remains with the old IP. Use IPs for reliability.

Q: What is the difference between INPUT and FORWARD chains?

INPUT handles packets destined for the local server. FORWARD handles packets that pass through the server to another destination (routing). For server-to-server traffic, you’ll mostly use INPUT.

Q: How do I delete a rule?

First list rules with line numbers: sudo iptables -L --line-numbers. Then delete by number: sudo iptables -D INPUT 3 (deletes rule 3 in INPUT).

Q: Will these rules affect the server’s ability to make outgoing connections?

No, the OUTPUT chain is set to ACCEPT, so outgoing connections are allowed. Only incoming traffic is filtered.

Q: Is it safe to set the default policy to DROP?

Yes, it’s a security best practice. However, you must ensure all necessary traffic is explicitly allowed, or you’ll break services.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.