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
- Check current IP forwarding status. Run
sysctl net.ipv4.ip_forward. If it returns0, forwarding is off. - Enable IP forwarding temporarily. Execute
sysctl -w net.ipv4.ip_forward=1. This takes effect immediately but resets on reboot. - Make IP forwarding permanent. Edit
/etc/sysctl.conf(or create/etc/sysctl.d/99-ipforward.conf) and add the linenet.ipv4.ip_forward = 1. Then runsysctl -pto apply without rebooting. - Verify the change. Run
sysctl net.ipv4.ip_forwardagain; it should now show1. - Identify your network interfaces. Use
ip link showorip addrto list interfaces. Note the names for your WAN (internet-facing) and LAN (private) connections. For this guide, we’ll useeth0for WAN andeth1for LAN. - Add masquerade rule to firewalld. Run
firewall-cmd --permanent --add-masquerade. This enables NAT on all interfaces. - 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. - 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. - Reload firewalld. Run
firewall-cmd --reloadto apply the changes. - 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 withping 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 -walone works until reboot; always add the line tosysctl.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 setsetsebool -P httpd_can_network_connect 1or 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.confto 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 7and 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
- Car Trends Making Cars Worse: 7 Downsides
- Nostalgic Classic Games Revamped on G2A?
- Nvidia RTX 4000 Super GPUs: Full Specs & Buying Guide
- Photoshop vs Lightroom AI Features Compared
- DNF vs DNF5: Which Package Manager?
- Korean SUV Making BMW and Audi Owners Question Their Choices?