Saturday, March 13, 2010

How to setup a general IP proxy in linux

the networking features in Linux are really amazing , in this post I want to show you how you can setup a general IP proxy on a specific port.
in order to write the required script open a new file named proxy :

1 nano proxy

copy and paste the following script into the file :

1 YourIP=X.X.X.X
2 TargetIP=Y.Y.Y.Y
3 Port=Z
4
5 iptables -t nat -A PREROUTING -p tcp –dport $Port -d $YourIP -j DNAT –to $TargetIP
6 iptables -t nat -A POSTROUTING -p tcp –dport $Port -d $TargetIP -j SNAT –to $YourIP

Change X.X.X.X to your nic IP.
Change Y.Y.Y.Y to target IP.
and Change Z to the port which needs to be proxied.
save and close the file.
make it executable and run it :

1 chmod +x proxy
2 ./proxy

enable IPv4 forwarding in sysctl.conf :

1 nano /etc/sysctl.conf

and make sure you have the following line uncommented :

1 net.ipv4.ip_forward=1

and apply the changes :

1 sysctl -p

No comments:

Post a Comment