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 withnetplan
? - Are there better solutions to add/remove ip addresses with
netplan
? - ... or am I just completely off the path?