Categories
SysOps

How to configure static IP address on Raspberry Pi

Configure static IP address on Raspberry Pi 4 device.

IP address on Raspberry Pi 4 device is controlled by a DHCP client service.

$ sudo systemctl status dhcpcd.service 
* dhcpcd.service - DHCP Client Daemon
     Loaded: loaded (/lib/systemd/system/dhcpcd.service; enabled; vendor preset: enabled)
    Drop-In: /etc/systemd/system/dhcpcd.service.d
             `-wait.conf
     Active: active (running) since Sun 2022-04-24 21:29:06 CEST; 1min 2s ago
       Docs: man:dhcpcd(8)
    Process: 434 ExecStart=/usr/sbin/dhcpcd -w -q (code=exited, status=0/SUCCESS)
      Tasks: 2 (limit: 8986)
        CPU: 427ms
     CGroup: /system.slice/dhcpcd.service
             |-542 wpa_supplicant -B -c/etc/wpa_supplicant/wpa_supplicant.conf -iwlan0
             `-704 /usr/sbin/dhcpcd -w -q

Apr 24 21:29:04 raspberrypi dhcpcd[434]: eth0: probing for an IPv4LL address
Apr 24 21:29:06 raspberrypi dhcpcd[434]: wlan0: leased 192.168.8.159 for 604800 seconds
Apr 24 21:29:06 raspberrypi dhcpcd[434]: wlan0: adding route to 192.168.8.0/24
Apr 24 21:29:06 raspberrypi dhcpcd[434]: wlan0: adding default route via 192.168.8.1
Apr 24 21:29:06 raspberrypi dhcpcd[434]: forked to background, child pid 704
Apr 24 21:29:06 raspberrypi systemd[1]: Started DHCP Client Daemon.
Apr 24 21:29:43 raspberrypi dhcpcd[704]: eth0: using IPv4LL address 169.254.26.43
Apr 24 21:29:43 raspberrypi dhcpcd[704]: eth0: adding route to 169.254.0.0/16
Apr 24 21:29:46 raspberrypi dhcpcd[704]: eth0: no IPv6 Routers available
Apr 24 21:29:47 raspberrypi dhcpcd[704]: wlan0: no IPv6 Routers available

By default it is using dynamic IP addresses.

$ cat /root/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.
# Some non-RFC compliant DHCP servers do not reply with this set.
# In this case, comment out duid and enable clientid above.
#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
# Respect the network MTU. This is applied to DHCP routes.
option interface_mtu

# Most distributions have NTP support.
#option ntp_servers

# A ServerID is required by RFC2131.
require dhcp_server_identifier

# Generate SLAAC address using the Hardware Address of the interface
#slaac hwaddr
# OR generate Stable Private IPv6 Addresses based from the DUID
slaac private

# Example static IP configuration:
#interface eth0
#static ip_address=192.168.0.10/24
#static ip6_address=fd51:42f8:caae:d92e::ff/64
#static routers=192.168.0.1
#static domain_name_servers=192.168.0.1 8.8.8.8 fd51:42f8:caae:d92e::1

# It is possible to fall back to a static IP if DHCP fails:
# define static profile
#profile static_eth0
#static ip_address=192.168.1.23/24
#static routers=192.168.1.1
#static domain_name_servers=192.168.1.1

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

Configure Ethernet device with a fallback IP address

Update configuration to define fallback profile.

$ cat /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.
# Some non-RFC compliant DHCP servers do not reply with this set.
# In this case, comment out duid and enable clientid above.
#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
# Respect the network MTU. This is applied to DHCP routes.
option interface_mtu

# Most distributions have NTP support.
#option ntp_servers

# A ServerID is required by RFC2131.
require dhcp_server_identifier

# Generate SLAAC address using the Hardware Address of the interface
#slaac hwaddr
# OR generate Stable Private IPv6 Addresses based from the DUID
slaac private

# Example static IP configuration:
#interface eth0
#static ip_address=192.168.0.10/24
#static ip6_address=fd51:42f8:caae:d92e::ff/64
#static routers=192.168.0.1
#static domain_name_servers=192.168.0.1 8.8.8.8 fd51:42f8:caae:d92e::1

# It is possible to fall back to a static IP if DHCP fails:
# define static profile
profile static_eth0
static ip_address=172.16.0.110/24
static routers=172.16.0.1
static domain_name_servers=172.16.2.2

