How IP forwarding Helps Connecting Private interface to Internet in Linux

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 :-) 

5 Comments... add one

  1. Hi sir how are you?Sir I want to share my internet from one wifi router to another wifi router for long range use is it possible please help me.

    • Hey Umar,

      To extend wifi range it would be best to search for a suitable wifi extender or wifi boosters

      • Sir I want to share my wifi's internet to another wifi router and want to use these routers separately in the range of 100 kilo meter is this possible or not if possible what is the procedure please guide me in details.

        • If you are talking about 100m (instead of 100km) then any old cheap wifi range extender will do.

          If you are not talking about providing internet, and are instead talking about connecting networks using existing internet.

          Running an Wireguard, or OpenVPN might be the best fit for trying to answer what you are trying to ask, a Virtual Private Network s essentially and encrypted tunnel to connect 2 networks together.

          openvpn would be like aircraft carrier, big heavy and incredibly customizable and capable wireguard would be a fighter jet, small, light simple, and very fast.


Leave a Comment