pleriche
Posts: 90
Joined: Mon Oct 14, 2013 8:44 am

IP config depending on wireless network

Thu Jun 20, 2019 3:06 pm

I have a Pi which I connect to two different WiFi routers according to the location. For simplicity, I want to use a fixed IP address for the Pi but the two routers have different IP addresses, requiring a different default router in the Pi IP configuration.

Under Wheezy this was quite easy to set up. In wpa_supplicant.conf I could associate an id with each network and use these ids in /etc/network/interfaces to specify different IP configurations, but I recently upgraded to the latest Stretch where this doesn't work, as the IP config is set up in /etc/dhcpcd.conf.

Can anyone please point me to an equivalent mechanism?

Andyroo

Re: IP config depending on wireless network

Thu Jun 20, 2019 3:43 pm

Assuming you have two SSIDs WLAN1 (192.168.1.x) and WLAN2 (192.168.2.x) try something like this in /etc/dhcpcd.conf:

Code: Select all

ssid WLAN1
static ip_address=192.168.1.10/24
static routers=192.168.1.254
static domain_name_servers=192.168.1.2

ssid WLAN2
static ip_address=192.168.2.200/24
static routers=192.168.2.1
static domain_name_servers=192.168.2.1

pleriche
Posts: 90
Joined: Mon Oct 14, 2013 8:44 am

Re: IP config depending on wireless network

Fri Jun 21, 2019 9:08 pm

Thank you @Andyroo, that seems to be what I want. I hadn't spotted the ssid directive in dhcpcd.conf(5).

But that man page is rather sparse and leaves me with several questions:

So what is the scope of an ssid option (or for that matter an interface option or a profile option)? The man page simply says "subsequent options". The examples imply it might be until the next blank line. There's no option to cancel the effect of any of those options, only to override them with another similar.

In the example you gave, don't I also need an interface wlan0 option? And how does the scope of that interact with the scope of the ssid option?

I tried inserting the ssid options as per your example (with and without the interface option) and now it's totally broken, even putting dhcpcd.conf back how it was (as far as I can remember). Possibly something to do with pulling the plug when I couldn't get a connection at all with which to shut it down, and a subsequent reboot which took an age doing fsck. lsusb still shows the WiFi adapter but ifconfig doesn't show wlan0.

Return to “Advanced users”