NAT or Network Address Translation allows multiple computers on a private network to share a common IP to access the Internet. One set of IP addresses is used inside the organization and the other set is used by it to present itself to the internet. This helps in conserving limited public IP space. At the same time, it also provides security by hiding the internal network from direct access from the outside world.
How Does the NAT Work?
NAT simply converts the source address of the outgoing packet to a public IP address to make it routable on the internet. In the same way, the source address of the response packets coming from outside (internet) is converted back to local or private IP address.
What will we cover?
In this guide we will learn to set up Ubuntu 20.04 as a NAT router. For this we will use an Ubuntu VM as NAT router and another Ubuntu VM as client VM for the purpose of testing. To test the setup, we are using Virtualbox for creating and managing virtual machines(VMs).
- Two Ubuntu VMs with one having two network interfaces(NICs).
- Ubuntu 20.04 installed on both the VMs.
- Administrative(sudo) access on both the VMs.
We have used the following setup for the two VMs mentioned above:
1. VM1 (NAT Router): Our Router machine has two network interfaces: enp0s3 and enp0s8 (these names may vary depending on the system). The enp0s3 interface acts as a WAN(internet) interface and is accessible from the outside world(Internet). Its IP address is assigned via DHCP and in our case it is 192.168.11.201.
The enp0s8 interface is a local or LAN interface and is accessible only on the local network where our client will be deployed. We have manually set the IP address for this interface as 10.10.10.1/24 and the “gateway address is left blank”.
2. VM2 (Client Machine): The client machine has only one local or LAN interface i.e. enp0s3. It is attached to the local network of the above machine(VM2) with the IP address set to 10.10.10.3/24. The only thing to care of is that the gateway in this case is the local interface (enp0s8) IP address of the above machine(VM2) i.e., 10.10.10.1
The summary of the configuration of the two virtual machines is given in the below table:
|Interface Name →||enp0s3||enp0s8|
|VM Name ↓||IP address||Gateway IP||IP address||Gateway IP|
|VM1(NAT Router )||192.168.11.201/24||Via DHCP||10.10.10.1/24|
Now that we have set up the required IP addresses on our machine, we are set to configure them. Let us first check the connectivity between these machines. Both the machines should be able to ping each other. VM1, which is our NAT router machine, should be able to reach the global internet as it is connected to WAN via enp0s3. VM2, which is our local client machine, should not be able to reach the internet until we configure the NAT router on VM1. Now, follow the steps below:
Step 1. First check the IP addresses on both the machines with the command:
Step 2. Also check the connectivity of the machines before configuring the NAT router as mentioned above. You can use the ping command like:
Result for the VM1 (NAT Router VM) are shown below:
Result for the VM2 (ClientVM) are shown below:
Both the VMs are working as we have expected them to be. Now we will start configuring VM2(NAT Router).
Step 3. On VM2 open the sysctl.conf file and set the “net.ipv4.ip_forward” parameter to one by uncommenting it:
Step 4. Now enable the changes to above file using the command:
Step 5. Now, install the iptables-persistent package (boot-time loader for netfilter rules, iptables plugin) using:
Step 6. List the already configured iptable policies by issuing the command:
Step 7. Now mask the requests from inside the LAN with the external IP of NAT router VM.
$ sudo iptables -t nat –L
Step 8. Save the iptable rules using:
Testing The Setup
Now, to check if everything is working fine, ping any public IP from the VM2(client):
Note: If you want, you can add a DNS server manually in the client network configuration for domain name resolution. This will suppress the ‘Temporary failure in name resolution’. We have used the Google DNS IP i.e. 188.8.131.52 in our VM1.
We can see that ping is now working as expected on VM1(client machine).
NAT is a very essential tool for preserving limited public IPv4 address space. Although IPv6 is emerging next generation IP protocol which is supposed to finish the IPv4 limitations, but that’s a long way process; so until then NAT is very important for organizations.