Sharing Internet to Raspberry Pi 4 via USB-C

Share Internet between Raspberry Pi and your computer via USB-C.

Sharing Internet to Raspberry Pi 4 via USB-C
The Real and Linux way to do this.

So I was testing some code on my loyal Raspberry Pi 4, ver. 1.2. The Pi is directly connected to my router in another room. Cuz, like, Pi's ethernet is so much more stable than its wireless. The downside is that every time the Pi had an issue, I had to run over to the other room to recover it.

Let's just say it's not a pleasant experience. Plugging in and out the microSD card, just to find out the Pi still didn't work. And the ethernet cable is great for tripping somebody. Even worse, all those cables made my room messy.

Oh, only if you can connect your Pi to your laptop directly, and it would magically provide electricity and data transfer functionalities, the whole thing would be much easier...

Apparently you can. Raspberry Pi 4's USB-C port is not just a dumb charging port like many other single board computers' (Orange Pi's charging port sucks). Thanks to Pi's data signal lines actually being wired, it is possible to transfer both data and electricity.

Obviously, setting up Pi's USB-C as an ethernet gadget is the best option. I can connect to it directly via SSH, transfer files with SFTP, and so much more, all over the same USB cable.

Bonus: no unstable wireless network.

Config Raspberry Pi's Bootloader

Add dtoverlay=dwc2 as a new line to the bottom of your Pi's config.txt.

Where's config.txt? I'm sure you know at this point.

Setting up your Raspberry Pi's OS

I am using Debian's Raspberry Pi 4 image on a Raspberry Pi, but technically any single board computers that supports OTG is supported.

Make sure you have a monitor(not headless) in case something went wrong.

Install Necessary Software

Install vim, or any other editor except emacs.

avahi avoids the hassle of trying to find out Pi's IP address, and is recommended.

apt install vim avahi-daemon
systemctl enable --now avahi-daemon

Enable SSH

Debian's image comes with SSH disabled, enabling.

Enable Modules for OTG Ethernet

Create /etc/modules-load.d/g_ether.conf on your Pi:

vim /etc/modules-load.d/g_ether.conf

...and enter those lines:


Configure Network Interfaces

The interface name of the ethernet gadget will be usb0.

Create /etc/network/interfaces.d/usb0 on your Pi:

vim /etc/network/interfaces.d/usb0

...and enter those lines:

allow-hotplug usb0
iface usb0 inet dhcp

allow-hotplug is quite important here, without it the Pi boots up much slower.

Actually, you might want to change auto eth0 in /etc/network/interfaces.d/eth0to allow-hotplug eth0 as well. Why does Debian default to auto anyway? smh

Obtain a unique MAC

Now, plug the Pi in your computer, and wait for a USB ethernet adapter to appear.

You should be able to connect to the Pi via raspberrypi.local, or your-pi-hostname.local.

Unfortunately, OTG ethernet gadget does not come with a unique MAC address. You need to specify one manually, otherwise it will generate a random MAC every time it boots.

To specify MAC address, execute as root:

echo 'options g_ether host_addr='$(dmesg | awk '/: HOST MAC/{print $NF}')' dev_addr='$(dmesg | awk '/: MAC/{print $NF}') | tee /etc/modprobe.d/g_ether.conf

Reboot the Pi.

You did it! Your Pi has evolved into a standard ethernet adapter via USB-C. Annoying ethernet cables no more, and you can share your computer's Internet connection to the Pi.

The interesting thing is, since iOS supports Zeroconf and ethernet adapters, you can actually connect your Raspberry Pi 4 to an iPad for unlimited productivity. So yeah, you can do offline coding on an iPad with a Raspberry Pi.