0

I’m currently working on a portable headless project with a Raspberry Pi Zero W, on which I have installed Arch Linux, and am currently using systemd-networkd to connect it wirelessly to my home router. I want to be able to have it auto-connect to a prioritised pair of SSIDs using different static IPs for each SSID, for the following use case:

When I’m using it away from home, I need it to automatically connect to my Android device’s wireless hotspot using a static IP (so that I can easily SSH or VNC into it from my phone).

When I’m at home I also need to be able to SSH into it from my home LAN to perform administration, again using a static IP for ease of SSH access.

However, when I’m at home I also need to be able to connect it to my phone for testing purposes and to have it automatically switch between SSIDs based on the presence or absence of my Android hotspot.

To add to the complications, my home router uses a different IP range from my Android phone, so the static IP will need to be different on both connections.

Ideally, I need the Pi to work as follows:

  • If it detects my Android Hotspot’s SSID, it will connect to that exclusively, using a static IP address specific to that SSID
  • If it doesn’t detect my Android Hotspot’s SSID it will instead attempt to connect to my home Wifi SSID using a different static IP

This means that I can test the Pi at home using my phone simply by switching on my phone’s hotspot, and I can administer it using my home PC by switching the hotspot off.

Does anyone know how I can achieve this?

1 Answer 1

0

Finally worked out how to do it with Network Manager, using nmcli. Set up two profiles as follows:

nmcli con add con-name "[Connection Name]" ifname wlan0 type wifi ssid "[SSID]" 
ip4 [Static IP Address]/24 gw4 [Default Gateway]
nmcli con mod "[Connection Name]" ipv4.dns "8.8.8.8 8.8.4.4"
nmcli con mod "[Connection Name]" wifi-sec.key-mgmt wpa-psk
nmcli con mod "[Connection Name]" wifi-sec.psk [Passkey]
nmcli con mod "[Connection Name]" connection.autoconnect-priority 0

Ensure that the connection for my phone wifi has a higher autoconnect-priority than my home wifi. Then just put both connections up with

nmcli con up "[Connection Name]"

Then Network Manager automatically decides which to use based on priority first then availability. To switch from my home wifi to phone wifi, I switch on my phone's hotspot and restart the NetworkManager service; To switch back again, I switch off my phone's hotspot.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.