# fallback to static profile on eth0
interface eth0
fallback static_eth0

Restart service or a whole device to apply changes.

$ sudo systemctl restart dhcpcd.service

Inspect service logs.

$ sudo journalctl --boot --unit dhcpcd
-- Journal begins at Mon 2022-04-04 16:41:41 CEST, ends at Sun 2022-04-24 19:05:43 CEST. --
Apr 24 18:58:17 raspberrypi systemd[1]: Starting DHCP Client Daemon...
Apr 24 18:58:17 raspberrypi dhcpcd[409]: dev: loaded udev
Apr 24 18:58:17 raspberrypi dhcpcd[492]: wlan0: starting wpa_supplicant
Apr 24 18:58:17 raspberrypi dhcpcd[409]: wlan0: connected to Access Point `'
Apr 24 18:58:17 raspberrypi dhcpcd[409]: eth0: waiting for carrier
Apr 24 18:58:17 raspberrypi dhcpcd[409]: dhcpcd_prestartinterface: wlan0: Operation not possible due to RF-kill
Apr 24 18:58:17 raspberrypi dhcpcd[409]: dhcpcd_prestartinterface: wlan0: Operation not possible due to RF-kill
Apr 24 18:58:17 raspberrypi dhcpcd[409]: wlan0: waiting for carrier
Apr 24 18:58:21 raspberrypi dhcpcd[409]: eth0: carrier acquired
Apr 24 18:58:21 raspberrypi dhcpcd[409]: DUID 00:01:00:01:29:dd:c0:3d:e4:5f:01:42:3a:49
Apr 24 18:58:21 raspberrypi dhcpcd[409]: eth0: IAID 01:42:3a:49
Apr 24 18:58:21 raspberrypi dhcpcd[409]: eth0: adding address fe80::14b8:2700:4354:ec24
Apr 24 18:58:22 raspberrypi dhcpcd[409]: eth0: soliciting an IPv6 router
Apr 24 18:58:22 raspberrypi dhcpcd[409]: eth0: soliciting a DHCP lease
Apr 24 18:58:27 raspberrypi dhcpcd[409]: eth0: selected profile static_eth0
Apr 24 18:58:27 raspberrypi dhcpcd[409]: eth0: IAID 01:42:3a:49
Apr 24 18:58:27 raspberrypi dhcpcd[409]: eth0: probing address 172.16.0.110/16
Apr 24 18:58:28 raspberrypi dhcpcd[409]: eth0: soliciting an IPv6 router
Apr 24 18:58:33 raspberrypi dhcpcd[409]: eth0: using static address 172.16.0.110/16
Apr 24 18:58:33 raspberrypi dhcpcd[409]: eth0: adding route to 172.16.0.0/16
Apr 24 18:58:33 raspberrypi dhcpcd[409]: eth0: adding default route via 172.16.0.1
Apr 24 18:58:33 raspberrypi dhcpcd[409]: forked to background, child pid 623
Apr 24 18:58:33 raspberrypi systemd[1]: Started DHCP Client Daemon.
Apr 24 18:58:40 raspberrypi dhcpcd[623]: eth0: no IPv6 Routers available

Inspect IP address.

$ ip -br a
lo               UNKNOWN        127.0.0.1/8 ::1/128 
eth0             UP             172.16.0.110/16 fe80::14b8:2700:4354:ec24/64 
wlan0            DOWN   

Configure Ethernet device with a static IP address

Update configuration to define static IP address.

$ cat /etc/dhcpcd.conf 
# A sample configuration for dhcpcd.
# See dhcpcd.conf(5) for details.

# Generate SLAAC address using the Hardware Address of the interface
#slaac hwaddr
# OR generate Stable Private IPv6 Addresses based from the DUID
slaac private

# Static IP configuration
interface eth0
static ip_address=172.16.0.111/16
static routers=172.16.0.1
static domain_name_servers=172.16.2.2

Restart service or a whole device to apply changes.

$ sudo systemctl restart dhcpcd.service

Inspect service logs.

$ sudo journalctl --boot --unit dhcpcd
-- Journal begins at Mon 2022-04-04 16:41:41 CEST, ends at Sun 2022-04-24 19:23:51 CEST. --
Apr 24 19:18:59 raspberrypi systemd[1]: Starting DHCP Client Daemon...
Apr 24 19:18:59 raspberrypi dhcpcd[408]: dev: loaded udev
Apr 24 19:19:00 raspberrypi dhcpcd[498]: wlan0: starting wpa_supplicant
Apr 24 19:19:00 raspberrypi dhcpcd[408]: wlan0: connected to Access Point `'
Apr 24 19:19:00 raspberrypi dhcpcd[408]: eth0: waiting for carrier
Apr 24 19:19:00 raspberrypi dhcpcd[408]: dhcpcd_prestartinterface: wlan0: Operation not possible due to RF-kill
Apr 24 19:19:00 raspberrypi dhcpcd[408]: dhcpcd_prestartinterface: wlan0: Operation not possible due to RF-kill
Apr 24 19:19:00 raspberrypi dhcpcd[408]: wlan0: waiting for carrier
Apr 24 19:19:04 raspberrypi dhcpcd[408]: eth0: carrier acquired
Apr 24 19:19:04 raspberrypi dhcpcd[408]: DUID 00:01:00:01:29:dd:c0:3d:e4:5f:01:42:3a:49
Apr 24 19:19:04 raspberrypi dhcpcd[408]: eth0: IAID 01:42:3a:49
Apr 24 19:19:04 raspberrypi dhcpcd[408]: eth0: adding address fe80::14b8:2700:4354:ec24
Apr 24 19:19:04 raspberrypi dhcpcd[408]: eth0: probing address 172.16.0.111/16
Apr 24 19:19:05 raspberrypi dhcpcd[408]: eth0: soliciting an IPv6 router
Apr 24 19:19:09 raspberrypi dhcpcd[408]: eth0: using static address 172.16.0.111/16
Apr 24 19:19:09 raspberrypi dhcpcd[408]: eth0: adding route to 172.16.0.0/16
Apr 24 19:19:09 raspberrypi dhcpcd[408]: eth0: adding default route via 172.16.0.1
Apr 24 19:19:09 raspberrypi dhcpcd[408]: forked to background, child pid 623
Apr 24 19:19:09 raspberrypi systemd[1]: Started DHCP Client Daemon.
Apr 24 19:19:18 raspberrypi dhcpcd[623]: eth0: no IPv6 Routers available

