I have a RPi zero W with Waveshare ETH/USB hat. An USB LTE dongle is connected to that hat.
Both are recognized as a ETHxx port, but sometimes they switch names (ETH0 and ETH1). I cannot configure them right when they don't have persistent names.
I read about it on this forum, and with Hotplug this problem can be solved
I tried to understand and configure it, but unfortunately failed. Can someone help me with step-by-step instructions?
This hotplug script should swap eth0 and eth1 if the MAC address of eth1 is not the expected one.
I assume the interface names are lowercase. Otherwise, fix them accordingly.
cat << "EOF" > /etc/hotplug.d/iface/10-swap_eth_ifaces
[ "$ACTION" == "ifup" -a "$DEVICE" == "eth1" ] || exit 0
eth1mac="00:11:11:33:44:55" # The correct eth1 MAC address here
currmac=$(ip link show dev eth1 | grep link | awk '{print $2}')
if [ "$eth1mac" != "$currmac" ]; then
ip link set eth0 down
ip link set eth0 name tmp
ip link set eth1 down
ip link set eth1 name eth0
ip link set eth0 up
ip link set tmp name eth1
ip link set eth1 up
exit 0
else
exit 0
fi
EOF
The code you provided works great on the eth devices. I expected it to work also on the two wlan devices (RPi has one and there is one USB wifi device). But for some reasons that doesn't work well.
When starting the router without the usb device connected, wlan0 is the RPi onboard wifi devices. But with the usb wifi device connected at startup, wlan0 is the usb device, and wlan1 is the onboard RPi device.
Your code swaps the devices when eth1 does not have the expected mac-address. I guess another way could be that at startup or when an usb device is connected, the (new) device will get it's name (wlan0, wlan1) based upon the mac address.
When configuring a wifi-iface (in /etc/config/wireless), you can define a name for the corresponding wlan device (option ifname 'wlan0'). Connect this wifi-iface with the network, which has eth0 as its wired interface. That's all. Good luck.
It doesn't solve the problem.
After booting with the usb wifi device disconnected, RPi onboard wifi device = wlan0
After booting with the usb wifi device connected, RPI onboard wifi device = wlan1, usb wifi device = wlan0
So the names are not persistent.