<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.zhouba.cz/index.php?action=history&amp;feed=atom&amp;title=ZhoubaWiki%3AIpTablesPortForwarding</id>
	<title>ZhoubaWiki:IpTablesPortForwarding - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.zhouba.cz/index.php?action=history&amp;feed=atom&amp;title=ZhoubaWiki%3AIpTablesPortForwarding"/>
	<link rel="alternate" type="text/html" href="https://wiki.zhouba.cz/index.php?title=ZhoubaWiki:IpTablesPortForwarding&amp;action=history"/>
	<updated>2026-04-04T06:55:43Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.2</generator>
	<entry>
		<id>https://wiki.zhouba.cz/index.php?title=ZhoubaWiki:IpTablesPortForwarding&amp;diff=18&amp;oldid=prev</id>
		<title>Garak: Created page with &quot;While doing a server migration, it happens that some traffic still go to the old machine because the DNS servers are not yet synced or simply because some people are using the...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.zhouba.cz/index.php?title=ZhoubaWiki:IpTablesPortForwarding&amp;diff=18&amp;oldid=prev"/>
		<updated>2021-05-22T16:31:03Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;While doing a server migration, it happens that some traffic still go to the old machine because the DNS servers are not yet synced or simply because some people are using the...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;While doing a server migration, it happens that some traffic still go to the old machine because the DNS servers are not yet synced or simply because some people are using the IP address instead of the domain name....&lt;br /&gt;
&lt;br /&gt;
By using iptables and its masquerade feature, it is possible to forward all traffic to the old server to the new IP.&lt;br /&gt;
&lt;br /&gt;
This tutorial will show which command lines are required to make this possible.&lt;br /&gt;
&lt;br /&gt;
In this article, it is assumed that you do not have iptables running, or at least no nat table rules for chain PREROUTING and POSTROUTING.&lt;br /&gt;
&lt;br /&gt;
The first thing to do is do enable IP forwarding. This is done either by using:&lt;br /&gt;
&lt;br /&gt;
  echo &amp;quot;1&amp;quot; &amp;gt; /proc/sys/net/ipv4/ip_forward&lt;br /&gt;
&lt;br /&gt;
or &lt;br /&gt;
&lt;br /&gt;
  sysctl net.ipv4.ip_forward=1&lt;br /&gt;
&lt;br /&gt;
Then, we will add a rule telling to forward the traffic on port 1111 to ip 2.2.2.2 on port 1111:&lt;br /&gt;
&lt;br /&gt;
  iptables -t nat -A PREROUTING -p tcp --dport 1111 -j DNAT --to-destination 2.2.2.2:1111&lt;br /&gt;
&lt;br /&gt;
and finally, we ask IPtables to masquerade:&lt;br /&gt;
&lt;br /&gt;
  iptables -t nat -A POSTROUTING -j MASQUERADE&lt;br /&gt;
&lt;br /&gt;
Optionally, you could only redirect the traffic from a specific source/network with, for a host only:&lt;br /&gt;
&lt;br /&gt;
  iptables -t nat -A PREROUTING -s 192.168.1.1 -p tcp --dport 1111 -j DNAT --to-destination 2.2.2.2:1111&lt;br /&gt;
&lt;br /&gt;
or for a whole network&lt;br /&gt;
&lt;br /&gt;
  iptables -t nat -A PREROUTING -s 192.168.1.0/24 -p tcp --dport 1111 -j DNAT --to-destination 2.2.2.2:1111&lt;br /&gt;
&lt;br /&gt;
that's it, now the traffic to port 1111 will be redirected to IP 2.2.2.2 .&lt;br /&gt;
If you go on host 2.2.2.2, you should see a lot of traffic coming from the host doing the redirection.&lt;br /&gt;
&lt;br /&gt;
In order to disengage the port forwarding: this depends on each individual cases, but in a nutshell, you will need to run&lt;br /&gt;
&lt;br /&gt;
  iptables -t nat -L -n --line-numbers&lt;br /&gt;
&lt;br /&gt;
and find the line that matches the rules you want to deletes. then&lt;br /&gt;
&lt;br /&gt;
  iptables -t nat -D ...&lt;br /&gt;
&lt;br /&gt;
Like:&lt;br /&gt;
&lt;br /&gt;
  iptables -t nat -A PREROUTING -d 89.187.142.86 -p tcp --dport 80 -j DNAT --to-destination 185.18.28.37:80&lt;br /&gt;
  iptables -t nat -A PREROUTING -d 89.187.142.86 -p tcp --dport 443 -j DNAT --to-destination 185.18.28.37:443&lt;/div&gt;</summary>
		<author><name>Garak</name></author>
	</entry>
</feed>