Linux configure Network Address Translation or NAT

Linux configure Network Address Translation or NAT

To configure Network Address Translation (NAT) on a Linux system, you will need to follow these steps:

  1. Enable packet forwarding. NAT works by allowing the Linux system to forward packets from the internal network to the internet and vice versa. To enable packet forwarding on the Linux system, you will need to edit the /etc/sysctl.conf file and set the net.ipv4.ip_forward parameter to 1. For example:
refe‮al:ot r‬utturi.com
net.ipv4.ip_forward = 1

After you have edited the sysctl.conf file, you will need to apply the changes by running the following command:

sysctl -p

This will enable packet forwarding on the Linux system.

  1. Set up iptables rules to enable NAT. NAT works by using iptables rules to rewrite the source and destination addresses of packets as they pass through the Linux system. To set up NAT on the Linux system, you will need to add iptables rules that perform NAT. For example, to enable NAT on an interface named eth0 that is connected to the internal network, and an interface named eth1 that is connected to the internet, you can use the following iptables commands:
# Enable NAT on eth0
iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE

# Enable traffic from the internal network to reach the internet
iptables -A FORWARD -i eth0 -o eth1 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -i eth0 -o eth1 -j ACCEPT

# Enable traffic from the internet to reach the internal network
iptables -A FORWARD -i eth1 -o eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT

These iptables rules will enable NAT on the Linux system, allowing devices on the internal network to access the internet through the Linux system.

  1. Save the iptables rules. After you have set up the iptables rules, you will need to save them so that they persist across reboots. To save the iptables rules, you can use the iptables-save command like this:
iptables-save > /etc/iptables.rules

This will save the iptables rules to the /etc/iptables.rules file.

  1. Configure the internal network devices to use the Linux system as their default gateway. To allow devices on the internal network to access the internet through the Linux system, you will need to configure the devices to use the Linux system as their default gateway. To do this, you will need to set the default gateway on the devices to the IP address of the Linux system on the internal network. For example, if the IP address of the Linux system on the internal network is 192.168.1.1, you can set the default gateway on the internal network devices to 192.168.1.1.
Created Time:2017-10-29 22:09:01  Author:lautturi