Skip to content

Instantly share code, notes, and snippets.

@tzermias
Last active March 20, 2024 02:06
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save tzermias/5408466 to your computer and use it in GitHub Desktop.
Save tzermias/5408466 to your computer and use it in GitHub Desktop.
Forward traffic from wlan0 to eth0 interface

Forward traffic from a laptop's eth0 to wlan0

###To laptop

Specify an IP address to eth0 (here 192.168.56.1)

sudo ifconfig eth0 192.168.56.1 netmask 255.255.255.0

Enable IP forwarding

sudo sysctl -w net.ipv4.ip_forward=1

Forward packets from eth0 to wlan0

sudo iptables -A FORWARD --in-interface eth0 -j ACCEPT
sudo iptables --table nat -A POSTROUTING --out-interface wlan0 -j MASQUERADE

###To device connected to laptop

Setup IP address for eth0,and add gateway

sudo ifconfig eth0 192.168.56.2 netmask 255.255.255.0
sudo route add default gw 192.168.56.1
@moinahmed001
Copy link

On top you have stated: Forward traffic from wlan0 to eth0 interface
However on the file states the opposite. Which is correct?

@adnanhz
Copy link

adnanhz commented Nov 20, 2016

It's wlan0 to eth0.

@valp124
Copy link

valp124 commented Sep 22, 2017

That works! What's the best way to have it persist between reboots?

@henrytom1703
Copy link

henrytom1703 commented Apr 25, 2018

Hi all.
How to forward all android(rooted device) traffic to Laptop?

@the-celtic
Copy link

the-celtic commented Jun 12, 2018

Hi valp124 and any other, out there. Just for the records, if someone looks for the same infos :)

1st to make iptables rules persistent, install the "iptables-persistent" package, avaiable on every common linux distro
this will add your current tables to an autostart script

2nd to make routing persistent, edit /etc/sysctl.conf and add or modify following entry to "net.ipv4.ip_forward = 1"
think is verry common, checked with debian stretch

Cheers the-celtic

@rashid4c7
Copy link

Thanks

@vsayanam
Copy link

Thank you very much. I just wanted to connect mobile hotspot to a laptop via an Ethernet cable directly without a router.Many suggestions in Google are very elaborate. I just added ipforwarding and iptable commands as per your suggestion. I have already configured both the Ethernet interfaces. It worked beautifully. Thanks again.

@amoldhamale1105
Copy link

I have created a generic script to which can simply provide 2 args, one for master interface from which you want to forward and the slave interface which is supposed to receive traffic from master. Check it out https://github.com/amoldhamale1105/BeagleboneLab/blob/master/usbnet.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment