How to Forward ipv4 Between Two Networks: Difference between revisions

From RidgeRun Developer Wiki
No edit summary
 
(No difference)

Latest revision as of 17:23, 31 July 2024

Overview

Sometimes it's difficult to connect a board to internet by Ethernet or WiFi. Here you can find a way to share internet from a PC host to a board if they are connected through a network. An example of this is an NVIDIA Jetson system, which it enables a network over 192.168.55.X when is connected via USB to the PC.

Setup

All of the following commands are run on the host PC which will perform the forwarding. This will need to be performed after each boot.

  • Setup the kernel to allow ipv4 packages forwarding:
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
  • Log as super user:
sudo su
  • Configure the iptables to forward the packages correctly.
IN_NETWORK=<Interface_To_Connect_to_the Board>
OUT_NETWORK=<Interface_Providing_Internet>

iptables -t nat -A POSTROUTING -o $OUT_NETWORK -j MASQUERADE
iptables -A FORWARD -i $OUT_NETWORK -o $IN_NETWORK -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i $IN_NETWORK -o $OUT_NETWORK -j ACCEPT
  • You can get out the super user mode:
exit

After this you can try to ping any device on the parent network or if this network can access the web a site such as Google.

Example

  • Check your Network interfaces on your PC:
ip addr

...
2: wlp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    ...
    inet 192.168.1.32/24 brd 192.168.1.255
3: enp0s20f0u2i5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 1000
    ...
    inet 192.168.55.100/24 brd 192.168.55.255 scope global dynamic noprefixroute enp0s20f0u1
...
  • From the output, find the interface providing the internet on your PC and the interface with used to communicate with the board (in case of a Jetson system find the IP with 192.168.55.X domain)
IN_NETWORK=enp0s20f0u2i5 # Board interface
OUT_NETWORK=wlp5s0 # External interface with internet
  • Finally run the commands from above section.

After this you can try to ping any device on the parent network or if this network can access the web a site such as Google.