rpi zero 2w usb ethernet

Abstract

This writeup explains how to configure your rpi zero 2w (from scratch) to allow ethernet over a USB cable. This allows you (for example) to plug your rpi zero 2w directly into a linux PC(ubuntu 24.01) to both power it as well as connect to it over SSH WITHOUT requiring wifi.

Process

As of 2025-03-05, here are the steps to get everything working from “scratch”:

  1. Download and run the Raspberry Pi Imager

  2. Raspberry Pi Device: Raspberry zero 2w, Operating System: Raspberry Pi OS (64-bit), Storage: Micro SD card

  3. OS Customization: Edit Settings

  4. Set your hostname, username and password, wireless LAN, and Locale settings

  5. Choose Yes to apply your OS customization settings

  6. Confirm you want to continue (assuming it is correctly listing your micro SD card) by clicking Yes

  7. Wait for the write and the verification to finish

  8. Plug the micro SD card into your zero and then use a high quality Micro USB to USB cable to connect it to your PC

  9. Using an terminal , ssh into your zero 2w using your previously (step 4) wifi and username/password settings.

  • You may need to use your wireless router to find your zero 2w’s address. If you cannot find it, then use an ethernet cable to access it long enough to finish the configuration below.

  • For the uninitiated, you can use the hostname you set up in step 4 above postpended with .local. So if the hostname you configured was pi, you would type ssh username@pi.local (where username is whatever you provided in step #4 above) to connect…

  • Your initial connection will need you to trust the host key. So beware that this is a one time aspect of this connection…

  1. Type sudo bash so that you can have elevated privileges for the remaining steps in this guide
  • Alternatively you can pre-pend sudo to the front of all the commands in this guide
  1. Edit (this guide assumes you are familiar with either vi or nano) the file /boot/firmware/cmdline.txt and add the following just AFTER rootwait:
1
modules-load=dwc2,g_ether
  1. Edit (again, pick either vi or nano) the file /boot/firmware/config.txt and confirm it has an UNCOMMENTED line near the end that is otg_mode=1. Then add below the line [all] (it HAS to be after the [all] line for it to work properly) the following:
1
dtoverlay=dwc2
  1. Now we need to add our connection name by running the following command:
1
nmcli con add type ethernet con-name ethernet-usb0
  1. Now edit the file we just created (using vi or nano) named /etc/NetworkManager/system-connections/ethernet-usb0.nmconnection
  • You will be adding the lines for autoconnect and interface-name, and then modifying the line with method= to change auto to shared:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[connection]
id=ethernet-usb0
uuid=<random group of characters here created by nmcli>
type=ethernet
autoconnect=true
interface-name=usb0

[ethernet]

[ipv4]
method=shared

[ipv6]
addr-gen-mode=default
method=auto

[proxy]
  1. Create yet another new file (again with vi or nano) at /usr/local/sbin/usb-gadget.sh with the following contents:
1
2
3
4
5
6
7
#!/bin/bash

nmcli con up ethernet-usb0
sleep 1
ifconfig usb0 192.168.7.2 netmask 255.255.255.0
route add default gw 192.168.7.1

  1. Make this new file executable with the following command:
1
chmod a+rx /usr/local/sbin/usb-gadget.sh
  1. Create your last new file (using vi or nano) named /lib/systemd/system/usbgadget.service with the following text:
1
2
3
4
5
6
7
8
9
10
11
12
[Unit]
Description=My USB gadget
After=NetworkManager.service
Wants=NetworkManager.service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/sbin/usb-gadget.sh

[Install]
WantedBy=sysinit.target
  1. Run the following command to enable this new service:
1
systemctl enable usbgadget.service
  1. Reboot your zero 2w so all your changes can be put to use. Run the following command to reboot it:
1
shutdown -r now
  1. To test that this is working as expected, you can do the following on Linux PC(ubuntu):
1
2
sudo ifconfig usb0 192.168.7.1
sudo sysctl -w net.ipv4.ip_forward=1

Then ping 192.168.7.2
confirm you can successfully connect. If you can connect, then this means you can now power AND connect to your zero 2w over a simple Micro-USB to USB cable, no Wi-Fi required!

  1. In case if you met the renaming problem like me ,enxc2a0f04efdea: renamed from usb0:

Create a file in /etc/udev/rules.d/ eg: 99-pico-zero-2w-persistent-usb0.rules

1
SUBSYSTEM=="net", ACTION=="add", ATTRS{idVendor}=="0525", ATTRS{idProduct}=="a4a2", NAME="usb0",RUN+="/sbin/ifconfig usb0 192.168.7.1"

get 0525:a4a2 from lsusb

then

1
2
udevadm control --reload
udevadm trigger

also reboot your pi zero 2w

Background Info

Because wifi of zero 2w is too slow for me

Original reference address: https://github.com/verxion/RaspberryPi/blob/main/Pi5-ethernet-and-power-over-usbc.md

ESP32-s3 jetbrain clion startup

安装esp-idf

1
2
3
git clone -b v5.4 --recursive https://github.com/espressif/esp-idf.git
./install.sh esp32s3

会遇到些问题
比如 components/xxxx/yyy is not a directoy ,这是git submodule 没有完全下载完全导致

删除 components/xxxx/yyy ,然后重新运行

1
git submodule update --init --recursive

确保完整即可

在.bashrc里 增加一个 快捷命令

1
alias get_idf='. $HOME/work/esp32/esp-idf/export.sh'

具体文档: https://docs.espressif.com/projects/esp-idf/en/v5.4/esp32s3/get-started/linux-macos-setup.html

编译运行

以Hello world 为例

先在 terminal 里 运行

get_idf

然后

idf.py set-target esp32s3

会创建一个build目录

之后
idf.py menuconfig
如果需要做什么配置

之后
idf.py build

自动编译

编译完成 就是刷入

idf.py -p /dev/ttyACM1 flash

查看串口信息 可以用
idf.py -p /dev/ttyACM1 monitor

效果和 arduino IDE的 serial monitor 一样

退出 monitor不是 ctrl+C, 而是 ctrl+]

