Configure multiple IPv4 addresses on Raspberry Pi device.
By default Raspberry Pi OS is using a DHCP client service to get dynamic IP addresses. I will use systemd network configuration to define multiple IP addresses on an Ethernet device.
Configure a DHCP client to ignore Ethernet interface and to not install additional default routes.
$ 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 denyinterfaces eth0 nogateway
Configure a network associated with an Ethernet device.
$ cat /etc/systemd/network/20-ethernet.network
[Match] Name=eth0 [Network] DHCP=no DNS=172.16.2.2 NTP=172.16.0.1 [Route] Gateway=172.16.0.1 Metric=202 [Address] Address=172.16.0.210/16 [Address] Address=172.16.0.211/16 [Address] Address=172.16.0.212/16
It can be simplified if you do not need to define metric for default route.
[Match] Name=eth0 [Network] Gateway=172.16.0.1 Address=172.16.0.210/16 Address=172.16.0.211/16 Address=172.16.0.212/16 DNS=172.16.2.2 NTP=172.16.0.1
Enable system service that manages networks as it is disabled by default, simply restart it if you change configuration later.
$ systemctl enable --now systemd-networkd
Restart a DHCP client service, but system reboot will be a good choice too.
$ systemctl restart dhcpcd.service
Inspect service logs.
$ journalctl --boot --unit dhcpcd.service
-- Journal begins at Mon 2022-04-04 16:41:41 CEST, ends at Mon 2022-04-25 02:26:19 CEST. -- Apr 25 02:25:42 raspberrypi systemd[1]: Starting DHCP Client Daemon... Apr 25 02:25:42 raspberrypi dhcpcd[420]: dev: loaded udev Apr 25 02:25:42 raspberrypi dhcpcd[474]: wlan0: starting wpa_supplicant Apr 25 02:25:42 raspberrypi dhcpcd-run-hooks[489]: wlan0: starting wpa_supplicant Apr 25 02:25:43 raspberrypi dhcpcd[420]: wlan0: connected to Access Point `' Apr 25 02:25:43 raspberrypi dhcpcd[420]: wlan0: waiting for carrier Apr 25 02:25:48 raspberrypi dhcpcd[420]: wlan0: carrier acquired Apr 25 02:25:48 raspberrypi dhcpcd[420]: wlan0: connected to Access Point `Searching...' Apr 25 02:25:48 raspberrypi dhcpcd[420]: DUID 00:01:00:01:29:dd:c0:3d:e4:5f:01:42:3a:49 Apr 25 02:25:48 raspberrypi dhcpcd[420]: wlan0: IAID 01:42:3a:4a Apr 25 02:25:48 raspberrypi dhcpcd[420]: wlan0: adding address fe80::9b63:3d58:e21:aacf Apr 25 02:25:48 raspberrypi dhcpcd[420]: wlan0: soliciting an IPv6 router Apr 25 02:25:48 raspberrypi dhcpcd[420]: wlan0: rebinding lease of 192.168.8.159 Apr 25 02:25:48 raspberrypi dhcpcd[420]: wlan0: probing address 192.168.8.159/24 Apr 25 02:25:53 raspberrypi dhcpcd[420]: wlan0: leased 192.168.8.159 for 604800 seconds Apr 25 02:25:53 raspberrypi dhcpcd[420]: wlan0: adding route to 192.168.8.0/24 Apr 25 02:25:53 raspberrypi dhcpcd[420]: forked to background, child pid 704 Apr 25 02:25:53 raspberrypi systemd[1]: Started DHCP Client Daemon. Apr 25 02:26:01 raspberrypi dhcpcd[704]: wlan0: no IPv6 Routers available
$ journalctl --boot --unit systemd-networkd
-- Journal begins at Mon 2022-04-04 16:41:41 CEST, ends at Mon 2022-04-25 02:26:04 CEST. -- Apr 25 02:25:39 raspberrypi systemd[1]: Starting Network Service... Apr 25 02:25:39 raspberrypi systemd-networkd[174]: Enumeration completed Apr 25 02:25:39 raspberrypi systemd[1]: Started Network Service. Apr 25 02:25:40 raspberrypi systemd-networkd[174]: eth0: Link UP Apr 25 02:25:42 raspberrypi systemd-networkd[174]: wlan0: Link UP Apr 25 02:25:44 raspberrypi systemd-networkd[174]: eth0: Gained carrier Apr 25 02:25:45 raspberrypi systemd-networkd[174]: eth0: Gained IPv6LL Apr 25 02:25:48 raspberrypi systemd-networkd[174]: wlan0: Gained carrier Apr 25 02:25:48 raspberrypi systemd-networkd[174]: wlan0: Connected WiFi access point: Searching... (00:78:cd:01:be:3a) Apr 25 02:25:49 raspberrypi systemd-networkd[174]: wlan0: Gained IPv6LL
$ journalctl --boot --unit systemd-timesyncd
-- Journal begins at Mon 2022-04-04 16:41:41 CEST, ends at Mon 2022-04-25 02:26:19 CEST. -- Apr 25 02:25:41 raspberrypi systemd[1]: Starting Network Time Synchronization... Apr 25 02:25:42 raspberrypi systemd[1]: Started Network Time Synchronization. Apr 25 02:25:44 raspberrypi systemd-timesyncd[352]: Network configuration changed, trying to establish connection. Apr 25 02:25:53 raspberrypi systemd[1]: Stopping Network Time Synchronization... Apr 25 02:25:53 raspberrypi systemd[1]: systemd-timesyncd.service: Succeeded. Apr 25 02:25:53 raspberrypi systemd[1]: Stopped Network Time Synchronization. Apr 25 02:25:53 raspberrypi systemd[1]: Starting Network Time Synchronization... Apr 25 02:25:53 raspberrypi systemd[1]: Started Network Time Synchronization. Apr 25 02:26:04 raspberrypi systemd-timesyncd[702]: Timed out waiting for reply from 172.16.0.1:123 (172.16.0.1).
Display status of network links.
$ networkctl
IDX LINK TYPE OPERATIONAL SETUP 1 lo loopback carrier unmanaged 2 eth0 ether routable configured 3 wlan0 wlan routable unmanaged 3 links listed.
Inspect network addresses and routes.
$ ip -br a
lo UNKNOWN 127.0.0.1/8 ::1/128 eth0 UP 172.16.0.210/16 172.16.0.211/16 172.16.0.212/16 fe80::e65f:1ff:fe42:3a49/64 wlan0 UP 192.168.8.159/24 fe80::9b63:3d58:e21:aacf/64
$ ip route list
default via 172.16.0.1 dev eth0 proto static metric 202 172.16.0.0/16 dev eth0 proto kernel scope link src 172.16.0.210 192.168.8.0/24 dev wlan0 proto dhcp scope link src 192.168.8.159 metric 303
Notice, you can allow DHCP client service to install additional default routes, this is where metrics (cost) comes into play for default route.
$ ip route list
default via 172.16.0.1 dev eth0 proto static metric 202 default via 192.168.8.1 dev wlan0 proto dhcp src 192.168.8.159 metric 303 172.16.0.0/16 dev eth0 proto kernel scope link src 172.16.0.210 192.168.8.0/24 dev wlan0 proto dhcp scope link src 192.168.8.159 metric 303