Configure static IP in Ubuntu 20.04 via the CLI

Doing some lab testing, and needed to add another interface on an Ubuntu server virtual machine.

Quickly added it via the host (TrueNAS), but it came unconfigured and uninitialized.

The interface is in a down state, so it doesn't show up in ifconfig without using a switch:

ifconfig
enp0s4: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.245  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::2a0:98ff:fe0a:8b09  prefixlen 64  scopeid 0x20<link>
        ether 00:a0:98:0a:8b:09  txqueuelen 1000  (Ethernet)
        RX packets 648  bytes 98987 (98.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 142  bytes 22349 (22.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 172  bytes 13404 (13.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 172  bytes 13404 (13.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

To get it to show up, we need to use the -a switch, which will show all interfaces, even if they are down.

ifconfig -a
enp0s4: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.245  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::2a0:98ff:fe0a:8b09  prefixlen 64  scopeid 0x20<link>
        ether 00:a0:98:0a:8b:09  txqueuelen 1000  (Ethernet)
        RX packets 677  bytes 101586 (101.5 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 158  bytes 26065 (26.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s5: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 00:a0:98:67:56:d9  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 172  bytes 13404 (13.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 172  bytes 13404 (13.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

For my lab testing purposes, I needed to configure an address on separate network than my DHCP server hands out, so I needed to manually configure an IP address.

Create the configuration yaml file in /etc/netplan/, in this case the file is /etc/netplan/05-10net.yaml :

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s5:
      dhcp4: no
      addresses:
        - 10.10.10.245/24
      gateway4: 10.10.10.1
      nameservers:
          addresses: [10.10.10.15]

After the yaml file is created, apply the new network config:

sudo netplan apply

The new interface now shows up, and is configured with the address I needed:

ifconfig
enp0s4: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.245  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::2a0:98ff:fe0a:8b09  prefixlen 64  scopeid 0x20<link>
        ether 00:a0:98:0a:8b:09  txqueuelen 1000  (Ethernet)
        RX packets 1977  bytes 251487 (251.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 700  bytes 131631 (131.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s5: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.10.10.245  netmask 255.255.255.0  broadcast 10.10.10.255
        inet6 fe80::2a0:98ff:fe67:56d9  prefixlen 64  scopeid 0x20<link>
        ether 00:a0:98:67:56:d9  txqueuelen 1000  (Ethernet)
        RX packets 43  bytes 8269 (8.2 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7  bytes 586 (586.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 196  bytes 15444 (15.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 196  bytes 15444 (15.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0