Jetbrain CLion

clion compiling

在 打开的时候,cmake settings 界面 暂时不需要什么设置

然后

在 clion的 菜单 Settings->Build,Exection,Deployment->Toolchains 里
增加一个,比如esp32s3, 右边点击 Add enviroment -> From File
内容就是 export.sh

1
~/work/esp32/esp-idf/export.sh

然后 在cmake 的settings里
可以设置:

1
2
3
Toolchain: esp32s3 <- 必须选择
Build directoy 为build
Enviroment: 里 增加 IDF_TARGET=esp32s3

手动 删除 目前任何残留的 build目录

重新 cmake 配置 即可

Flashing

cmake settings 里 Enviroment 里增加 ESPPORT=/dev/ttyACM1

然后在顶部菜单里,从hello_world.elf 换成 flash
点击 锄头的图标,就能完成刷写

Xiaomi Tm1703 Freebsd 14.1

/etc/rc.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
hostname="tm1703"
ifconfig_ue0="DHCP"
ifconfig_ue0_ipv6="inet6 accept_rtadv"
local_unbound_enable="YES"
sshd_enable="YES"
moused_enable="YES"
ntpd_enable="YES"
ntpd_sync_on_start="YES"
powerd_enable="YES"
# Set dumpdev to "AUTO" to enable crash dumps, "NO" to disable
dumpdev="AUTO"
zfs_enable="YES"
saver="blank"
blanktime="300"
dbus_enable="YES"
hald_enable="YES"
kld_list="i915kms"

#hcsecd_enable="YES"
#bthidd_enable="YES"
#sdpd_enable="YES"

blued_enable="YES"

uhidd_flags="-kmohsu"
uhidd_enable="YES"

sndiod_enable="YES"

/boot/loader.conf

1
2
3
4
5
6
7
8
9
10
11
kern.geom.label.disk_ident.enable="0"
kern.geom.label.gptid.enable="0"
cryptodev_load="YES"
zfs_load="YES"
blank_saver_load="YES"
sysctlinfo_load="YES"
snd_driver_load="YES"

vkbd_load="YES"
ng_ubt_load="YES"
ng_l2cap_load="YES"

/boot/device.hints

1
2
3
4
##for Headphone jack 
hint.hdac.0.cad0.nid33.config="as=1 seq=15 device=Headphones"
hint.hdac.0.cad0.nid18.config="as=2 seq=0 device=speakers"

new golang basis

First

1
2
3
export GO111MODULE=on
export GOPROXY=direct
export GOSUMDB=off

otherwise the dumbass golang will try to visit https://sum.golang.org/lookup/xxxxxx

Then directly go to work folder where gonna write go code

1
2
3
4
5
go mod init example.com/thebinaryname
go get github.com/cuu/yyyy@latest

...

the developers must got these ideas from nodejs development….

then create main.go ,the old days back

go build or go run main.go

Ultra Mobile Paygo(或T-Mobile)的WIFI CALLING开不起来了,怎么解决 2022?

手机端手动改DNS。最简单方便。

在连上WIFI的信号上,添加2条手动DNS

1
2
3
199.85.126.10

199.85.127.10

或是

1
2
3
4
5
208.54.49.131 depdg.epc.mnc260.mcc310.pub.3gppnetwork.org
208.54.36.3 ss.epdg.epc.mnc260.mcc310.pub.3gppnetwork.org
208.54.35.163 epdg.epc.mnc260.mcc310.pub.3gppnetwork.org
208.54.35.163 ss.epdg.epc.mnc260.mcc310.pub.3gppnetwork.org
208.54.87.3 ss.epdg.epc.geo.mnc260.mcc310.pub.3gppnetwork.org

格式规律是

1
epdg.epc.<服务商 mnc>.<服务商 mcc>.pub.3gppnetwork.org

mnc mcc 号码来自

https://en.wikipedia.org/wiki/Mobile_country_code

例如

1
epdg.epc.mnc260.mcc310.pub.3gppnetwork.org for MNC260 MCC310 (T-Mobile USA)

wifi calling

UDP 500,4500

TCP 143