Multiple static IPs for different wi-fi connections on Linux. Is it possible?

Hi again, it's always me struggling with Linux (and Raspi in particular). My question is what's asked in the topic's title: can I set multiple static IPs for different wi-fi connections? Thanks.

Do you mean you'll have multiple wireless adaptors? Then yeah, you have to give them different IPs.

a short google gave me this: https://www.raspberrypi.org/forums/viewtopic.php?f=26&t=31003

answer: yes you can. Either using a GUI or by editing your local config files.

Or do you mean you want a different IP based on which wi-fi network you're connected to? Because that would be much easier to configure on the DHCP server for each network. But I suppose you could write a script which is run when the wireless interface is up which will check the SSID and assign a configuration. But again, configuring the DHCP server would be easier.

EDIT: Ignore this nonsense, it's been a while since I've had to set a static IP on a wireless network.

@mauli I've edited already the dhcpcd.conf so the interfaces file would be redundant to edit and also is not suggested to edit it. I did many researches but didn't had any luck.

@Dexter_Kane I want to use just one adapter and "automagically" change the static IP.

please edit either the wpa_supplicant or ifconfig. the dhcpd is the dhcp deamon running on your raspberry/machine that GIVES away adresses for devices that connect to the network where your raspberry/machine is managing the dhcp

for example: http://raspberrypi.stackexchange.com/questions/11631/how-to-setup-multiple-wifi-networks

1 Like

I've already set up correctly a connection to multiple wi-fi networks in the wpasupplicant.conf. But, since I have a static IP set in the dhcpcd.conf file I can't access it through the other connection.

P.S. I used that guide to set up the wpasupplicant.conf

what do you mean by that, what do you want to access, do you want multiple connections to be bridged?

could you show us your config file(s) and a diagram of what you want?

All I can think of is that you want to aggregate your wifi connections to get faster transfers/redundancy?

If so then you'll need network equipment that can do aggregation.


This is a "diagram" (I guess) of what I want to do, pretty easy I think. What config files you'd want to see?

@shu_kaze Nothing as fancy as that lol

Think of two possible ways for a client to obtain an IP, it can generate a DHCP request and acquire one from a DHCP server, or it can assign itself one. Now, if you can configure your DHCP Server, which would be the router that connects your client to the internet, to assign an IP Address automatically to your clients MAC address, your job is done. All you have to do is set the DHCP to hand out that specific static IP you want. Unfortunately, you are using a mobile phone as your DHCP server for one of the connections, so it's not that simple (unless you have a rooted phone and get a tethering management app).

You will have to configure the static IP for that connection to take place client-side. There may be a very easy way to do this, probably with shell scripting.

"#!/bin/sh
GATEWAY_DEFAULT=$(ip route list | sed -n -e "s/^default.[[:space:]]([[:digit:]]+.[[:digit:]]+.[[:digit:]]+.[[:digit:]]+)./\1/p")
echo ${GATEWAY_DEFAULT}"

That will get the default gateway. Then you need to check IF {GATEWAY_DEFAULT} = [Phone default gateway] then replace the IP with something like:

ifconfig eth0 192.168.2.123 netmask 255.255.255.0

There may be a more graceful way, but that's how I would do it.

I don't so I think that I can't go that route unfortunately.

I've already set the static IP client side editing the dhcpcd.conf file for bot eht0 and wlan0 connections.

^^This is a script right? Are you sure it won't overwrite permanently the static IP I'm already using at home?
I'm a newbie when it comes to Linux so I might be repetitive or not understand immediatly what to do. Thanks a lot for the detailed answer.

That's just some code to show an example of what you would do with scripting. Give me a couple hours I'll write an actual script for you if you need it.

If I understand correctly, you want your RP to be on one static IP when it's connected to the internet through one network, and another static IP when it's connected to another network, but it connects to both networks with the same network adapter (at different times of course).

Therefore, my script idea was just to say "If I'm on network A, then my address is X. If I'm on network B, my address is Y."

