keepcalculator

06 Numbers and Powers

Enable IP Forwarding and Masquerading on CentOS

Card No. 06·83253 · Updated · Standard formula, shown below

This guide walks you through enabling IP forwarding and masquerading on CentOS, turning your machine into a router or NAT gateway. You’ll edit kernel parameters and firewall rules so traffic from one network interface is forwarded to another, with masquerading rewriting source addresses to make replies route back correctly. By the end, you’ll have a working gateway that lets devices on a private network reach the internet through your CentOS box.

What You’ll Need

  • A CentOS system (7, 8, or 9) with root or sudo access.
  • At least two network interfaces: one connected to the internet (e.g., eth0, ens33) and one to the private network (e.g., eth1, ens37).
  • Firewalld enabled and running (default on CentOS 7+).
  • Basic familiarity with the command line and editing files with vi/nano.

Step-by-Step

  1. Check current IP forwarding status. Run sysctl net.ipv4.ip_forward. If it returns 0, forwarding is off.
  2. Enable IP forwarding temporarily. Execute sysctl -w net.ipv4.ip_forward=1. This takes effect immediately but resets on reboot.
  3. Make IP forwarding permanent. Edit /etc/sysctl.conf (or create /etc/sysctl.d/99-ipforward.conf) and add the line net.ipv4.ip_forward = 1. Then run sysctl -p to apply without rebooting.
  4. Verify the change. Run sysctl net.ipv4.ip_forward again; it should now show 1.
  5. Identify your network interfaces. Use ip link show or ip addr to list interfaces. Note the names for your WAN (internet-facing) and LAN (private) connections. For this guide, we’ll use eth0 for WAN and eth1 for LAN.
  6. Add masquerade rule to firewalld. Run firewall-cmd --permanent --add-masquerade. This enables NAT on all interfaces.
  7. Restrict masquerade to the WAN interface (recommended). Masquerading on all interfaces can cause issues. Instead, add a rich rule: firewall-cmd --permanent --direct --add-rule ipv4 nat POSTROUTING 0 -o eth0 -j MASQUERADE. This tells the kernel to rewrite source addresses only for packets leaving eth0.
  8. Allow forwarding between interfaces. Add a firewall rule to permit traffic from the LAN to the WAN: firewall-cmd --permanent --direct --add-rule ipv4 filter FORWARD 0 -i eth1 -o eth0 -j ACCEPT. Also allow related return traffic: firewall-cmd --permanent --direct --add-rule ipv4 filter FORWARD 0 -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT.
  9. Reload firewalld. Run firewall-cmd --reload to apply the changes.
  10. Test the configuration. From a machine on the LAN, ping a public IP (e.g., ping 8.8.8.8). If it succeeds, forwarding and masquerading are working. Then test DNS with ping google.com; if that fails, you may need to configure DNS on the client or set up a DNS forwarder.

Common Mistakes

  • Forgetting to make forwarding permanent. Using sysctl -w alone works until reboot; always add the line to sysctl.conf.
  • Masquerading on the wrong interface. If you enable masquerade globally, packets from the WAN to the LAN get rewritten too, breaking routing. Always specify the output interface.
  • Not allowing forwarding in firewalld. Even with IP forwarding on, firewalld’s default policy drops forwarded packets. You must add explicit FORWARD rules.
  • Ignoring SELinux. On CentOS, SELinux might block forwarding. Check with getenforce; if enforcing, you may need to set setsebool -P httpd_can_network_connect 1 or adjust policy, though for basic NAT it usually works.
  • Clients not setting the gateway. Ensure each client’s default gateway points to the CentOS LAN IP.

Tips

  • Use a dedicated sysctl file like /etc/sysctl.d/99-custom.conf to keep your changes separate from defaults.
  • Test with a single client first before rolling out to many devices.
  • Enable logging on the FORWARD chain temporarily to debug: firewall-cmd --permanent --direct --add-rule ipv4 filter FORWARD 0 -j LOG --log-prefix "FORWARD: " --log-level 7 and check /var/log/messages.
  • If you have a dynamic WAN IP, masquerade handles it automatically; no need to update rules.
  • Document your interface names; they can change between reboots if using predictable naming.

Troubleshooting

Ping from LAN to internet fails. First, check sysctl net.ipv4.ip_forward returns 1. Then verify firewalld rules: firewall-cmd --direct --get-all-rules. Ensure the FORWARD rules are present. Test from the CentOS box itself to the internet to rule out WAN issues.

Clients can ping IPs but not domain names. DNS is not forwarded. Either set the client’s DNS to a public resolver (e.g., 8.8.8.8) or configure a DNS forwarder like dnsmasq on CentOS.

Masquerade not working. Confirm the rule is applied: firewall-cmd --list-all should show masquerade in the appropriate zone. If you used a direct rule, check with iptables -t nat -L POSTROUTING to see the rule.

Forwarding works but slow. Check for packet drops: ip -s link show eth1 shows errors. Also ensure the NIC drivers are up to date.

After reboot, forwarding stops. Verify the sysctl file is in place and that firewalld is enabled: systemctl enable firewalld and systemctl start firewalld.

SELinux blocks forwarding. If you see AVC denials in /var/log/audit/audit.log related to forwarding, temporarily set setenforce 0 to test. If it works, you’ll need to adjust SELinux policy or use setsebool to allow the specific operation.

Frequently Asked Questions

Q: Do I need to enable IP forwarding on the client machines too?
A: No, only the gateway (CentOS) needs it. Clients just send packets to their default gateway.

Q: Can I use this to share my CentOS’s internet connection with a private network?
A: Yes, that’s exactly what this setup does. The CentOS box acts as a NAT router.

Q: What’s the difference between IP forwarding and masquerading?
A: IP forwarding allows packets to pass between interfaces; masquerading rewrites the source IP to the gateway’s IP, enabling many devices to share one public IP.

Q: Will this work with IPv6?
A: This guide covers IPv4. IPv6 typically uses NDP and doesn’t need masquerading; you’d enable net.ipv6.conf.all.forwarding=1 and configure appropriate firewall rules.

Q: How do I remove the masquerade rule?
A: Use firewall-cmd --permanent --remove-masquerade or remove the direct rules with --direct --remove-rule.

Q: Is it safe to enable IP forwarding on a production server?
A> It’s safe if you have proper firewall rules. Without them, it could expose internal networks, so always restrict forwarding to specific interfaces.

Related

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

More cards in this drawer

The whole Numbers and Powers drawer →