Code: Select all
interface eth0
static ip_address=192.168.1.101/24
static routers=192.168.1.1
However dhcpcd.conf offers more features than network/interfaces, and we can now change the static IP address in various circumstances.
The most useful of these has dhcpcd probe the network looking for the router. Depending on which router it finds, it then configures a static IP address to suit:
Code: Select all
######################################################
# TEMPLATE: A different IP address on each network
#
# The arping address should be the router
# or some other machine guaranteed to be
# available. You need to know the addresses
# of the servers. If none of the arpings find
# an active machine then you will get a DHCP
# allocation.
######################################################
interface eth0
arping 192.168.2.1
arping 192.168.0.254
profile 192.168.2.1
static ip_address=192.168.2.44/24
static routers=192.168.2.1
static domain_name_servers=192.168.2.1
profile 192.168.0.254
static ip_address=192.168.0.44/24
static routers=192.168.0.254
static domain_name_servers=192.168.0.254
Another common scenario is where the Pi is used without a router, and so it cannot get a dhcp address. Without any configuration this would mean that it got a "link-local" address in the 169.254.0.0/16 network. However the address it gets is random, which is useless in many cases. Instead you can tell dhcpcd to allocate a specified address when DHCP fails:
Code: Select all
######################################################
# TEMPLATE: A static IP address only when no DHCP
#
# The profile name is arbitrary. Use "fred"
# if you want. Not much we can put as
# default servers, but set them up as
# you usually would.
######################################################
interface eth0
fallback nodhcp
profile nodhcp
static ip_address=10.0.0.1/8
static routers=10.0.0.1
static domain_name_servers=10.0.0.1
Code: Select all
interface eth0
arping 192.168.1.1
fallback mylan
profile 192.168.1.1
static ip_address=192.168.1.99/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1
profile mylan
static ip_address=192.168.0.99/24
Code: Select all
interface eth0
interface wlan0
It is possible to tell dhcpcd to select based on the WiFi ssid. However I had problems with this that seem to have permanantly messed up my WiFi, so I don't recommend it.