Dhcpcd. Conf for Both Static and Dynamic Ip Address

Long story short: I need to configure two interfaces for one physical interface, one with a static and one with a dynamic ip address. I know how to do it in /etc/network/interfaces but my box uses dhcpcd.conf.

Long story:

I have two raspberry pi raspbian boxes in my parents' house. Both have a static IP address and one of them runs openvpn client that allows me to reach that network because the whole network IP address in behind a ISP's NAT.

Now my parents are getting connectivity upgrade from a poor noisy DLS to new shiny optic line. So I have to prepare because the DSL router will be replaced and it config will be replaced too, mainly the subnet will most probably change and the static addresses will stop working

I added a second interface to /etc/network/interfaces of one of the boxes just to get any IP via DHCP:

auto eth0:1
iface eth0:1 inet dhcp

Once it gets connected it calls home (my server here) via ssh so I can connect to it and update the static config as well.

The other box got a newer raspbian that uses dhcpcd. I would like to do the same (second interface, IP from DHCP) but I do not know how.

My current config is:

# 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
# Most distributions have NTP support.
option ntp_servers
# Respect the network MTU. This is applied to DHCP routes.
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

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

interface eth0:1
        # i do not know what to put here #

# 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

Sure I should back up the file before editing it not to loose the config but I did not.

Unfortunately, there is no room for trial and error because I do not have a physical access to the device and I can only hope I will get there during Christmas unless omicron & lockdowns will be faster.

1 Answer

You can disable dhcpcd and use the legacy networking method that you use in your first raspberry.

sudo systemctl disable --now dhcpcd

I also think that the alias interface in /etc/network/interfaces should be the static ip, because just the original interface can assign the gateway, so in case of wrong static configuration you won't have connection. More information in the debianwiki.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest