Difference between revisions of "Iptables"

From Leaky
Jump to: navigation, search
(Created page with 'To forward packets sent to one IP over to a different IP on a different server, you can use iptables. OLD_IP=92.48.119.131 NEW_IP=213.229.103.79 iptables -t nat -A PREROUTI…')
 
(updated syntax (-s ! $OLD_IP is deprecated))
Line 5: Line 5:
 
   
 
   
 
  iptables -t nat -A PREROUTING -d $OLD_IP -p tcp -m tcp --dport 80 -j DNAT --to-destination $NEW_IP:80  
 
  iptables -t nat -A PREROUTING -d $OLD_IP -p tcp -m tcp --dport 80 -j DNAT --to-destination $NEW_IP:80  
  iptables -t nat -A POSTROUTING -s ! $OLD_IP -d $NEW_IP -p tcp -m tcp --dport 80 -j SNAT --to-source $OLD_IP
+
  iptables -t nat -A POSTROUTING ! -s $OLD_IP -d $NEW_IP -p tcp -m tcp --dport 80 -j SNAT --to-source $OLD_IP
  
 
Blanket allow all packets being forwarded to the new IP.
 
Blanket allow all packets being forwarded to the new IP.

Revision as of 01:17, 2 August 2014

To forward packets sent to one IP over to a different IP on a different server, you can use iptables.

OLD_IP=92.48.119.131
NEW_IP=213.229.103.79

iptables -t nat -A PREROUTING -d $OLD_IP -p tcp -m tcp --dport 80 -j DNAT --to-destination $NEW_IP:80 
iptables -t nat -A POSTROUTING ! -s $OLD_IP -d $NEW_IP -p tcp -m tcp --dport 80 -j SNAT --to-source $OLD_IP

Blanket allow all packets being forwarded to the new IP.

iptables -A FORWARD -d $NEW_IP -j ACCEPT

Things to watch for if it doesn't work - IP forwarding needs to be enabled (sysctl -w net.ipv4.ip_forward=1) and you may need to allow the specific port in the INPUT chain. Not sure about this as I had already allowed port 80 prior to setting up the forwarding.