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.

Saren Tasciyan
  • 181
  • 1
  • 6
  • 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.). – Saren Tasciyan Jul 16 '18 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 '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. – Saren Tasciyan Jul 16 '18 at 23:12

2 Answers2

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.

Ingo
  • 42,107
  • 20
  • 85
  • 197
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)

goldilocks
  • 58,859
  • 17
  • 112
  • 227
Jaromanda X
  • 2,415
  • 1
  • 13
  • 14
  • 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 '22 at 06: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 '22 at 05:46
  • I'd remove it if I could ... perhaps I should edit ? – Bravo Mar 28 '22 at 09: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 '22 at 16:23
  • @user68186: This is not an opinion. man dhcpcd.conf is explicit: For IPv4, you should use the inform ipaddress option instead of setting a static address. Your comment however, reads very much like an opinion because it assumes facts not in evidence; e.g. what if the user has no control over the "dynamic IP address pool". You could be useful if you would get your facts squared away, and quit picking at imaginary nits. – Seamus Mar 31 '22 at 16:28
  • @user68186: "alarmingly"... perhaps that should tell you something. You can view the list that approved the edit; perhaps take your issues up with them, eh? Oh, btw - it's Bravo, not Brovo :) – Seamus Mar 31 '22 at 17:25
  • 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 '22 at 18:06
  • @user68186 Why don't you let it go? - you don't have all the facts, and you have gone 'full nuisance mode'. – Seamus Mar 31 '22 at 19:01
  • @Seamus I don't have all the facts and neither do the original poster who accepted the so called "wrong" answer. I have deleted my nuisance comments. Can you admit that your "wrong answer" comment led to the "vandal edit" and delete it? I guess not. If I am wrong I will delete this comment. – user68186 Mar 31 '22 at 19:16
  • @user68186: No - and you remain a total nuisance. I stand by each and every post I've made here. We are all encouraged to leave a comment when we downvote an answer. That's what I did. – Seamus Mar 31 '22 at 19:58
  • @Seamus I thought so. When you can't find words like "must" of "deprecated" in your argument you descend to pointing at typos and calling names. You are an upstanding netizen of this community. – user68186 Mar 31 '22 at 20:34