Hello there,

I want to share my solution to connect Bela to a Linux computer.
My host computer detected the Bela network adapter when plugged in via USB.
However, it did not automatically get an IP address assigned.
In the past I used sudo dhclient enxbe1aXXXXXXXX to retrieve an IP adress for the adapter. I needed to do this every time I rebooted bela or my computer.

Setting up DHCP

Here is a solution to automatically setup a DHCP connection on Linux (I'm running Kubuntu 23.04).

sudo nano /etc/netplan/config.yaml

network:
  version: 2
  renderer: networkd
  ethernets:
    all:
      match:
        name: enxbe1a*
      dhcp4: true
sudo chmod 600 /etc/netplan/config.yaml
sudo chmod 600 /etc/netplan/01-network-manager-all.yaml
sudo netplan apply

This should set up DHCP automatically once your bela device connects.

Internet Sharing

For sharing the internet connection of the host computer, you can create the following two files:
internetToTargetOnHost.sh

#!/bin/sh
sudo iptables --table nat --append POSTROUTING --out-interface enxXXXXXXXXXXXX -j MASQUERADE
sudo iptables --append FORWARD --in-interface enxbe1aXXXXXXXX -j ACCEPT
sudo iptables --append FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward

internetToTargetOnTarget.sh

#!/bin/sh
ip route add default via 192.168.7.1
echo nameserver 8.8.8.8 | tee -a /etc/resolv.conf

Replace enxXXXXXXXXXXXX with your host interface that connects you to the internet and replace enxbe1aXXXXXXXX with the bela ethernet device. You can list your ethernet devices with the ip address command. Run internetToTargetOnHost.sh on the host computer and internetToTargetOnTarget.sh on the Bela device.

For further debugging refer to this excellent write up about connecting the beagle bone to the host computer.
But realize that some of the steps only work on older linux systems that do not use netplan and network-manager yet.

Greetings, Clemens

Works nicely on Kubuntu 23.10.
I think it would be even better, if the adapter only gets the Bela IP and bela.local address assigned to as static routes and the real network adapter can keep the dhcp4, but I don't know enough netplan to make this work.

    max if the adapter only gets the bela IP and bela.local address assigned to as static routes

    I think that for that you'd need to be able to disambiguate between the enxbela1xxxxxxx that would get 192.168.7.1 and the one that would get 192.168.6.1 and that's probably very much specific to each BBB/PB (as the mac address is different for each). I'd think that getting it from DHCP should be good enough (that's how it works on other systems anyhow). What makes you prefer it to be static?

    • max replied to this.

      giuliomoro
      Because it happened before that after sudo dhclient enbe1axxxxxxxx my computer lost the internet connectivity (I assume because it tried to resolve every URL through the Bela).

      Hmm I don't know much about how that works, but it seems that dhclient does take default gateway information from a DHCP server by design, depending on the client's settings in /etc/dhcp/dhclient.conf.

      However, it looks to me lile setting the default gateway is disabled on the DHCP server side (i.e.: on Bela), at least judging from /etc/dhcp/dhcpd.conf there: afaiu, one would need a option routers entry there for each relevant subnet there, but there is no such thing:

      subnet 192.168.7.0 netmask 255.255.255.0 {
              range 192.168.7.1;
      }
      subnet 192.168.6.0 netmask 255.255.255.0 {
              range 192.168.6.1;
      }

      So I am confused as to how this may have happened to you before. Anyhow, it seems that most recent netplan (above 0.90) (which, btw, I am unsure whether it is supposed to behave very much the same as dhclient under the hood to begin with), has an option to explicitly prevent the dhcp server from setting your routes:
      https://askubuntu.com/questions/1008825/how-do-i-stop-netplan-from-binding-a-default-gateway-on-every-interface
      https://netplan.readthedocs.io/en/latest/netplan-yaml/#dhcp-overrides

      2 months later

      Just came here to say that the Bela experience is so much more convenient (for a Linux user) ever since I've edited this netplan file. Maybe this tip needs to go to the knowledge base.