Ubuntu – Does netplan support DHCP and static addresses on one interface

netplannetworkingserver

I have a couple Ubuntu Server 16 installations where the NIC gets a static address and a dhcp address using the interface:X syntax in the /etc/network/interfaces file.

auto eno1
iface eno1 inet static
        address 172.16.12.18
        netmask 255.255.252.0
        network 172.16.12.0
        broadcast 172.16.15.255
        gateway 172.16.12.1
        # dns-* options are implemented by the resolvconf package, if installed
        dns-nameservers 172.16.0.3 172.16.8.7 8.8.8.8

auto eno1:1
iface eno1:1 inet static
        address 172.16.13.18
        netmask 255.255.252.0
        network 172.16.12.0
        broadcast 172.16.15.255
        gateway 172.16.12.1
        # dns-* options are implemented by the resolvconf package, if installed
        dns-nameservers 172.16.0.3 172.16.8.7 8.8.8.8

auto eno1:0
iface eno1:0 inet dhcp

This results in the server above getting the two static and one DHCP addresses below.

$ ip addr show eno1
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 48:0f:cf:63:09:91 brd ff:ff:ff:ff:ff:ff
    inet 172.16.12.18/22 brd 172.16.15.255 scope global eno1
       valid_lft forever preferred_lft forever
    inet 172.16.13.18/22 brd 172.16.15.255 scope global secondary eno1:1
       valid_lft forever preferred_lft forever
    inet 172.16.15.27/22 brd 172.16.15.255 scope global secondary eno1:0
       valid_lft forever preferred_lft forever

I installed Ubuntu Server 18 yesterday and found netplan was the preferred network manager. I was able to add multiple IPs in the yaml config for netplan, but the addresses are both static. I'd like to get one static and a DHCP address.

# cat /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eno1:
      addresses: [ 172.16.9.250/22 , 172.16.10.250/22 ]
      gateway4: 172.16.8.1
      nameservers:
          search: [ staff.example.com , example.com ]
          addresses:
              - "172.16.8.7"
              - "172.16.0.3"

The reason I want a static and dynamic is a holdover from a fix I found on the NFS server I set up a couple years ago. One of the old Solaris machines couldn't connect to the 12.18 address above, so I would add another static address – and later configure the NFS server to get a DHCP address – and the flaky Solaris server could mount the NFS volume on the additional IPs.

Is it possible to configure netplan to get a DHCP address and assign a static address on the same interface?

Best Answer

The solution was quite simple, just set a static IP address and enable DHCP. Basically you just have to add dhcp4: yes to your configuration.

This configuration gave me a primary static IP address and a secondary DHCP assigned IP address:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: yes
      dhcp6: no
      addresses: 
        - 10.1.2.15/24
      gateway4: 10.1.2.1
      nameservers:
        search:
          - example.com
        addresses: [10.1.2.10]

The result of ip address show enp0s3 gave me:

2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:ab:cd:ef brd ff:ff:ff:ff:ff:ff
    inet 10.1.2.15/24 brd 10.0.1.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet 10.1.2.96/24 brd 10.0.1.255 scope global secondary dynamic enp0s3
       valid_lft 3224sec preferred_lft 3224sec
    inet6 fe80::a00:27ff:fe20:2c40/64 scope link 
       valid_lft forever preferred_lft forever

The address 10.1.2.96 is the secondary DHCP assigned address as indicated by the secondary dynamic keywords.

Related Question