When working with cloud servers, we often encounter situations where we need to maintain DHCP-assigned primary IPs while adding static secondary IPs to the same interface. This is particularly common when:
- Running multiple services on a single host
- Implementing high-availability setups
- Maintaining legacy systems that require fixed IPs
The original netplan configuration typically looks like this:
network:
version: 2
ethernets:
ens3:
dhcp4: true
match:
macaddress: fa:**:**:**:**:**
set-name: ens3
Many administrators attempt to solve this by creating a new netplan file that redefines both IPs as static:
network:
version: 2
ethernets:
ens3:
addresses:
- 10.0.0.5/24
- 10.0.0.250/24
While this works, it introduces several issues:
- Loses DHCP flexibility for the primary IP
- Creates potential IP conflicts if the DHCP range changes
- Makes the configuration less maintainable
The correct approach combines DHCP for the primary IP with static secondary IPs:
network:
version: 2
ethernets:
ens3:
dhcp4: true
addresses: [10.0.0.250/24]
match:
macaddress: fa:**:**:**:**:**
set-name: ens3
- Create or modify your netplan configuration file (e.g., /etc/netplan/60-static-ip.yaml)
- Apply the new configuration:
sudo netplan apply
- Verify the IP assignment:
ip addr show dev ens3
For more complex scenarios, consider these additional parameters:
network:
version: 2
ethernets:
ens3:
dhcp4: true
dhcp4-overrides:
route-metric: 100
addresses:
- 10.0.0.250/24
- 192.168.1.100/24
routes:
- to: 10.0.0.0/24
via: 10.0.0.1
metric: 50
This configuration:
- Maintains DHCP for the primary IP
- Adds multiple secondary IPs
- Controls routing metrics for better network control
If you encounter issues:
- Check for syntax errors:
sudo netplan --debug generate
- Verify interface naming consistency
- Ensure no IP conflicts exist on your network
- Remember that changes might require a network restart
When working with netplan configurations, a common requirement is adding secondary static IP addresses to interfaces that primarily use DHCP. The key challenge is maintaining the existing dynamic IP assignment while adding static addresses.
Many administrators initially try modifying the configuration like this:
network:
version: 2
ethernets:
ens3:
addresses:
- 10.0.0.5/24
- 10.0.0.250/24
This approach has two major issues:
- Converts the dynamic IP (10.0.0.5) to static
- Creates potential conflicts between DHCP and static configuration
The proper netplan configuration that maintains DHCP while adding static addresses:
network:
version: 2
ethernets:
ens3:
dhcp4: true
addresses:
- 10.0.0.250/24
match:
macaddress: fa:**:**:**:**:**
set-name: ens3
- Create or modify your netplan configuration (typically in /etc/netplan/)
- Keep dhcp4: true while adding the addresses section
- Apply changes with:
sudo netplan apply
After applying the configuration, verify with:
ip addr show ens3
You should see output similar to:
2: ens3: mtu 1450 qdisc fq_codel state UP group default qlen 1000
link/ether fa:**:**:**:**:** brd ff:ff:ff:ff:ff:ff
inet 10.0.0.5/24 brd 10.0.0.255 scope global dynamic ens3
inet 10.0.0.250/24 scope global secondary ens3
For complex setups, you might need additional parameters:
network:
version: 2
ethernets:
ens3:
dhcp4: true
dhcp4-overrides:
route-metric: 100
addresses:
- 10.0.0.250/24
- 192.168.1.100/24
routes:
- to: 10.0.1.0/24
via: 10.0.0.1
metric: 50