Ethernet tethering from a Raspberry Pi 3

I tried to follow the “Share via ethernet” section of this guide from the wiki, but it’s seemingly either outdated or not general enough: the first step gives me Cannot find device "eth0". Swapping eth0 for enu1u1, which looks like it might be the right candidate based on the output of ip a, results in a failure at the 4th step, with Error: No such file or directory; did you mean table ‘mangle’ in family ip?.

I’ve done this before via the network settings GUI on a Manjaro+XFCE system, but I’m pretty lost when it comes to command line network config.

Finally, is there a more declarative way to achieve this? I like how simple the WiFi examples further up the wiki page look, but that’s unfortunately the opposite direction to what I want to achieve.

If I understand correctly, you basically want to make your Rapsberry Pi 3 into a router? If so, you basically need the networking.nat option. You can also use dnsmasq as the DHCP server declaratively using services.dnsmasq.

You should be able to steal some networking configuration from Solene’s blogpost on making a router.

Sort of? That sounds more general than what I want to achieve, which is to share my WiFi connection with another computer which doesn’t have WiFi. But I suppose maybe that does mean making it a router in a sense.

That blog post may well contain the solution, and I’ll try to digest it when I have time, but currently I understand about half of the words!

This is very easy on my Linux desktop (create a new wired connection and select Shared to Other Computers), and a one-click option on an Android phone (USB tethering in the USB Preferences menu which pops up when a cable is connected). So I hoped it would be simple on a headless NixOS device as well.

Modified title to reflect that USB-based tethering, as I’m now able to do from my phone, would also be adequate. Huh, turns out “Ethernet” is the protocol, not the physical connector, so it’s still Ethernet either way. Anyway, this looks like it’s essentially my question but for USB, and it looks like it goes to some effort to overcome hardware quirks, where the solutions may be Pi4-specific. So using the actual Ethernet port is sounding like a preferable solution anyway.

The important parts to achieve this (i.e. make your device into a “router”) are:

  1. Configuring a static, internal IP for your “LAN”, i.e. the interface that you are sharing the connection to. This will be the “gateway address” for devices connected to this “hotspot”.
  2. Doing network address translation from “WAN” to “LAN”, for which you need the networking.nat options, which are pretty straightforward. Technically, you also need to enable IP packet forwarding, but the NixOS NAT module does that for you.
  3. Serving DHCP. Technically not required, but without a DHCP server on the network you’ll need to manually configure IP addresses on each client connecting to the “hotspot”. Solene seems to be using dhcpd4, but dnsmasq works as well. Be sure it’s only serving on the “LAN” interface.

Pretty much everything else in that blog post, such as unbound (DNS resolver), miniupnpd (UPnP server), making a bridge out of two interfaces, etc. is optional. Your DHCP server can just advertise a public DNS resolver (Cloudflare, Google DNS, your ISP’s nameservers, etc.)

Thanks for the response @justinas! I haven’t tested it but I’m sure it will be useful to someone.

As it happens my use case for this has disappeared (I had issues with the built-in WiFi when running Linux on an iMac Pro, but to my surprise that got fixed upstream in the past few days).