I have a virtualization server with one IP and 5 VM. To get internet access and ssh access in every VM I do this in


auto vmbr2
iface vmbr2 inet static
bridge_ports none
bridge_stp off
bridge_fd 0
post-up echo 1 > /proc/sys/net/ipv4/ip_forward
post-up iptables -t nat -A POSTROUTING -s '' -o vmbr0 -j MASQUERADE
post-down iptables -t nat -D POSTROUTING -s '' -o vmbr0 -j MASQUERADE

post-up iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 1022 -j DNAT --to 
post-down iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 1022 -j DNAT --to

This is ok. Now I need set a different external proxy to every VM. For example: VM used this proxy user:pass@ to access the internet.

How can I do it? Is it possible using only iptables?

1 Answer 1


Please clarify - what do you want.

At first - iptables work only with packets. It can't set user and password for proxy.

But you can set redirect traffic from each VM(it's ipadress) to each own proxy server. I think iptables -t nat -A PREROUTING -i eth0 -s vm_ipaddress -p tcp —dport 80 -j DNAT —to proxy_ipaddress:port should help you.

  • thanks for your answer, I need to set one proxy per VM. I have external (another isp) proxies configured with squid. I would like to avoid implement this into each VM's OS. How can I do that using iptables or any service?
    – erama
    Aug 27, 2015 at 0:10
  • If you need separate proxy per VM, you can do it on host machine, as described below. May be you may want to write automation script like * read proxy from any source(file, database, etc) * read VM ip addresses from VM API. * create iptables rule: foreach vm_ip do: set_proxy proxy_ip:port
    – BaZZiliO
    Aug 27, 2015 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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