Thursday, June 30, 2011

Setting Up NAT Server in Linux

Here I have mentioned How to setup a NAT server in Linux in just few servers with the help  of iptable.
In my scenario I have tested with Red Hat enterprise Linux 6(Kernal 3.5) but I think I should work with most of linux kernels .
In my Environment there are two lan card on my linux system where I want to setup NAT server.

I am assuming a well configured linux system where iptable  and network connectivity is working with two NIC card and  IP address  configured are :

For eth0
IP= and gateway =
(In most of practical scenario eth0 is given as public ip provided by your ISP)
For eth0
Now login to terminal with superuser (root)
First you need to tell your kernel that you want to allow IP forwarding.
#echo 1 > /proc/sys/net/ipv4/ip_forward
Now  configure iptables to forward the packets from your internal network(, on /dev/eth1, to your external network( i.e provided by your ISP) on /dev/eth0. You do this will the following commands:
# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
#  iptables -A FORWARD -i eth0 -o eth1 -m state   --state RELATED,ESTABLISHED -j ACCEPT
# iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT

Now now open /etc/sysctl.conf in your editor (in my case I am using vim)
#vim . /etc/sysctl.conf 
Change the line that says net.ipv4.ip_forward = 0to net.ipv4.ip_forward = 1

Now now edit  /etc/sysconfig/iptables-config in your editor (in my case I am using vim) make sure that:

Reboot machine and you are done.

No comments:

Post a Comment