aoakley
Posts: 64
Joined: Fri May 11, 2012 10:27 pm
Contact: Website

Setting a 169.254.x.x IP for USB OTG g_ether in Stretch

Sun Dec 03, 2017 9:20 pm

Hi folks. I'm trying to set a static IP in the 169.254.x.x range for USB OTG using g_ether (an ethernet connection using a micro USB cable from a Pi Zero to a PC).

I'm using Raspbian Stretch Lite 2017-11-29 with apt updates/upgrades as of today 2017-12-03 .

Without an entry in /etc/dhcpcd.conf , g_ether works as expected and usb0 gets a random link-local IP on 169.254.x.x . So long as I can find out what that IP address is (or use zeroconf/avahi) that's fine, but I have an edge case (headless, and other zeroconf hosts elsewhere on another network) where I can't do that.

If I set the following in /etc/dhcpcd.conf it also works, but this isn't on the 169.254.x.x subnet so it's inconvenient to connect from my PC:

Code: Select all

interface usb0
static ip_address=172.16.31.4/29
I can happily set any static IP address for any of the usual private IP ranges; 192.168... , 10... , 172.16-32... etc. and they work fine.

However since my PC gets a 169.254.x.x link-local address, I also want to assign my Pi Zero usb0 to a static 169.254.x.x address. This does NOT work:

Code: Select all

interface usb0
static ip_address=169.254.64.64/24
Instead of working, what happens is that no IPv4 address is assigned to the usb0 interface.

Code: Select all

# ifconfig
...
usb0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::227f:8b8b:b84b:3c85  prefixlen 64  scopeid 0x20<link>
        ether 46:55:32:f6:f6:97  txqueuelen 1000  (Ethernet)
        RX packets 189  bytes 42208 (41.2 KiB)
        RX errors 0  dropped 4  overruns 0  frame 0
        TX packets 30  bytes 5143 (5.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
There are various guides on t'interwebs, including from the great Andrew Mulholland, that suggest that this SHOULD work. But it really, really doesn't.

Did this perhaps stop working in Stretch recently?

I can understand why DHCPCD might baulk at assigning a static IP in the link-local range. What I can't understand is why there are tutorials out there that swear that it does work.

I'm also aware that I can use multicast DNS (zeroconf/avahi) to work around this problem. Also I can set up a DHCP server on the Zero to assign the PC an address in a valid private IP range (that works really well). Also I can set up Internet Connection Sharing and have my PC assign the Pi Zero an IP address. I consider those to be workarounds - what I really want is a static 169.254.x.x address so there is absolute minimal fuss in plugging a Pi Zero into a random PC.

Please, help! This is driving me nuts.
Andrew Oakley - Cotswold Raspberry Jam, Gloucestershire - http://www.cotswoldjam.org

User avatar
DougieLawson
Posts: 34141
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Setting a 169.254.x.x IP for USB OTG g_ether in Stretch

Sun Dec 03, 2017 9:23 pm

The netmask for 169.254.xxx.xxx is not /24. It is /16.

There should be no problems setting it with sudo ifconfig eth0 169.254.3.14/16
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

mattmiller
Posts: 1954
Joined: Thu Feb 05, 2015 11:25 pm

Re: Setting a 169.254.x.x IP for USB OTG g_ether in Stretch

Tue Jan 16, 2018 9:13 pm

Late reply but I've also found that we can't set a static address (using dhcpcd.conf) in the 169.254.0.0/16 address space in Stretch

But I've found a very consistent workaround which basically just uses dougies idea in a startup script

Code: Select all

nano .config/lxsession/LXDE-pi/autostart
and add

Code: Select all

@sudo ifconfig usb0 169.254.3.14/16
to the end

User avatar
thagrol
Posts: 869
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Setting a 169.254.x.x IP for USB OTG g_ether in Stretch

Wed Jan 17, 2018 6:13 pm

You might want to rethink where you're putting that command. Isn't .config/lxsession/LXDE-pi/autostart a per user run on login/desktop start script?

With no-one logged in to the desktop it won't be run and USB0 won't be (re)configured.

/etc/rc.local or, perhaps, a systemd service might be better places.
Note to self: don't feed the trolls
If you believe "L'enfer, c'est les autres" (Hell is other people) have you considered that it may be of your own making?

mattmiller
Posts: 1954
Joined: Thu Feb 05, 2015 11:25 pm

Re: Setting a 169.254.x.x IP for USB OTG g_ether in Stretch

Thu Jan 18, 2018 12:38 am

You might want to rethink where you're putting that command. Isn't .config/lxsession/LXDE-pi/autostart a per user run on login/desktop start script?

With no-one logged in to the desktop it won't be run and USB0 won't be (re)configured.
Sounds like a fair point - I'm a desktop user so I wouldn't have noticed

Return to “Raspbian”