iandk
Posts: 7
Joined: Tue Sep 10, 2019 4:42 pm

[headless] Set usb0 ip in cmdline

Tue Sep 10, 2019 4:49 pm

Hi,

I'm currently wondering how to setup a static IP for the USB OTG connection to my PI Zero W/ PI 4?
I don't want to depend on the bonjour/ .local addresses, but rather have a static IP for the usb0 connection.

Is it somehow possible to set a static IP for it in the cmdline in order to set it headless?



Thanks!

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

Re: [headless] Set usb0 ip in cmdline

Tue Sep 10, 2019 9:25 pm

iandk wrote:
Tue Sep 10, 2019 4:49 pm
Hi,

I'm currently wondering how to setup a static IP for the USB OTG connection to my PI Zero W/ PI 4?
I don't want to depend on the bonjour/ .local addresses, but rather have a static IP for the usb0 connection.

Is it somehow possible to set a static IP for it in the cmdline in order to set it headless?



Thanks!
You do it the same way you'd do it for any other network interface:

Edit /etc/dhcpcd.conf and add something like this:

Code: Select all

interface usb0
static ip_address=192.168.99.106
static routers=192.168.99.3
static domain_name_servers=192.168.99.3
Obviously, you'll need to chage the actual IP addresses to match your network.

The above will only set the IP address etc on the zero end. You may need to do something similar on the USB host end but use a different IP address to the one used on the zero and to the IP address(es) used by any other interfaces on it.

If you want to acces the zero from machines on your network other than the USB host, you'll also need to setup bridging or routing on it (the USB host that is).
This space unintentionally left blank.

iandk
Posts: 7
Joined: Tue Sep 10, 2019 4:42 pm

Re: [headless] Set usb0 ip in cmdline

Wed Sep 11, 2019 3:23 am

Thanks! But I want to set it up before the first boot, in the cmdline.txt,

ejolson
Posts: 3546
Joined: Tue Mar 18, 2014 11:47 am

Re: [headless] Set usb0 ip in cmdline

Wed Sep 11, 2019 3:55 am

iandk wrote:
Wed Sep 11, 2019 3:23 am
Thanks! But I want to set it up before the first boot, in the cmdline.txt,
A Debian-style initial RAM disk will parse an ip= directive in the kernel command line, fiddle with it and then pass it to the corresponding BusyBox utility. This can be used to give usb0 an IP number which can then allow you to pivot root to an NFS export mounted over the Ethernet gadget.

If you are booting off an SD card without a RAM disk, you could write a startup script that parses your own command-line option and sets the interface accordingly. To do this, I would replace the symbolic link from init to systemd in sbin with a shell script that sets up the usb0 network device and then execs systemd. There may be a more elegant way to do this directly, but the systemd documentation seems impossible for me.

iandk
Posts: 7
Joined: Tue Sep 10, 2019 4:42 pm

Re: [headless] Set usb0 ip in cmdline

Wed Sep 11, 2019 4:51 am

How can I specify the interface for the „ip=„ value?
Since the interface is usb0 rather than eth0/wlan0

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

Re: [headless] Set usb0 ip in cmdline

Wed Sep 11, 2019 11:46 am

iandk wrote:
Wed Sep 11, 2019 3:23 am
Thanks! But I want to set it up before the first boot, in the cmdline.txt,
I'm not at all sure that you can, and why do you need to? It wouldn't take effect until the network is up anyway.

Is you aim to be able to write an SD card from an image then set a static IP address before ever inserting the SD card into the zero and booting it? If so, I can think of a couple of ways to achieve that. Unfortunately both require the ability to read and write to an ext4 partition which is likely to be a problem if you don't have access to a linux computer.

  1. Mount the root partition (the second one) of the SD card and edit /etc/dhcpcd.conf as I suggested above.
  2. Create a customised OS image
    1. Mount the root partition of the SD card
    2. Add the following to /etc/rc.local making sure it is above the "exit 0" line

      Code: Select all

      if [ -e /boot/static_ip.txt ]; then
          cat /boot/static_ip.txt >> /etc/dhcpcd.conf
          rm /boot/static_ip.txt
      fi
      
    3. Take an image of the SD card for future use.
    4. Add a file called static_ip.txt to the boot partition containing the IP address configuration you want to use
    5. Insert SD card into the zero and boot
This space unintentionally left blank.

ejolson
Posts: 3546
Joined: Tue Mar 18, 2014 11:47 am

Re: [headless] Set usb0 ip in cmdline

Thu Sep 12, 2019 3:03 am

iandk wrote:
Wed Sep 11, 2019 4:51 am
How can I specify the interface for the „ip=„ value?
Since the interface is usb0 rather than eth0/wlan0
The value for the ip= flag on the kernel command line is a string with seven colon-delimited fields. The sixth field specifies the network device. Other fields are used for the network mask, default gateway and hostname among other things. A somewhat complicated example in which five Pi Zero computers are booted using rpiboot over USB with statically assigned IP numbers configured by the above mentioned command-line option is available here.

iandk
Posts: 7
Joined: Tue Sep 10, 2019 4:42 pm

Re: [headless] Set usb0 ip in cmdline

Mon Sep 23, 2019 4:20 am

Is it possible to set both an IPv4 and IPv6 address using the ip= command in the cmdline?

Return to “Troubleshooting”