5

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.

5
  • 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, 2018 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.). Jul 16, 2018 at 17:52
  • 1
    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, 2018 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 Jul 16, 2018 at 22:38
  • Yes, always wifi. Jul 16, 2018 at 23:12

2 Answers 2

2

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)

12
  • 1
    I'm sorry, but this is the wrong answer, and will lead to issues. It's also diametrically opposed to the instructions in man dhcpcd.conf: For IPv4, you should use the inform ipaddress option instead of setting a static address. For an explanation as to why this is so, pls see RFC 2131, esp Section 3.4. To be clear: Your static IP answer will work - but not by itself. The DHCP server must be configured properly also.
    – Seamus
    Mar 25, 2022 at 6:17
  • @Bravo: Yeah - when I first saw it, I set a reminder 4 years in the future. Thought I'd give him time to reconsider. 4 years gone by; time to change :) lol No - truth is that this bothers me from time to time because it's a persistent & widespread fallacy that sometimes will work for a while. Even the 'official' docs have got it wrong. I posted an answer a few days ago. After reading a couple of comments, I realized I wasn't getting through, and wrote up a more detailed explanation.
    – Seamus
    Mar 28, 2022 at 5:46
  • I'd remove it if I could ... perhaps I should edit ?
    – Bravo
    Mar 28, 2022 at 9:36
  • @Bravo: I'm sorry, but I wasn't advocating removal - or even an extreme edit. I only wanted to make the point that using the fixed ip option in dhcpcd.conf is a recipe for disaster unless it is accompanied by still further changes in the configuration of other network services. It's impossible to say precisely what those changes are because that depends on how the network is set up, and how these services are implemented. Maybe a lesser edit would be better?
    – Seamus
    Mar 31, 2022 at 16:23
  • 1
    @Bravo I have rolled your edit back. Please do not do that again, no matter how wrong you think an answer is -- or do you want the door opened such that anyone who disagrees with one of your answers can just do a vandal edit, and everybody has to sit back and watch Broken Windows 17 because we have to respect everyone's right to just strike over other people's content? That is not the way to deal with these things. Downvote and leave a comment -- which your comments here are commendable, I agree this is a problematic answer.
    – goldilocks
    Mar 31, 2022 at 18:06
2

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.

1
  • I have updated my question. Jul 16, 2018 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.