Admins eHow SysAdmin Tips & Tricks

November 15, 2009

How to setup a general IP proxy in linux

Filed under: General — admin @ 10:09 pm

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 :

nano proxy

copy and paste the following script into the file :

YourIP=X.X.X.X
TargetIP=Y.Y.Y.Y
Port=Z

iptables -t nat -A PREROUTING -p tcp --dport $Port -d $YourIP -j DNAT --to $TargetIP
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 :

chmod +x proxy
./proxy

enable IPv4 forwarding in sysctl.conf :

nano /etc/sysctl.conf

and make sure you have the following line uncommented :

net.ipv4.ip_forward=1

and apply the changes :

sysctl -p

now we are all set 🙂

Powered by WordPress