NAT(Network Address Translation) Part2 without using AWS

Preet Padariya
4 min readMar 22, 2023

--

What is NAT?

NAT is Network Address Translation. Basically, It is a technique in which a router or a similar device translates one IP address into another IP address information in packets’ IP headers as they transit across a traffic routing device.

A router translates the private address of an internal host into a public IP address for outgoing traffic with that from the opposite side similarly router will translate its own public IP address into a private one for the incoming traffic. This lets organizations use one public IP address and many private IP addresses within the network.

An example of a real-life situation is a receptionist at a company. Just call on the company and once you reach the receptionist, he/she will transfer your call to one of the private numbers inside the company.

What is the need for NAT?

As we cannot use private IPv4 addresses to access the internet at the same time, the number of public IPv4 addresses is limited. Therefore, we need a way in which hosts in our network that have been assigned private IPv4 addresses can access the internet. So here is the need for Network Address Translation (NAT) as few public IPv4 addresses are made which are used to access the internet even if they have many hosts who have been assigned private IPv4 addresses.

DEMO/Confirmation

Step 1: Install the Required Packages

To configure the NAT server, we need to install the iptable packages. Running the below command in cmd the package will be installed:

```sudo apt-get install iptables```

install of iptables

Step 2: Enable IP Forwarding

First, we need to enable the IP forwarding in Ubuntu because by default it is disabled. You can do this by editing the `/etc/sysctl.conf` file. Open the file with a text editor such as nano:

```nano /etc/sysctl.conf```

Uncomment the following line by removing the “#” sign to enable IP forwarding:

```net.ipv4.ip_forward=1```

enable IP forwarding

Save the file and run the following command:

```sysctl -p```

This command will reload the modified configuration file.

Step 3: Configure IPTables

Next, we need to configure IPTables, which is the Linux firewall. You can do this by running the following commands:

``` iptables -F

iptables -t nat -F

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

iptables -A FORWARD -i eth1 -j ACCEPT```

configure IPTables

The first two commands remove any previous rules from the IPTables configuration. The third command adds a Masquerade rule to the POSTROUTING chain of the NAT table, which allows the device to share its internet connection with other devices. Replace `eth0` with the device’s name connected to the internet.

The last command adds a rule to the FORWARD chain of the IPTables configuration, which allows the forwarding of packets from the local network to the internet. Replace `eth1` with the device’s name connected to the local network.

Step 4: Save IPTables Configuration

After configuring IPTables, you need to save the configuration for it to persist across reboots. You can do this by running the following command:

```sh -c “iptables-save > /etc/iptables.rules”```

This command will save the IPTables configuration to the `/etc/iptables.rules` file.

These lines tell Ubuntu to load the IPTables configuration from `/etc/iptables.rules` during boot, and to save the configuration to the same file during the shutdown.

Save the file and reboot the device.

File

Conclusion

From this google.com server identify this private instance through the NAT gateway IP. With that global address space allocation in face of IPv4 address exhaustion by sharing one Internet-routable IP address of a NAT gateway for an entire private network. Taking the CHARUSAT example the PCs of the lab are in a private subnet and the wi-fi access/routers are using a NAT gateway.

Contributed by

Jay Nakarani(20CE058) & Preet Padariya(20CE063)

NAT using AWS

NAT using AWS

--

--