When running virtual machines in Azure, you sometimes need to add additional IP addresses to your VM’s network interface. While Azure typically manages IP assignments through its network interface configuration, there are situations where you might need to configure IP addresses directly on the VM level.

Introduction

Azure VMs come with a primary IP address assigned through DHCP. This IP is managed by Azure and configured automatically when your VM boots up. However, there are scenarios where you might need to add secondary IP addresses:

  • Running multiple services that need different IP addresses
  • Setting up IP-based security rules
  • Migrating services from other environments

You have two options for adding IPs: a quick temporary fix or a permanent solution using netplan. Let’s look at both approaches.

The quick fix: Using ip addr add

If you just need to add an IP address temporarily (until the next reboot), you can use the ip addr add command:

ip address add 4.234.0.211/32 dev eth0

This command immediately adds the IP to your network interface. However, this configuration won’t survive a VM reboot. The next time your VM starts up, cloud-init will reset the network configuration to its default state.

Making it stick: The netplan way

For a permanent solution, you’ll need to modify the netplan configuration. Here’s how to do it step by step.

1. Understanding the default configuration

First, look at the default netplan configuration in /etc/netplan/50-cloud-init.yml:

# This file is generated from information provided by the datasource.  Changes
# to it will not persist across an instance reboot.  To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        eth0:
            dhcp4: true
            dhcp4-overrides:
                route-metric: 100
            dhcp6: false
            match:
                driver: hv_netvsc
                macaddress: 00:22:48:3f:e2:42
            set-name: eth0
    version: 2

2. Disable cloud-init network management

Create a file to disable cloud-init’s network configuration:

sudo bash -c 'echo "network: {config: disabled}" > /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg'

3. Create your custom configuration

Create a new file /etc/netplan/99-custom.yaml:

sudo nano /etc/netplan/99-custom.yaml

Add your configuration:

network:
    ethernets:
        eth0:
            dhcp4: true
            dhcp4-overrides:
                route-metric: 100
            dhcp6: false
            match:
                driver: hv_netvsc
                macaddress: 00:22:48:3f:e2:42
            set-name: eth0
            addresses:
                - 4.234.0.211/32
    version: 2

4. Fix permissions and apply configuration

Set the correct file permissions:

sudo chmod 600 /etc/netplan/50-cloud-init.yaml
sudo chmod 600 /etc/netplan/99-custom.yaml

Apply the configuration:

sudo netplan apply

You might see a warning about OpenVSwitch:

Cannot call openvswitch: ovsdb-server.service is not running.

This warning can be safely ignored if you’re not using OpenVSwitch networking.

Verify your configuration:

ip addr show eth0

Key takeaways

  • Temporary IP additions using ip addr add work but don’t survive reboots
  • For permanent changes, modify netplan configuration
  • Always maintain the original DHCP configuration to keep your primary IP
  • Keep netplan configuration files secure with proper permissions
  • Ignore OpenVSwitch warnings if you’re not using it
  • Test your configuration thoroughly before relying on it in production

Remember that modifying network configurations can potentially disrupt connectivity. Always have a backup plan (like Azure Serial Console access) in case something goes wrong.