Skip to content

Instantly share code, notes, and snippets.

@Lewiscowles1986
Last active March 11, 2019 20:27
Show Gist options
  • Save Lewiscowles1986/f303d66676340d9aa3cf6ef1b672c0c9 to your computer and use it in GitHub Desktop.
Save Lewiscowles1986/f303d66676340d9aa3cf6ef1b672c0c9 to your computer and use it in GitHub Desktop.
Allows passthrough for bridges, wireless access-point's and range extenders
#!/bin/bash
if [ "$EUID" -ne 0 ]
then echo "Must be root"
exit
fi
ADAPTER="eth0"
# Allow overriding from eth0 by passing in a single argument
if [ $# -eq 1 ]; then
ADAPTER="$1"
fi
#Uncomment net.ipv4.ip_forward
sed -i -- 's/#net.ipv4.ip_forward/net.ipv4.ip_forward/g' /etc/sysctl.conf
#Change value of net.ipv4.ip_forward if not already 1
sed -i -- 's/net.ipv4.ip_forward=0/net.ipv4.ip_forward=1/g' /etc/sysctl.conf
#Activate on current system
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o $ADAPTER -j MASQUERADE
iptables -A FORWARD -i $ADAPTER -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i wlan0 -o $ADAPTER -j ACCEPT
@Lewiscowles1986
Copy link
Author

Awesome, thanks, I'll check it out and leave issues or PR's if I find anything I think could change

@Ankoul
Copy link

Ankoul commented Jun 1, 2017

@Lewiscowles1986 i realy would like to thank you for this script. I am trying to create a captive portal since may 22(almos ten days working). I followed every tutorial i found and i could't made it works. I faced a lot of trouble and i was able to passthrough everithing but the internet connection. I was able to see and connect in my network, but no internet connection. I decided to separate things and first try to create a simple hotspot router. I faced the same thing on every place i went, every tutorial i followed and now is the first time i got abble to connect and use the internet with some device. My notebook couldn't connect, but my cellphone did. I am so happy. Thank you so much for this script, it was a light for me. Now i can try the captive portal again. Thank you one more time.

@vicatcu
Copy link

vicatcu commented Nov 1, 2017

Hi @Lewiscowles1986, I used your excellent script to turn my Raspberry Pi 3 into an access point. I've just tried to add this adapter-passthrough script to sudo crontab -e as a @reboot script as you suggested, but I'm not getting any love. In fact if I run ifconfig eth0 isn't even listed as an interface.

My /etc/network/interfaces file contains the following contents:

# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
#source-directory /etc/network/interfaces.d
#
auto lo
iface lo inet loopback

auto eth0
allow-hotplug eth0
iface eth0 inet manual

# Added by rPi Access Point Setup
allow-hotplug wlan0
iface wlan0 inet static
	address 10.0.0.1
	netmask 255.255.255.0
	network 10.0.0.0
	broadcast 10.0.0.255

Can you spot what's wrong?

@vicatcu
Copy link

vicatcu commented Nov 2, 2017

I got it working, here's what I had to do:

  • First my /etc/network/interfaces file contents looks like this:
# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
#source-directory /etc/network/interfaces.d
#
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

# Added by rPi Access Point Setup
allow-hotplug wlan0
iface wlan0 inet static
	address 10.0.0.1
	netmask 255.255.255.0
	network 10.0.0.0
	broadcast 10.0.0.255
  • Next, I followed advice in the answer from Luis Godinez on Oct 6 '16 at 23:32 [here]
    (https://raspberrypi.stackexchange.com/questions/24500/eth0-interface-not-starting-on-boot).
    • In a nutshell I enabled (sudo apt-get update && sudo apt-get upgrade maybe needed) "Predictable Network Interface Names" through sudo raspi-config > Advanced Options > Network interface names > Yes.
    • Then vi /lib/udev/rules.d/73-usb-net-by-mac.rules (not rules.d not rules, contrary to the referenced post), and changed NAME="$env{ID_NET_NAME_MAC}" to NAME="eth0".
    • Reboot and it's working, including the passthrough functionality!

@rongyinggang
Copy link

Thanks a lot, I've tried several ways, but finally I just use your code, and it works!

@Lewiscowles1986
Copy link
Author

Lewiscowles1986 commented Jan 20, 2018

@vicatcu yeah raspbian stretch has some awful decisions including the renaming of adapters. Try working with https://gist.github.com/Lewiscowles1986/390d4d423a08c4663c0ada0adfe04cdb as well to allow you to set /etc/network/interfaces as you've indicated you have here.

Big Picture

  • the script accepts an interface, it's documented above, use it for whatever your eth0 is called.
  • this is only designed for raspbian without edits (although it should work on most vanilla debian-based distro's)
  • the script linked helps get interfaces started if you've edited /etc/network/interfaces in a debian-based distro using the raspbian setup (to skip any networking if a single interface is defined in /etc/network/interfaces ...)

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