How IP forwarding Helps Connecting Private interface to Internet in Linux

April 22, 2015 | By in FIREWALL, NETWORK
| Reply More

Hi everyone, today we'll learn about IP forwarding or Packet forwarding from one network interface to another in Linux using iptables. IP forwarding is a concept to make Linux machine to send data from one network to another as a router. So, it can be used as a router or a proxy server that can share internet or network connection from one connection to multiple client machines.

Here are some easy steps to enable IP Forwarding or Network packets forwarding method.

1. Enabling IPv4 Forwarding

First of all, we'll gonna enable IPv4 forwarding in our Linux Operating System. To do that, we'll need to execute the following command in a shell or a terminal under sudo mode.

$ sudo -s

# echo 1 > /proc/sys/net/ipv4/ip_forward

Temporary IP Forwarding

Note: the above command is enables ip forwarding instantly and temporarily until the next reboot. To enable it permanently, we'll need to open /etc/sysctl.conf using our favorite text editor.

# nano /etc/sysctl.conf

Then, add net.ipv4.ip_forward = 1 into that file or uncomment that line then, we'll need to save and exit that file.

net.ipv4.ip_forward = 1

Editing Sysctl

To enable the changes made run the following command.

# sysctl -p /etc/sysctl.conf

Enabling Sysctl Changes

2. Configuring Iptables firewall

We'll need to allow specific (or all of the) packets to traverse our router. Before that, we'll need to know the interface name of the network devices connected with our Linux box. To get the interface name, we can simply run the following command in a terminal or a shell.

# ifconfig -a


Here, in our box we have eth2 as network interface which is connected to the internet or a network and wlan2 is the interface where we need to forward the network packets from eth2 using iptables. So, to do so, we'll need to run the following command.

# iptables -A FORWARD -i wlan2 -o eth2 -j ACCEPT

Note: Please replace wlan2 and eth2 with the device name available in your Linux box.

Now, we'll need iptables to allow traffic for already established connections as netfilter is a stateless firewall. To do so, we'll need to run the following command.

# iptables -A FORWARD -i eth2 -o wlan2 -m state --state ESTABLISHED,RELATED  -j ACCEPT

Forwarding with iptables

3. Configuring NAT

Then, at last we'll need to change the source address as eth2 on packets going out to the internet by executing the command below.

# iptables -t nat -A POSTROUTING -o eth2 -j MASQUERADE

configuring nat


Finally, we've successfully configured Packet forwarding from one interface to another interface on our Linux machine having iptables installed as firewall solution. This article enables to connect your private interface to the internet, you don't need bridging the interfaces, you need to route packets coming in on one interface to the other one, thats all. If you have any questions, suggestions, feedback please write them in the comment box below so that we can improve or update our contents. Thank you ! Enjoy :-) 


Share This :

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.