That would be the easiest way to set your different IP addresses. Unless you have multiple NICs, you can only be connected to one network at a time, so all your script has to do is figure out which network your on, and change the IP. Easy, right?

this file for example would be a nice to have indeed.

You wouldnt want to do it that way, it needs user interaction, and thats precicely what we dont want nor have at that moment of time (when not having a pre defined IP in a network, sure we have a hostname, but don't rely on that)

@MetalizeYourBrain please further give your current configs/contents of:

  • /etc/wpa_supplicant/wpa_supplicant.conf (if there are any passwords, try to censor them)
  • /etc/network/interfaces
  • /etc/dhcpcd.conf

simple way: configure all networks to have the same subnet, for example: 10.1.0.0/24 and change config to have one single static IP on the wlan0 interface.

You do realize that android has a hotspot feature, you don't need to root your phone for other devices to connect to it's network.

As for your diagram, you can just set up two connections, one to your home network and one to the mobile hotspot network. However depending on your mobile you may have to request a static ip from the raspberry pi then at your access point.

Some simple googlefu:

http://www.instantsupportsite.com/self-help/raspberry-pi/raspberry-connect-multiple-wireless-networks/

@mauli Here are those configs file as I have them set right now:

1)WPA Supplicant

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US

network={
        ssid="NETGEAR45"
        psk="password_goes_here"
        key_mgmt=WPA-PSK
        id_str="home"
}

network={
        ssid="XT1032 3680"
        psk="password_goes_here"
        key_mgmt=WPA-PSK
        id_str="phone"
}

2) Interfaces (this file has not been modified by me)

# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.con$

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

iface eth0 inet manual

allow-hotplug wlan0
iface wlan0 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

allow-hotplug wlan1
iface wlan1 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

3) DHCPCD.conf

# A sample configuration for dhcpcd.
# See dhcpcd.conf(5) for details.

# Allow users of this group to interact with dhcpcd via the c$
#controlgroup wheel

# Inform the DHCP server of our hostname for DDNS.
hostname

# Use the hardware address of the interface for the Client ID.
clientid
# or
# Use the same DUID + IAID as set in DHCPv6 for DHCPv4 Client$
#duid

# Persist interface configuration when dhcpcd exits.
persistent

# Rapid commit support.
# Safe to enable by default because it requires the equivalen$
# on the server to actually work.
option rapid_commit

# A list of options to request from the DHCP server.
option domain_name_servers, domain_name, domain_search, host_$
option classless_static_routes
# Most distributions have NTP support.
option ntp_servers
# Respect the network MTU.
# Some interface drivers reset when changing the MTU so disab$
#option interface_mtu

# A ServerID is required by RFC2131.
require dhcp_server_identifier

# Generate Stable Private IPv6 Addresses instead of hardware $
slaac private
# A hook script is provided to lookup the hostname if not set by the DHCP
# server, but it should not be run by default.
nohook lookup-hostname

interface wlan0
static ip_address=192.168.0.15/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1

interface eth0
static ip_address=192.168.0.16/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1

These are the files you requested. The subnet on my phone is 192.168.1., at home is 192.168.0. so that's were the problem is. I don't know if it's possible to change the subnet on the phone and at home would be a total mess if I change it.

@shu_kaze I know I don't need root to connect to the wi-fi hotspot on my phone and I already set up the Raspberry in order to connect automatically at home and to my phone. The problem is that I'd like to use two different static IPs for those connections because both connections have different subnet.

it should be, check when the hotspot is active if there is an additional menu

I've checked before posting a question here and do some researches aand it's still not available now lol

have a custom rom running (Pacman) so coudln't check for myself.

remove the wlan0 part of dhcpcd.conf because the static IP is obviously what we don't want. Then follow the guide to finally change settings in /etc/network/interfaces to use iface home inet static and iface phone inet static

So I have to edit the interfaces file, I can't do differently right?