0

Apologies for the tortured title but I'll explain the question more clearly:

We have a headless Pi compute module (CM4) which needs to be remotely configurable, and that includes being able to send it a command to configure a static IP address, gateway, netmask, etc. (or set it to DHCP) and have this setting persist across reboots.

I've done this before in various embedded Linuxes and there seems to be several ways of doing it depending on when the StackOverflow question was last asked and which specific Linux flavour you're using... so I am here asking what's the right way of doing this in 2022 on a CM4 running the latest Raspbian?

Ideally something doable / callable from within standard C code.

EDIT: No, we cannot just do this from the DHCP server as there's no guarantee there IS one in any given installation or that it's configurable, it's an operational requirement of THIS hardware that it can be set to a static IP configuration regardless of the network it's currently plugged into.

John U
  • 181
  • 11
  • 1
    Assuming you're using dhcpcd: Have you tried?: Create two dhcpcd.conf files in etc (e.g dhcpcd.static.conf, and dhcpcd.dynamic.conf), and then do a mv dhcpcd.dynamic.conf dhcpcd.conf to "swap" to the one you want. Afterwards, you'll need to tell dhcpcd to re-read its dhcpcd.conf file. – Seamus Oct 17 '22 at 15:03
  • @Seamus if by "using" you mean "whatever Raspbian uses by default" then yes, I'd suggest it's dhcpcd - I've seen the method of re-writing / generating a dhcpcd.conf file but wasn't sure if that is the "right" or most compatible / nicest way of doing it compared to (say) calling ip with popen() or something. – John U Oct 17 '22 at 15:13
  • 1
    RE "using": I had to ask because the Rpi org has decided to replace dhcpcd in the not-distant future, and of course some are moving already. Personally, I feel dhcpcd is the better choice, but... Anyway - yes, write one conf file for using dynamic address assignment, and another for static - if you feel you need a static config at all. – Seamus Oct 18 '22 at 05:47
  • It's easiest to do this on your ROUTER. Assign an IP address to your MAC address and your Raspberry can run dhcpcd as normal but will get a fixed IP address. – Dougie Oct 18 '22 at 11:45
  • @Dougie that's not the question being asked - the customer wants to be able to configure THIS device to have a fixed IP whenever & wherever you plug it in. This is not a debate about why you'd want to do that, it's an operational requirement we have to meet. – John U Oct 18 '22 at 12:10
  • The problem with "fixed IP whenever and whereever" is there's too many combinations. You can't assume everything uses 192.168.0.x or 192.168.1.x. There's also the 172.[16-31].x.x and 10.x.x.x networks to consider. Setting a fixed address on the router makes most sense. Setting it on the RPi makes no sense at all. – Dougie Oct 18 '22 at 14:54
  • @Dougie you are missing the point - the device/equipment gets installed and ONE OPTION the customer requires of it is that they can configure it to have a static IP, there's no argument/discussion about whether this is a good idea or not or whether other ways are "better". If they want to configure it badly or incorrectly that's their problem. – John U Oct 18 '22 at 15:48

0 Answers0