Forward Traffic with firewalld and iptables on AlmaLinux 9 / Rocky Linux 9
Pass traffic from an old server to a new one during a migration, using firewalld or nftables on AlmaLinux 9 and Rocky Linux 9, with the iptables equivalents.
When you move a service to a new server, you can make the old server pass incoming connections on to the new one while DNS changes spread. This guide sets that up on AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9 with firewalld, shows the same thing in native nftables, and ends with the classic iptables commands for readers who arrived looking for them. The original version of this procedure was written for CentOS 6, which has been end-of-life since November 2020; the RHEL family now uses firewalld on top of nftables by default.
The examples use 198.51.100.10 for the old server that receives the traffic and 203.0.113.20 for the new server it is forwarded to.
How the forwarding works
Three things have to happen on the old server:
- The kernel must be allowed to route packets between hosts (IP forwarding).
- A destination NAT (DNAT) rule rewrites the destination of incoming packets to the new server's address.
- A masquerade (source NAT) rule rewrites the source to the old server's own address, so that the new server sends its replies back through the old server instead of straight to the client, where they would be discarded.
One side effect of the third point: the new server sees every forwarded connection as coming from 198.51.100.10. Access logs and per-address rate limits there will not show real client addresses for forwarded traffic.
Requirements
- A user with
sudorights on the old server. - The service already running on the new server, with its firewall accepting connections from the old server's address on the forwarded ports.
- The ports and protocols to forward. The examples forward TCP 80 and 443.
Method 1: firewalld (default on the RHEL 9 family)
Confirm firewalld is active and note the zone your public interface belongs to (normally public):
sudo firewall-cmd --state
sudo firewall-cmd --get-active-zones
Add one forward rule per port and enable masquerading in that zone:
sudo firewall-cmd --zone=public --add-forward-port=port=80:proto=tcp:toaddr=203.0.113.20
sudo firewall-cmd --zone=public --add-forward-port=port=443:proto=tcp:toaddr=203.0.113.20
sudo firewall-cmd --zone=public --add-masquerade
You do not need to edit sysctl.conf: firewalld turns on IP forwarding itself when a forward rule has a toaddr or when masquerading is enabled.
Variations on the rule:
- Port range:
port=8000-8010:proto=tcp:toaddr=203.0.113.20 - UDP: replace
proto=tcpwithproto=udp. - Different port on the new server: add
toport, for exampleport=8080:proto=tcp:toport=80:toaddr=203.0.113.20.
These rules are in the runtime configuration only. Test them (see "Verify" below), then make them permanent:
sudo firewall-cmd --runtime-to-permanent
If a web server on the old machine is still listening on the forwarded ports, that does not matter: the destination is rewritten before the packet reaches local services. You can stop the local service once you are satisfied.
The --add-forward-port and --add-masquerade options handle IPv4. For IPv6, firewalld expects a rich rule with family="ipv6" and a forward-port element instead.
Method 2: native nftables
Use this method if the server runs the nftables service instead of firewalld, or on Debian 12 and Ubuntu 24.04 systems that use nftables directly. Do not combine it with firewalld on the same machine.
Enable IP forwarding permanently:
echo 'net.ipv4.ip_forward = 1' | sudo tee /etc/sysctl.d/99-ip-forward.conf
sudo sysctl --system
Create a NAT table with the two chains and add the rules:
sudo nft add table ip nat
sudo nft 'add chain ip nat prerouting { type nat hook prerouting priority -100; }'
sudo nft 'add chain ip nat postrouting { type nat hook postrouting priority 100; }'
sudo nft 'add rule ip nat prerouting ip daddr 198.51.100.10 tcp dport { 80, 443 } dnat to 203.0.113.20'
sudo nft 'add rule ip nat postrouting ip daddr 203.0.113.20 masquerade'
If your rule set has a forward chain with a drop policy, also accept the forwarded traffic there, for example:
sudo nft 'add rule inet filter forward ip daddr 203.0.113.20 tcp dport { 80, 443 } accept'
sudo nft 'add rule inet filter forward ct state established,related accept'
Adjust the table and chain names to your own rule set (sudo nft list ruleset shows them). Rules added on the command line are lost at reboot. Once they work, add the same table to the file your nftables service loads: /etc/sysconfig/nftables.conf on the RHEL family, /etc/nftables.conf on Debian and Ubuntu.
The classic iptables commands
These are the rules the CentOS 6 procedure used, written with the example addresses. Replace eth0 with your public interface name from ip -br addr:
sudo sysctl -w net.ipv4.ip_forward=1
sudo iptables -A FORWARD -d 203.0.113.20 -i eth0 -p tcp -m tcp --dport 80 -j ACCEPT
sudo iptables -t nat -A PREROUTING -d 198.51.100.10 -p tcp -m tcp --dport 80 -j DNAT --to-destination 203.0.113.20
sudo iptables -t nat -A POSTROUTING -o eth0 -d 203.0.113.20 -j MASQUERADE
On AlmaLinux 9 and Rocky Linux 9 the iptables command comes from the deprecated iptables-nft package and stores these rules in nftables. They work for a quick test, but they are not saved across reboots, there is no service iptables save on a default install, and they must not be mixed with a running firewalld. For anything you intend to keep, use method 1 or 2. iptables-translate followed by the rule prints its nftables form if you want to convert an existing set.
Verify
On the old server, check that the rules and forwarding are in place:
sudo firewall-cmd --zone=public --list-all
sysctl net.ipv4.ip_forward
With firewalld, the output lists your forward-ports and masquerade: yes. With nftables, use sudo nft list table ip nat instead.
From a third machine (not either server), request the old address and confirm the new server answers:
curl -I http://198.51.100.10/
On the new server, the access log should show the request arriving from 198.51.100.10.
Troubleshooting
- Connections time out: the new server's firewall is probably not accepting the old server's address, or masquerading is missing so replies bypass the old server.
- Testing from the old server itself shows no forwarding: that is expected. The forward rule applies to packets arriving from the network, not to connections the old server opens locally.
- Works until reboot: the rules were not saved. Run
sudo firewall-cmd --runtime-to-permanent, or add the nftables rules to the service's configuration file. - Your provider filters traffic: some networks drop packets that a server forwards rather than originates. Masquerading normally avoids this because packets leave with the old server's own address.
Removing the forward
When DNS has moved and the old address no longer receives traffic, remove the rules:
sudo firewall-cmd --permanent --zone=public --remove-forward-port=port=80:proto=tcp:toaddr=203.0.113.20
sudo firewall-cmd --permanent --zone=public --remove-forward-port=port=443:proto=tcp:toaddr=203.0.113.20
sudo firewall-cmd --permanent --zone=public --remove-masquerade
sudo firewall-cmd --reload
Frequently asked questions
Can the new server see the visitor's real IP address?
Not with this method, because masquerading replaces the source address. If you need real client addresses for HTTP or HTTPS, run a reverse proxy such as Nginx or HAProxy on the old server instead; it can pass the address on in an X-Forwarded-For header or with the PROXY protocol.
Does forwarding HTTPS this way need certificates on the old server?
No. The packets are passed through unchanged at the network layer, so TLS is still terminated by the new server with its own certificate.
Is this a good permanent setup?
It is meant as a bridge during a migration. Every connection takes an extra hop, depends on two servers being up, and uses bandwidth on both. Point DNS at the new address and remove the forward once traffic to the old address stops.
Can I forward to a server at a different provider?
Yes. The destination only has to be reachable from the old server over IP; it does not need to be on the same network.