Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save pdp7/d2711b5ff1fbb000240bd8337b859412 to your computer and use it in GitHub Desktop.
Save pdp7/d2711b5ff1fbb000240bd8337b859412 to your computer and use it in GitHub Desktop.
BeagleBone: sharing Internet connection from Linux laptop with USB

WARNING: interfaces can enumarate differently:

ufw status
#iptables -F
#iptables --table nat --append POSTROUTING --out-interface wlxe0b94db737c9 -j MASQUERADE
#iptables --table nat --append POSTROUTING --out-interface enp0s20u1u1 -j MASQUERADE
iptables --table nat --append POSTROUTING --out-interface enp0s20u1u2 -j MASQUERADE
#iptables --append FORWARD --in-interface enx1cba8c9bbeb5 -j ACCEPT
#iptables --append FORWARD --in-interface enx9884e391a556 -j ACCEPT
iptables --append FORWARD --in-interface enx9884e391a559 -j ACCEPT
iptables -S
iptables -vL
iptables -t nat -v -L

Yesterday, I got Internet connection sharing over USB working on my system.

My BeagleBone Black is connected via USB to my Linux laptop (Ubuntu 17.10). The BBB is able to connect to the Internet by my Laptop laptop forwarding traffic through the WiFi interface.

BBB <-- USB --> Laptop <-- WiFi --> Internet

I got this working 6 months ago but had a really hard time recreating those steps yesterday. I've noted what I did in this GitHub Gist in case it helps anyone else (and myself in the future :)

My contact info: Drew Fustini drew@beagleboard.org

Linux laptop

Configure host (laptop running Ubuntu 17.10) to forward packets:

root@sp3:~# grep forward /etc/sysctl.conf
# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1
# Uncomment the next line to enable packet forwarding for IPv6
net.ipv6.conf.all.forwarding=1
# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1
# Uncomment the next line to enable packet forwarding for IPv6
net.ipv6.conf.all.forwarding=1

Make sure forwarding is configured on the host (laptop):

root@sp3:~# sysctl -a |fgrep .forwarding |grep ^net |grep ipv4
net.ipv4.conf.all.forwarding = 1
net.ipv4.conf.default.forwarding = 1
net.ipv4.conf.enx1cba8c9bbeb5.forwarding = 1
net.ipv4.conf.enx1cba8c9bbeb8.forwarding = 1
net.ipv4.conf.lo.forwarding = 1
net.ipv4.conf.wlxe0b94db737c9.forwarding = 1

Interfaces on the host (laptop):

