Setting up a NAT-server


I will here try to demonstrate how you can add a gateway for you local net.
It is understood that you are connected to a LAN with a static IP.

Prerequisits


This is a startup-script getting your gateway running.

ISP_IP=172.16.22.24                                                  # IP assign to you from your ISP
ISP_NETMASK=255.255.255.0                                            # Netmask from your ISP
ISP_GATEWAY=172.16.22.254                                            # Gateway from your ISP
LOCAL_IP=10.0.0.1                                                    # Local ip of your choice. Will be your gateway for computers inside of your network.
LOCAL_NETMASK=255.255.255.0                                          # Netmask for your local net (your choice)

/sbin/ifconfig lo 127.0.0.1                                          # Loopback
/sbin/route add -net 127.0.0.0 netmask 255.0.0.0 lo                  # Add route for loopback
/sbin/ifconfig eth0 $ISP_IP netmask $ISP_NETMASK                     # NIC connected to your ISP
/sbin/ifconfig eth1 $LOCAL_IP netmask $LOCAL_NETMASK                 # NIC connected to your local net. 
/sbin/route add default gw $ISP_GATEWAY netmask 0.0.0.0 metric 1     # Add default gateway (From your ISP)
echo 1 > /proc/sys/net/ipv4/ip_forward                               # Tells the kernel to forward packets
iptables -t nat -I POSTROUTING -o eth0 -j SNAT --to-source $ISP_IP   # Adds a rule saying that all forwarded packets to your ISP will have ip:s changed to
                                                                     # the one given to you from your ISP. Will also keep track of what packets have been
                                                                     # sent from different machines inside your local network so that each machine get
                                                                     # the packets responding to "their" communication

Setting up nameservers

Create a file /etc/resolv.conf. Assuming your dns-servers are 172.16.22.4 and 172.16.22.5.
nameserver 172.16.22.4
nameserver 172.16.22.5

Settings for machines on your local lan.

Gateway:     10.0.0.1       # Or whatever you chose as your gateway address (not your ISP:s)
DNS-server1: 172.16.22.4    # We want to use our ISP:s DNS-servers
DNS-server2: 172.16.22.5    
IP:          10.0.0.2       # Anything that matches your netmask. In this example 10.0.0.(2..254) would do nicely

Author Per-Olof Pettersson