大网数据广告活动
服务器运维问答

How to add a static secondary IP to a DHCP interface using netplan?

· 52CE 我爱测

My server has a file /etc/netplan/50-cloud-init.yaml with the following content: # This file is generated from information provided by # the datasource. Changes to it will not persist across an instance. # To disable cloud-init's…

问题原文

原帖链接 · Torsten Bronger · CC BY-SA 4.0

My server has a file /etc/netplan/50-cloud-init.yaml with the following content:

# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# 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:
    version: 2
    ethernets:
        ens3:
            dhcp4: true
            match:
                macaddress: fa:**:**:**:**:**
            set-name: ens3

This results in the following interface configuration:

2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc fq_codel state UP group default qlen 1000
    link/ether fa:**:**:**:**:** brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.5/24 brd 10.0.0.255 scope global dynamic ens3
       ...

So far, so good. But now I need to add 10.0.0.250 as an additional secondary static IP to this interface. What is the best way to do that? I created a new file /etc/netplan/60-service-ip.yaml with:

network:
    version: 2
    ethernets:
        ens3:
            addresses:
              - 10.0.0.5/24
              - 10.0.0.250/24

This seems to work, but it re-defines a dynamic IP as a static one.

社区高分解答

原帖链接 · Insan · CC BY-SA 4.0

I had a same issue where I have dhcp address as secondary and static as primary in Azure. Leaving dhcp address as primary is important because Azure virtual network provides gateway and nameserver information.

I found this workaround:

network:
    version: 2
    ethernets:
        eth0:
          dhcp4: true
          addresses:
              - 10.0.2.6/24 (IP acquired by DHCP)
              - 10.0.2.21/24
          match:
              macaddress: 00:0d:3a:4f:45:f0
          set-name: eth0

In Azure specifically, we assign static ip address on Azure Resource side and normally this is taken to Guest OS by dhcp. Therefore, even though guest os treats it as a dynamic ip, it will always get same static ip from Azure.
Above, I forced to put dhcp IP ahead of secondary IP.
In my case, it worked as expected.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-10-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。