1

Is it possible to set ONLY the raspberry pi's IP address as static, and request the gateway from the DHCP server?

I have devices that connect to the internet through mobile hotspots. They are always in the ip range 192.168.43.xxx but depending on some phones, the gateway can change i.e. 192.168.43.1 or 192.168.43.254 etc.

2
  • The usual way to do that is to reserve the RPi's address on the DHCP server (based on assigning a specific address to a specific hardware MAC address). The RPi just runs out of the box with nothing special and gets a fixed IP address that way.
    – Dougie
    Commented May 27, 2020 at 19:24
  • Should have mentioned it's an android hotspot. There is no way to change the config from the DHCP server side
    – ldmo
    Commented May 27, 2020 at 19:52

1 Answer 1

2

If you have little control over the network from using the mobile access point, you can create a script to check the wlan you are connected to and set a route by putting the script in /usr/lib/dhcpcd/dhcpcd-hooks/

You can get the connected network from

ifconfig wlan0

and the set the IP address etc within this script. In my mind, this is a little messy but very flexible.

One other way would need a network with only the Pi and the access point on it (unless you can make sure any other device does not get ANY possible gateway address) by using the arping option in dhcpcd.conf. This example assumes the first gateway is on .1 and the other one being on .254 and no other device can occupy these addresses and in both cases the Pi needs to have the IP address of .44

interface wlan0
arping 192.168.1.1
arping 192.168.1.254

profile 192.168.1.1
static ip_address=192.168.1.44/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1

profile 192.168.1.254
static ip_address=192.168.1.44/24
static routers=192.168.1.254
static domain_name_servers=192.168.1.254

The last way would be a mix. Set the static IP in dhcpcd.conf and use the hook to set up the route etc but this is so messy I'm loathe to go further with it :)

TBH, if my mobile network was requiring this level of complex control I would look to use a Pi as an access point giving you more control over DNS and DHCP or look to a router with a 3G/4G option that can have static addresses set up in it. TP-Link and Draytek have a few Options that can be used (I have seen both in action on building sites and small temporary warehouse / office cabins) but I'm sure others do them (e.g. Cisco).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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