Welcome to Halil Demirezen's Tips and Tutorials Page
Personal
Home
Contact
Biography
Research

Projects
Tutorials
Celsus Library (TR)

Categories

LinuxNetworkComputer VisionDatabasesProgramming
vmware


IPTABLES: Redirecting incoming traffic to another host using IPTABLES NAT 2021-06-01 23:23:08

If you want to route the network traffic coming to an interface to a remote host, you need to do

Suppose that you have an IP address 192.168.1.1 on interface A
Suppose that you have an IP address 192.168.2.1 on interface B
And there is server running on 192.168.2.2 on port 3000

If you want to map the traffic coming to your 192.168.1.1:300 to 192.168.2.2:3000 and
let the 192.168.2.2 to see the requests as coming from 192.168.2.1

Step 1: Do DNAT first

iptables -t nat -A PREROUTING -p tcp -d 192.168.1.1 --dport 443 -j DNAT --to-destination 192.168.2.2:443


Step 2: And do masquerading

iptables -t nat -A POSTROUTING -j MASQUERADE




And of course the forwarding should be enabled on the server

sysctl -w net.ipv4.conf.all.forwarding=1