Ubuntu – Redirect local traffic to proxy port with iptables

iptablesPROXYUbuntu

I have a Ubuntu 12.04 host that has a squid proxy on it running on port 8080. I want to proxy all the web traffic from the host through squid using iptables. Is the correct method:

iptables -t nat -I OUTPUT -p tcp -d 127.0.0.1 --dport 80 -j REDIRECT --to-ports 8080

This is not working. When I browse to a page it appears normally, even when the proxy is disabled. Any suggestions appreciated.

Best Answer

iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

In your rule, IPTables will only redirect traffic destined for localhost to the proxy. This rule will redirect any traffic destined for port 80. In your rule, change -d to -s and it shall work.

Related Topic