Inspect IP address.

$ ip -br a
lo               UNKNOWN        127.0.0.1/8 ::1/128 
eth0             UP             172.16.0.111/16 fe80::14b8:2700:4354:ec24/64 
wlan0            DOWN   

Configure wireless device with a static IP address

Ensure that regulatory country code is defined.

$ cat /etc/default/crda 
# Set REGDOMAIN to a ISO/IEC 3166-1 alpha2 country code so that iw(8) may set
# the initial regulatory domain setting for IEEE 802.11 devices which operate
# on this system.
#
# Governments assert the right to regulate usage of radio spectrum within
# their respective territories so make sure you select a ISO/IEC 3166-1 alpha2
# country code suitable for your location or you may infringe on local
# legislature. See `/usr/share/zoneinfo/zone.tab' for a table of timezone
# descriptions containing ISO/IEC 3166-1 alpha2 country codes.

REGDOMAIN=PL

Pre-compute PSK entry for network configuration block.

$ wpa_passphrase laboratory.network laboratory.password
network={
	ssid="laboratory.network"
	#psk="laboratory.password"
	psk=04496c4d0baf75e7dab58f4497ef956a89a1d80ca802658dfaeb2e18eadc6354
}

Update configuration for Wi-Fi Protected Access client and IEEE 802.1X supplicant.

$ cat  /etc/wpa_supplicant/wpa_supplicant.conf 
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
	ssid="laboratory.network"
	psk=04496c4d0baf75e7dab58f4497ef956a89a1d80ca802658dfaeb2e18eadc6354
}

Wireless device is disabled by default.

$ rfkill list
0: phy0: Wireless LAN
	Soft blocked: yes
	Hard blocked: no
1: hci0: Bluetooth
	Soft blocked: no
	Hard blocked: no

Enable wireless device.

$ rfkill unblock 0

Ensure that wireless device is enabled.

$ rfkill list
0: phy0: Wireless LAN
	Soft blocked: no
	Hard blocked: no
1: hci0: Bluetooth
	Soft blocked: no
	Hard blocked: no

Update configuration to define static addresses.

