2

I have a headless raspberry pi zero. It is configured to be connected to home network and my mobile hotspot (Android phone). When I am home, I would like it to get a static IP, where I know I can connect to it (works fine).

But when it connects to my mobile hotspot, it can't get that static IP. In that case, I want it to fall back to dynamic IP. How can I configure dhcpcd.conf to achieve that?

Most information I can find is the other way around (when DHCP server fails). Some claimed that this is impossible (which I doubt). I don't want to change dhcpcd.conf each time I move from one network to another.

Why is this such a problem? Why can't I configure mobile hotspot to accept same static IP? Android is simply primitive in this regard. It only allows a certain IP range (192.168.43.0/24). But I need to connect to the pi on the mobile network. I can find the dynamic IP either on the 16x2 display or with nmap. The weird issue is that notification on my phone tells me that there are 2 clients connected but nmap, doesn't find the rpi. I suppose on physical/data link layer RPi is connected but can't get the static IP. Only way to get around is to make sure RPi can get a dynamic IP, if static IP doesn't work out.

Edit - Summary of the discussions below: It is not possible to detect failed static IP. Client act like the IP belongs to it, while router won't deliver the packages to that IP. Client things that it is connected, while it is not. Actually, DHCP server delivers IP addresses. But my router's DHCP server is aware of clients requesting static IP and allows it (seen under DHCP clients at router's web panel), while other clients request a dynamic IP. However, my router doesn't allow a fixed IP on specified clients (e.g. my mac address).

I am looking for a configuration like it is done at Apple Mac OS, where users can define dynamic or static IP for each WiFi access point separately.

  • I think you can get it working with dhcpcd but the other way around. First ask for dhcp. If there's no dhcp service working then fallback to static. Check the config file for dhcpcd (etc/dhcpcd.conf?). It has plenty of information. – eftshift0 Jul 16 '18 at 17:34
  • That doesn't work because at home there is dhcp working but clients can ignore it and ask for a static IP. Raspberry Pis do that. But I don't want other clients to be using static IP (guests, phones, notebooks etc.). – Genom Jul 16 '18 at 17:52
  • Analyzing from a pure "development" point of view, it makes no sense. dhcpcd can detect if there's no DHCP service around (because there's no DHCP request being answered, right?) and then fall back into a given static configuration. But there's no actual way to know if a static configuration fails to go into something else. Any device can be set to use any static network configuration at any given time and there's no way to make sure if it actually is OK or not (that's on layer-3, at least... on higher layers you notice it's not working because no application works but.....). – eftshift0 Jul 16 '18 at 19:04
  • "Client act like the IP belongs to it" being statically assigned, it does. Anyway, does your pi always connect via wifi? I assume it does from the last bold statement, but want to be sure – Jaromanda X Jul 16 '18 at 22:38
  • Yes, always wifi. – Genom Jul 16 '18 at 23:12
3

I'm assuming you run latest Raspbian i.e stretch

Your last sentence asks "define dynamic or static IP for each WiFi access point separately" - you can use /etc/dhcpcd.conf to achieve this, in that you can define static IP address for a given SSID (one or more, simply repeat for each SSID), for any other SSID's will default to dynamic IP

Simply add to the end of that file, something like

ssid your-ssid
    static ip_address=192.168.1.19/24
    static routers=192.168.1.1
    static domain_name_servers=192.168.1.1

(of course, change your-ssid and the IP addresses as appropriate to your needs)

0

Seems there is some misunderstanding of a static ip address. This isn't given by any dhcp server. It is configured statically in local config files like /etc/dhcpcd.conf or /etc/network/interfaces or /etc/systemd/network/iface.network. And yes, it is static and will never change and be overwritten by a dhcp response.

But I understand your problem. You can fix it by using - I call it - a "fix ip address" that is given by the dhcp server. Configure your raspi using a dhcp client to get ip addresses from a dhcp server, not static ip address at all. That's the default. Most dhcp server can give a specific host always the same ip address. The host is identified by it's mac address. So when you are at home, configure that dhcp server to give your raspi a "fix ip address". You have to look how it is done on your dhcp server. For example the most used dhcp server on a raspi is dnsmasq. In its config file man dnsmasq.conf you find this example:

# Always allocate the host with ethernet address 11:22:33:44:55:66
# The IP address 192.168.0.60
dhcp-host=11:22:33:44:55:66,192.168.0.60

You can also set other options using the host name or give an infinite lease or others. Have a look at the man page. But you have to configure the dhcp server on your home router.

  • This seems to be the correct approach for anyone else. But my ISP gives a shitty modem/router to me. Which means that I can't configure DHCP server on the router to give a fix IP address to the clients. Interestingly, my clients somehow tell the router that they want a static IP. On the list of DHCP clients at the web page of the router, I see those clients listed with "STATIC IP". So DHCP server or the router is aware of this somehow. I was wondering, if I can configure my WiFi to obtain static or dynamic IP at diffferent APs like you can do at Apple Macs? – Genom Jul 16 '18 at 19:40
  • I have updated my question. – Genom Jul 16 '18 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.