I have just began using iptables. I have a server with a public ip address (A.A.A.A) wich is hosting a Virtual Machine (B.B.B.B) on local network.

I want to redirect all inbound traffic on the port 80 and 443 from my public address (A.A.A.A) to my virtual machine (B.B.B.B).

what is the rule to make that working.

Thank you.


This feature is called DNAT. And the network architecture you are trying to build is called DMZ. See more in Oscar's Anderson's IPTABLES HOWTO. The short answer is :

iptables -t nat -A PREROUTING -p tcp \
-d ${MY_EXTERNAL_IP} --dport 80 -j DNAT \
--to-destination ${MY_INTERNAL_HOST_IP}

iptables -t nat -A PREROUTING -p tcp \
    -d ${MY_EXTERNAL_IP} --dport 443 -j DNAT \
    --to-destination ${MY_INTERNAL_HOST_IP}
| improve this answer | |
  • 1
    The answer is incomplete. You need a return path, using either SNAT (preferred) or MASQUERADE in a POSTROUTING step. You also need to be sure that /proc/sys/net/ipv4/ip_forward is 1. – Doug Smythies Dec 11 '15 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.