2

I have a raspberry pi (jessie) which I would like to use both at home and at another location.

In case the raspberry pi is used at home then it should connect to my wifi router and use a specific static IP address.

In case the raspberry pi is used at the other location it should connect to that wifi router using dhcp.

How can I set this up so that it automatically properly connects to the wifi router when I plug the pi in at home (with static IP) or at the other location (using dhcp) ?

FYI I have already configured my pi properly for my home wifi router with static IP address.

My /etc/dhcpcd.conf :

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

# Allow users of this group to interact with dhcpcd via the control socket.
#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 ClientID as per RFC4361.
#duid

# Persist interface configuration when dhcpcd exits.
persistent

# Rapid commit support.
# Safe to enable by default because it requires the equivalent option set
# 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_name
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 disabled by default.
#option interface_mtu

# A ServerID is required by RFC2131.
require dhcp_server_identifier

# Generate Stable Private IPv6 Addresses instead of hardware based ones
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

# JVA 2017-11-15 :  following lines added for static IP address
interface wlan0

static ip_address=192.168.1.133/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1

nohook wpa_supplicant

My /etc/wpa_supplicant/wpa_supplicant.conf

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

network={
        ssid="bbox_jan"
        psk="xxxxxxx"
        key_mgmt=WPA-PSK
}
JanVdA
  • 121
  • 1
  • 4

2 Answers2

5

I'm answering a bit late I think but a solution exists, and I think it's worth mentioning it.

For each interface, it's possible in dhcpcd.conf to specify an SSID for which the configuration below will be valid. To do so:

  • First stop dhcpcd (e.g run sudo systemctl stop dhcpcd)
  • Open /etc/dhcpcd.conf
  • Under your interface configuration write ssid <name_of_network>
  • Write your static IP/router/etc. configurations for the aforementioned network.
  • Repeat for each specific SSID you want to configure.
  • Save your file, and run the command sudo systemctl daemon-reload && sudo systemctl start dhcpcd

Here is an extract of a dhcpcd.conf file of one of my Pi's. It should better illustrate this answer:

[...]

# fallback to static profile on eth0
#interface eth0
#fallback static_eth0

interface wlan0
    # work's dev network static IP
    ssid work-dev-wifi
    static ip_address=192.168.0.186/24
    static routers=192.168.0.1

    # Gateway access point static IP
    ssid IoT-Gateway-Ap
    static ip address=10.0.0.4/24
    static routers=10.0.0.4

[...]

Normally it should do the trick !

Resethel
  • 51
  • 1
  • 4
0

I still don't understand the fixation Pi users have with static IP addresses.

In your question it seems particularly unusual; static IP addresses are assigned independently of any network - even if there is NO network present.

What you want to do is not possible - the OS has no way of knowing whether the static IP address is appropriate, and if assigned there will be no DHCP check, although you could possibly write a script to check.

It is possible to assign a static IP address if DHCP fails using a Fallback profile; See How to set up networking/WiFi

Milliways
  • 62,573
  • 32
  • 113
  • 225