Thursday, April 25, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 672  / 3 Years ago, tue, may 18, 2021, 2:11:45

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?


More From » networking

 Answers
0

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.


[#117] Tuesday, May 18, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
herriail

Total Points: 11
Total Questions: 122
Total Answers: 118

Location: Peru
Member since Tue, Mar 16, 2021
3 Years ago
herriail questions
Sun, Dec 12, 21, 06:06, 2 Years ago
Wed, May 24, 23, 01:42, 1 Year ago
Sun, Jan 29, 23, 00:48, 1 Year ago
Fri, Jan 21, 22, 01:10, 2 Years ago
;