root@sp3:~# ifconfig 
enx1cba8c9bbeb5: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.7.1  netmask 255.255.255.252  broadcast 192.168.7.3
        inet6 fe80::8526:f82b:7c63:7b  prefixlen 64  scopeid 0x20<link>
        ether 1c:ba:8c:9b:be:b5  txqueuelen 1000  (Ethernet)
        RX packets 412  bytes 56069 (56.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 792  bytes 127857 (127.8 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enx1cba8c9bbeb8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 1c:ba:8c:9b:be:b8  txqueuelen 1000  (Ethernet)
        RX packets 44  bytes 7080 (7.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 17445  bytes 1546254 (1.5 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 17445  bytes 1546254 (1.5 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlxe0b94db737c9: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.127  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::b222:eaea:cc17:a43  prefixlen 64  scopeid 0x20<link>
        inet6 fddb:9efd:dee8::2ca  prefixlen 128  scopeid 0x0<global>
        inet6 fddb:9efd:dee8:0:cce5:1a39:dfa0:e5ab  prefixlen 64  scopeid 0x0<global>
        inet6 2601:241:1:1fbc:54b7:ae3f:4842:3768  prefixlen 64  scopeid 0x0<global>
        inet6 2601:241:1:1fbc:9e38:bdef:c5f1:78ae  prefixlen 64  scopeid 0x0<global>
        inet6 fddb:9efd:dee8:0:54b7:ae3f:4842:3768  prefixlen 64  scopeid 0x0<global>
        ether e0:b9:4d:b7:37:c9  txqueuelen 1000  (Ethernet)
        RX packets 3345184  bytes 4635776508 (4.6 GB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1731919  bytes 265077619 (265.0 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Check that ufw (simple firewall utility on Ubuntu) is disabled:

root@sp3:~# ufw status
Status: inactive

Flush old rules from iptables (e.g. firewall):

root@sp3:~# sudo iptables -t nat -F
root@sp3:~# sudo iptables -t mangle -F
root@sp3:~# sudo iptables -F
root@sp3:~# sudo iptables -X

Add iptables firewall rules to forward traffic from the USB "Ethernet" interface (which is connected to the BeagleBone) to the WiFi interface:

root@sp3:~# iptables --table nat --append POSTROUTING --out-interface wlxe0b94db737c9 -j MASQUERADE
root@sp3:~# iptables --append FORWARD --in-interface enx1cba8c9bbeb5 -j ACCEPT

NOTE:

  • Make sure wlxe0b94db737c9 matches the interface for WiFi interface on the laptop (which is connected to the Internet)
  • Make sure enx1cba8c9bbeb5 matches the interface for the USB "Ethernet" interface.

Check firewall rules in iptables:

root@sp3:~# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination    

BeagleBone Black (rev C)

Interfaces on the BeagleBone Black:

debian@beaglebone:~$ ifconfig 
eth0: flags=-28669<UP,BROADCAST,MULTICAST,DYNAMIC>  mtu 1500
        ether 1c:ba:8c:9b:be:b4  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 53  

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 7840  bytes 527520 (515.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7840  bytes 527520 (515.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

usb0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.7.2  netmask 255.255.255.252  broadcast 192.168.7.3
        inet6 fe80::1eba:8cff:fe9b:beb6  prefixlen 64  scopeid 0x20<link>
        ether 1c:ba:8c:9b:be:b6  txqueuelen 1000  (Ethernet)
        RX packets 910  bytes 91945 (89.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 501  bytes 96039 (93.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

usb1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.6.2  netmask 255.255.255.252  broadcast 192.168.6.3
        inet6 fe80::1eba:8cff:fe9b:beb9  prefixlen 64  scopeid 0x20<link>
        ether 1c:ba:8c:9b:be:b9  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 44  bytes 7696 (7.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Add default route so that the BeagleBone tries to connect to the Internet through the laptop:

debian@beaglebone:~$ sudo route add default gw 192.168.7.1

NOTE: this should be the IP address of the laptop on the USB interface

Verify the routes:

debian@beaglebone:~$ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.7.1     0.0.0.0         UG        0 0          0 usb0
192.168.6.0     0.0.0.0         255.255.255.252 U         0 0          0 usb1
192.168.7.0     0.0.0.0         255.255.255.252 U         0 0          0 usb0

Verify ping of Internet address works:

debian@beaglebone:~$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=120 time=11.4 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=120 time=12.0 ms

Add DNS server:

debian@beaglebone:~$ cat /etc/resolv.conf 
nameserver 8.8.8.8

Verify ping of an Internet hostname:

debian@beaglebone:~$ ping www.google.com
PING www.google.com (172.217.4.36) 56(84) bytes of data.
64 bytes from lga15s46-in-f36.1e100.net (172.217.4.36): icmp_seq=1 ttl=53 time=12.0 ms
64 bytes from lga15s46-in-f36.1e100.net (172.217.4.36): icmp_seq=2 ttl=53 time=10.4 ms
@pdp7
Copy link
Author

pdp7 commented Oct 8, 2018

for completeness, here is information on the BeagleBoard.org Debian image that I am running:

debian@beaglebone:/opt/scripts/tools$ sudo ./version.sh 
[sudo] password for debian: 
git:/opt/scripts/:[1aa73453b2c980b75e31e83dab7dd8b6696f10c7]
eeprom:[A335BNLT000C3014BBBK1316]
model:[TI_AM335x_BeagleBone_Black]
dogtag:[BeagleBoard.org Debian Image 2018-07-15]
bootloader:[microSD-(push-button)]:[/dev/mmcblk0]:[U-Boot 2018.03-00002-gac9cce7c6a]:[location: dd MBR]
bootloader:[eMMC-(default)]:[/dev/mmcblk1]:[U-Boot 2018.03-00002-g254339602c]:[location: dd MBR]
kernel:[4.14.54-ti-r63]
nodejs:[v6.14.4]
uboot_overlay_options:[enable_uboot_overlays=1]
uboot_overlay_options:[uboot_overlay_pru=/lib/firmware/AM335X-PRU-RPROC-4-14-TI-00A0.dtbo]
uboot_overlay_options:[enable_uboot_cape_universal=1]
pkg check: to individually upgrade run: [sudo apt install --only-upgrade <pkg>]
pkg:[bb-cape-overlays]:[4.4.20180803.0-0rcnee0~stretch+20180804]
pkg:[bb-wl18xx-firmware]:[1.20180517-0rcnee0~stretch+20180517]
pkg:[kmod]:[23-2rcnee1~stretch+20171005]
pkg:[librobotcontrol]:[1.0.0-git20180710.0-0rcnee0~stretch+20180712]
pkg:[firmware-ti-connectivity]:[20170823-1rcnee1~stretch+20180328]
groups:[debian : debian adm kmem dialout cdrom floppy audio dip video plugdev users systemd-journal i2c bluetooth netdev cloud9ide gpio pwm eqep admin spi tisdk weston-launch xenomai]
cmdline:[console=ttyO0,115200n8 bone_capemgr.uboot_capemgr_enabled=1 root=/dev/mmcblk0p1 ro rootfstype=ext4 rootwait coherent_pool=1M net.ifnames=0 quiet]
dmesg | grep pinctrl-single
[    1.040657] pinctrl-single 44e10800.pinmux: 142 pins at pa f9e10800 size 568
dmesg | grep gpio-of-helper
[    1.047789] gpio-of-helper ocp:cape-universal: ready
END

Find the current Debian images here: http://beagleboard.org/latest-images

@pdp7
Copy link
Author

pdp7 commented Mar 31, 2019

iptables --table nat --append POSTROUTING --out-interface wlxe0b94db737c9 -j MASQUERADE
iptables --append FORWARD --in-interface enx1cba8c9bbeb5 -j ACCEPT

# on beagle
# sudo route add default gw 192.168.7.1
# sudo bash -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf' 

@pdp7
Copy link
Author

pdp7 commented Apr 3, 2019

WARNING: interfaces can enumarate differently:

ufw status
#iptables -F
#iptables --table nat --append POSTROUTING --out-interface wlxe0b94db737c9 -j MASQUERADE
#iptables --table nat --append POSTROUTING --out-interface enp0s20u1u1 -j MASQUERADE
iptables --table nat --append POSTROUTING --out-interface enp0s20u1u2 -j MASQUERADE
#iptables --append FORWARD --in-interface enx1cba8c9bbeb5 -j ACCEPT
#iptables --append FORWARD --in-interface enx9884e391a556 -j ACCEPT
iptables --append FORWARD --in-interface enx9884e391a559 -j ACCEPT
iptables -S
iptables -vL
iptables -t nat -v -L

@rajesh2610
Copy link

Thank you so much for posting this. I got it working in the first try, but when I rebooted the BBB , I apparently lost the configuration .

  1. After following all the steps, I was able to ping 8.8.8.8 , but when I typed "car /etc/resolv.conf " I get a warning - # DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
    and the response to the above command is "nameserver 127.0.0.1" which is a loopback for the host, I was expecting to see "nameserver 8.8.8.8" instead.

How ever I tried this --> echo "nameserver 8.8.8.8" >> /etc/resolv.conf

  1. But when I restart the beaglebone all the configuration is supposedly lost. And network on beaglebone becomes unreachable again

Am I missing something ?
Any help is appreciated. Also other guys have discussed about using a network manager GUI on ubuntu.Any article or steps if thats easier ?

I am really new to the BBB and linux, I tried going through all the posts related to it on Ubuntu forums, and BBB forums. I tried changing the /etc/network/interfaces file by putting a static IP ,gateway ,netmask,etc . It doesn't help.

My initial hypothesis was that the IP address of eth0 port on my Ubuntu host changed from 192.168.7.1, but then I made it static. The weird part about this is on my BBB I could successfully ping 192.168.7.1 , but when I do a ping 8.8.8.8, all I get is
"From 192.168.7.1 icmp_seq=1 Destination Host unreachable"

Thanks

@lorforlinux
Copy link

Rajesh, the configuration we are doing here is not permanent. you have 2 options to solve this and both require you to create a bash script with these commands. You just have to create one file for the host and one for your BBB, try running the commands first and just put them in the bash file when you are sure they are working for you. After creating the files you can either

  1. Run the bash script(s) every time reboot your PC and BBB.
  2. Set the file to autorun mode so that it will run during boot. there is a folder for that in BBB, start cloud9 ide you'll see it.

@JDuchniewicz
Copy link

I tried setting a systemd unit which would call this script after network.target, however I could not successfully ping anything beyond the gateway.
All works fine when I run these commands after ssh'ing to the BBB, but somehow automatizing it fails to make it work.

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