Ubuntu – How to assign a static ip address before eth link running in ubuntu 18.04 LTS

dhcpethernetnetwork-managernetworkingserver

I know that if I need to assign a static ip address I will need to use netplan in Ubuntu 18.04, and I have done exactly that and made it work.

Here is my config file:

network:
  version: 2
  renderer: networkd
  ethernets:
    eno1:
      dhcp4: yes
    enp131s0f0:
      dhcp4: no
      addresses: [10.66.0.1/24]

After I use netplan apply, the ifconfig command shows the ip address of enp131s0f0 set to 10.66.0.1 after I plug the cable into the port for "enp131s0f0". This causes a problem because if I do not plug in the cable to this port, the port itself won't get a static ip address even if the ipaddress is set in netplan, this will cause some services to fail to start (eg, isc-dhcp-server).

For example, if you have 3 ports and you have configured them like so:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp131s0f0:
      dhcp4: no
      addresses: [10.66.0.1/24]
    enp131s0f1:
      dhcp4: no
      addresses: [10.66.1.1/24]
    enp131s0f2:
      dhcp4: no
      addresses: [10.66.2.1/24]
    enp131s0f3:
      dhcp4: no
      addresses: [10.66.3.1/24]

If isc-dhcp-server is installed on all these 3 ports. The isc-dhcp-server will start only if you connect all 3 ports, if you failed to connect your cable to any one port of these 3 ports the isc-dhcp-server will fail to start because the port does not have a static address (without the cable plugged into the port), even if you set the static ip address in netplan.

So I would like to ask how to permanently assign a static address to a net port. To make it have an ipaddress before I plug in the cable… I need this because there are some ports on my server that currently I do not use (the cable is left unplugged), but I need isc-dhcp-server to start…

Best Answer

Here's a minimal .yaml file for your configuration... this assumes that 10.66.0.2 is an available static address on your network, and that 10.66.0.1 is the proper gateway...

network:
  version: 2
  renderer: networkd
  ethernets:
    eno1:
      dhcp4: true
    enp4s0f0:
      addresses: [10.66.0.2/24]
      gateway4: 10.66.0.1
      nameservers:
        addresses: [8.8.8.8,8.8.4.4]
      optional: true # may or may not be desired, to stop 2 minute boot delay

sudo netplan --debug generate # generate config files

sudo netplan apply # apply configuration

reboot # reboot and verify operation