$ cat /etc/dhcpcd.conf 
# A sample configuration for dhcpcd.
# See dhcpcd.conf(5) for details.

# Generate SLAAC address using the Hardware Address of the interface
#slaac hwaddr
# OR generate Stable Private IPv6 Addresses based from the DUID
slaac private

# Example static IP configuration:
interface eth0
static ip_address=172.16.0.111/16
static routers=172.16.0.1
static domain_name_servers=172.16.2.2

interface wlan0
static ip_address=192.168.8.112/24
#static routers=192.168.8.1
#static domain_name_servers=192.168.8.1

Restart service or a whole device to apply changes.

$ sudo systemctl restart dhcpcd.service

Restart Wi-Fi Protected Access client and IEEE 802.1X supplicant.

$ systemctl restart wpa_supplicant.service 

Inspect service logs.

$ sudo journalctl --boot --unit dhcpcd
-- Journal begins at Mon 2022-04-04 16:41:41 CEST, ends at Sun 2022-04-24 20:53:17 CEST. --
Apr 24 20:51:57 raspberrypi systemd[1]: Starting DHCP Client Daemon...
Apr 24 20:51:57 raspberrypi dhcpcd[391]: dev: loaded udev
Apr 24 20:51:57 raspberrypi dhcpcd[471]: wlan0: starting wpa_supplicant
Apr 24 20:51:57 raspberrypi dhcpcd[391]: wlan0: connected to Access Point `'
Apr 24 20:51:57 raspberrypi dhcpcd[391]: eth0: waiting for carrier
Apr 24 20:51:57 raspberrypi dhcpcd[391]: wlan0: waiting for carrier
Apr 24 20:52:01 raspberrypi dhcpcd[391]: eth0: carrier acquired
Apr 24 20:52:01 raspberrypi dhcpcd[391]: DUID 00:01:00:01:29:dd:c0:3d:e4:5f:01:42:3a:49
Apr 24 20:52:01 raspberrypi dhcpcd[391]: eth0: IAID 01:42:3a:49
Apr 24 20:52:01 raspberrypi dhcpcd[391]: eth0: adding address fe80::14b8:2700:4354:ec24
Apr 24 20:52:01 raspberrypi dhcpcd[391]: eth0: probing address 172.16.0.111/16
Apr 24 20:52:02 raspberrypi dhcpcd[391]: wlan0: carrier acquired
Apr 24 20:52:02 raspberrypi dhcpcd[391]: wlan0: connected to Access Point `laboratory.network'
Apr 24 20:52:02 raspberrypi dhcpcd[391]: wlan0: IAID 01:42:3a:4a
Apr 24 20:52:02 raspberrypi dhcpcd[391]: wlan0: adding address fe80::138b:f3bb:2de6:b1
Apr 24 20:52:02 raspberrypi dhcpcd[391]: wlan0: probing address 192.168.8.112/24
Apr 24 20:52:02 raspberrypi dhcpcd[391]: eth0: soliciting an IPv6 router
Apr 24 20:52:03 raspberrypi dhcpcd[391]: wlan0: soliciting an IPv6 router
Apr 24 20:52:07 raspberrypi dhcpcd[391]: eth0: using static address 172.16.0.111/16
Apr 24 20:52:07 raspberrypi dhcpcd[391]: eth0: adding route to 172.16.0.0/16
Apr 24 20:52:07 raspberrypi dhcpcd[391]: eth0: adding default route via 172.16.0.1
Apr 24 20:52:07 raspberrypi dhcpcd[391]: forked to background, child pid 619
Apr 24 20:52:07 raspberrypi systemd[1]: Started DHCP Client Daemon.
Apr 24 20:52:07 raspberrypi dhcpcd[619]: wlan0: using static address 192.168.8.112/24
Apr 24 20:52:07 raspberrypi dhcpcd[619]: wlan0: adding route to 192.168.8.0/24
Apr 24 20:52:15 raspberrypi dhcpcd[619]: eth0: no IPv6 Routers available
Apr 24 20:52:16 raspberrypi dhcpcd[619]: wlan0: no IPv6 Routers available

Inspect IP address.

$ ip -br a
lo               UNKNOWN        127.0.0.1/8 ::1/128 
eth0             UP             172.16.0.111/16 fe80::14b8:2700:4354:ec24/64 
wlan0            UP             192.168.8.112/24 fe80::138b:f3bb:2de6:b1/64