Setting NAT On Linux
Setting NAT On Linux
This step-by-step tutorial shows how to set up Network Address Translation (NAT) with Open
Source Linux operating system and iptables firewall. This will allow your system to act as
gateway and to provide Internet access to multiple hosts in Local Area Network (LAN) using a
single public IP address.
Requirements
Basic definitions
Step-by-step set up
the output may vary but in most cases it would be like following one:
To verify that NICs are recognized by Linux as networking devices use the following
commands:
ifconfig eth0
ifconfig eth1
In case of success the output will be as follows:
You can find full manual page for ifconfig command here.
e.g.
Configuration files containing NIC settings may have different syntax and location in various
distributions. For such distributions as RedHat, Fedora, Centos and similar ones eth0
configuration file is at /etc/sysconfig/network-scripts/ifcfg-eth0. In Debian, Ubuntu NIC settings
are located at single file /etc/network/interfaces.
To edit configuration files use any preferred text editor like vim, GNU nano or any other.
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=aa.aa.aa.aa # e.g. 123.45.67.89
NETMASK=bb.bb.bb.bb # e.g. 255.255.255.0
GATEWAY=ee.ee.ee.ee # e.g. 123.45.67.1
HWADDR=00:30:4f:3b:af:45 # MAC address (optional entry)
After making changes to /etc/network/interfaces section regarding eth0 NIC should looks like:
auto eth0
iface eth0 inet static
address aa.aa.aa.aa
netmask bb.bb.bb.bb
gateway ee.ee.ee.ee
Related links: detailed syntax description of /etc/sysconfig/network-scripts/ifcfg-ethN, manual
page of /etc/network/interfaces.
5. Set up LAN NIC settings to apply after server start up. This step requires operations similar to
previous step.
DEVICE=eth1
ONBOOT=yes
BOOTPROTO=static
IPADDR=cc.cc.cc.cc # e.g. 192.168.0.1
NETMASK=dd.dd.dd.dd # e.g. 255.255.255.0
HWADDR=00:50:8d:d1:24:db # MAC address of LAN NIC (optional entry)
If you are using Debian or related Linux distribution, edit /etc/network/interfaces (see previous
step):
auto eth1
iface eth1 inet static
address cc.cc.cc.cc
netmask dd.dd.dd.dd
nameserver 203.145.184.13
nameserver 203.145.184.12
7. Enable IP Forwarding:
To delete existing rules from every iptables table, execute the following commands:
iptables -F
iptables -t nat -F
iptables -t mangle -F
IP address: from the same network as cc.cc.cc.cc (you can use IP/Subnet calculator to get it)
Netmask: dd.dd.dd.dd
DNS: ff.ff.ff.ff
Gateway: cc.cc.cc.cc
Example:
IP address: 192.168.0.7
Netmask: 255.255.255.0
DNS: 209.160.67.13
Gateway: 192.168.0.1
Setting all this up can be a lot easier if you’re using a control panel rather than the command line,
but I’ll save that for another article.