Setting up netplan with one or two IPs

This is relevant to Ubuntu 18,04 and newer installations that now use netplan.
Check what interfaces your Ubuntu currently has set:

ifconfig -a

You will see the interfaces that is available , even if they do not have IPs

    enp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 10.88.6.30  netmask 255.255.254.0  broadcast 10.88.7.255
            inet6 fe80::a20b:b343:cf02:29d5  prefixlen 64  scopeid 0x20<link>
            ether 9c:7b:ef:56:c0:40  txqueuelen 1000  (Ethernet)
            RX packets 1743841  bytes 695370348 (695.3 MB)
            RX errors 0  dropped 803332  overruns 0  frame 0
            TX packets 354174  bytes 33425302 (33.4 MB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    enp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 1.2.3.4  netmask 255.255.255.0  broadcast 1.2.3.255
            inet6 fe80::200:f1ff:fe03:0  prefixlen 64  scopeid 0x20<link>
            ether 00:00:f1:03:00:00  txqueuelen 1000  (Ethernet)
            RX packets 290849  bytes 23134325 (23.1 MB)
            RX errors 0  dropped 11  overruns 0  frame 0
            TX packets 363437  bytes 61415807 (61.4 MB)
            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 390443  bytes 31978742 (31.9 MB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 390443  bytes 31978742 (31.9 MB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

If you don’t have ifconfig installed you can use:

ip address

to show the same information in a different format:

    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host
           valid_lft forever preferred_lft forever
    2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
        link/ether 9c:7b:ef:56:c0:40 brd ff:ff:ff:ff:ff:ff
        inet 10.88.6.30/23 brd 10.88.7.255 scope global noprefixroute enp1s0
           valid_lft forever preferred_lft forever
        inet6 fe80::a20b:b343:cf02:29d5/64 scope link noprefixroute
           valid_lft forever preferred_lft forever
    3: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
        link/ether 00:00:f1:03:00:00 brd ff:ff:ff:ff:ff:ff
        inet 1.2.3.4/24 brd 1.2.3.255 scope global noprefixroute enp2s0
           valid_lft forever preferred_lft forever
        inet6 fe80::200:f1ff:fe03:0/64 scope link
           valid_lft forever preferred_lft forever

In this case the devices are “enp1s0” and “enp2s0”. This means that the system is using generated values. This can should be disabled in the GRUB config for the Siprecorder to function properly.
Disable Generate Network interface names.

If you make this change you will have to change the device names in the config file to “eth0” and “eth1” or they will not work when you reboot.

Check what the netplan configuration file is called. There can be different names, based on how the server got to 18.04. Upgraded servers, installed can have different names:

ls /etc/netplan

01-network-manager-all.yaml
or
00-installer-config.yaml
or something like that.

This is a Graphical desktop system that uses networkmanager as the Network configuration back end.

Backup and Restore
Make a backup of the our config file to allow a simple rollback.

Backup
cp /etc/netplan/01-network-manager-all.yaml ~/01-network-manager-all.yaml -i

Restore
cp ~/01-network-manager-all.yaml /etc/netplam/01-network-manager-all.yaml -i

Changes to make

You can check the
cat /etc/netplan/01-network-manager-all.yaml

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager

or

# This is the network config written by 'subiquity'
network:
  ethernets:
    eth0:
      dhcp4: true
    eth1:
      dhcp4: true
  version: 2

To add the exact details that are currently on the system the file should be changed to look like this.
This file is based on the indenting of the file. you can’t use TAB and have to used spaces.
If you just want one IP for a network card, you can remove the one IP from below the addresses

NOTE UBUNTU 22.04
The Gateway4 setting has been changed to Routes
replace the gateway4 to look like this:

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  ethernets:
    enp1s0:
      dhcp4: no
      addresses: 
         - 10.88.6.30/23
         - 192.168.1.123/24
      routes:
         - to: default
           via: 10.88.6.1
      nameservers:
        addresses: [127.0.0.1, 8.8.8.8]
   enp2s0:
      dhcp4: no
      addresses: [1.2.3.4/24]

Older Ubuntu installations

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  ethernets:
    enp1s0:
      dhcp4: no
      addresses: 
         - 10.88.6.30/23
         - 192.168.1.123/24
      gateway4: 10.88.6.1
      nameservers:
        addresses: [127.0.0.1, 8.8.8.8]
   enp2s0:
      dhcp4: no
      addresses: [1.2.3.4/24]

If this configuration is not correct, you may loose access to the server for about 2 minutes when you run the following command.
!!! Open a byobu session and perform the next steps there in !!!

Check that the config is correct by running this command for each network interface that you configured in the file.
netplan generate --mapping eth0
netplan generate --mapping eth1

Here are some common mistakes and what the error would look like:

  • Do not use TABs for indenting the file, use spaces.
/etc/netplan/01-netcfg.yaml:10:1: Invalid YAML: tabs are not allowed for indent:
        - 10.70.5.181/23
^
  • Format issue:
/etc/netplan/01-netcfg.yaml:14:34: Invalid YAML: did not find expected node content:
        addresses: [10.70.2.200, ,10.70.2.201, 8.8.8.8]
                                 ^
  • (generate:28057): WARNING : 14:53:27.455: Permissions for /etc/netplan/00-installer-config.yaml are too open. Netplan configuration should NOT be accessible by others.

The permission on the config file needs to be locked down more on the newer versions of Ubuntu.
These commands will remove the read access for other users and other groups. Make sure you use your config file name:

chmod o-r /etc/netplan/00-installer-config.yaml
chmod g-r /etc/netplan/00-installer-config.yaml

Making the config live
On session 1 start a mtr to 1.2.3.4 and check if it works. It should not get to the end point yet.

On Session 2 run the following command and check if you loose your access.

netplan try

You should get a display that counts down 120 seconds. If this appears your changes applied and you are still connected.
If the connection doesn’t show a count down or you loose your connection, it will reset back to the previous configuration after 120 seconds.

  • Recheck that the alignment of the config is correct,
  • Check that there is no spelling issues.
  • Check that the correct devices are configured.

Only one IP showing when using ifconfig

if you use ifconfig and it only shows one of the two IPs, try and use the command

ip addr

This should show all the IPs for all the interfaces.