Score:0

Temporarily add multiple ip addresses to interface in 22.04

ss flag

I need to temporarily, dynamically add and remove ip addresses from the netplan.conf (/etc/netplan/myNetplanConfig.conf).

In earlier versions the ip command could be used to temporarily add ip addresses to an interface, but I can't seem to find a replacement for it in Ubuntu 22.04 and I can't find any information on it in man netplan or in the netplan documentation.

I've thought about generating the netplan.conf with bash, but I'm hoping there is a cleaner solution to this.

#!/bin/bash

# Update the netplan configuration file
cat << EOF > /etc/netplan/myNetplanConf.yaml
network:
  ethernets:
    # 1st network interface
    eth0:
      dhcp4: false
      addresses: [192.168.1.10/24]
  version: 2
EOF

# Apply the changes to the network configuration
sudo netplan apply

Using the current solution means, that the ip addresses are added statically (they will live beyond a single boot cycle) and the netplan.conf has to be applied each time an ip address is added or removed.

Another solution is to add ip addresses to the netplan.conf using the sed command.

# Search for the existing ip addresses (.10) in the interface eth0 and append the new ip addresses (.11 .12 ...) to the list.
sed -i '/eth0/,/^\s*$/{/\s*addresses:\s*\[.*192\.168\.1\.(10).*\]/{s//&, 192.168.1.11, 192.168.1.12/}}' /etc/netplan/myNetplanConf.conf

# Apply the changes to the network configuration
sudo netplan apply

... and remove them using the same method.

# Search for the ip addresses (.11 and .12) in the interface eth0 and delete the corresponding entries from the netplan.conf file.
sed -i '/eth0/,/^\s*$/{/\s*addresses:\s*\[.*192\.168\.1\.(11|12).*\]/d}' /etc/netplan/myNetplanConf.conf

# Apply the changes to the network configuration
sudo netplan apply
  • Why does the ip command not work with netplan?
  • Are there better solutions to add/remove ip addresses with netplan?
  • ... or am I just completely off the path?
Score:1
us flag

The ip command still exists in Ubuntu 22.04. It is part of the iproute2 package that is installed by default as part of ubuntu-minimal.

You do not need to push temporary changes to network state through netplan, which is after all a configuration tool.

eDonkey avatar
ss flag
I've been unable to use the `ip` command in Ubuntu 22.04 (commands are executed but ips aren't added). I haven't installed any additional libraries or anything that could interfere. Has anything changed since 20.04?
us flag
no, the `ip` command is still a standard part of the system and we've had no reports of it not succeeding in adding IP addresses to interfaces. You might check the kernel logs (dmesg) to see if there's some kind of driver error on your hardware.
eDonkey avatar
ss flag
Thanks for pointing me to dmesg. Somehow `ifconfig` just didn't show the added ip addresses. They did show up though when using `ip a`.
Score:1
uy flag

changing the netplan yaml makes most sense, when working with netplan ...

there is a nice tool for yaml handling called yq. at least adding an IP is simple.

yq e '.network.ethernets.enp2s0.addresses += ["11.11.11.11/24" ]' network.yaml > new.yaml

A removal looks like

yq e '.network.ethernets.enp2s0.addresses -= ["11.11.11.11/24" ]' network.yaml > new.yaml

a netplan apply must be done anyway.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.