Skip to content

Instantly share code, notes, and snippets.

@dylanmtaylor
Created April 3, 2016 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dylanmtaylor/3e16c569e5ef1b91d1da70c2f283ea5f to your computer and use it in GitHub Desktop.
Save dylanmtaylor/3e16c569e5ef1b91d1da70c2f283ea5f to your computer and use it in GitHub Desktop.
How to forward requests from a host machine to a KVM guest and back using iptables
# echo 1 > /proc/sys/net/ipv4/ip_forward first
# 208.x.x.x is the host machine. 192.x.x.x is the guest machine
# This routes port 80 to the guest and back.
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.x.x.x6:80
iptables -t nat -A POSTROUTING -p tcp -d 192.x.x.x6 --dport 80 -j SNAT --to-source 208.x.x.x
# This routes port 2222 on the host to 22 on the guest machine
iptables -t nat -A PREROUTING -p tcp --dport 2222 -j DNAT --to-destination 192.x.x.x6:22
iptables -t nat -A POSTROUTING -p tcp -d 192.x.x.x6 --dport 22 -j SNAT --to-source 208.x.x.x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment