有没有办法在 netplan 中将自定义配置添加到呈现的网络配置中

有没有办法在 netplan 中将自定义配置添加到呈现的网络配置中

我想使用 networkd 中的一个选项,但 Netplan 中没有这个选项。也许在适当的时候可以实现。在此之前,有没有办法实现自定义网络代码,以便我渲染的 networkd 配置包含以下行?

[Network]
KeepConfiguration=dhcp-on-stop

我希望 Netplan YAML 文件中的一些隐藏配置可以用来实现这一点。

非常欢迎其他解决方案。

答案1

您尚未指定您的 Ubuntu 版本。以下内容在 Ubuntu 22.04 Server 上进行了测试。


这可以通过为您的接口创建一个“插入式”配置文件来实现,它将添加或覆盖您的网络配置。

systemd.网络(5)

   Along with the network file foo.network, a "drop-in" directory
   foo.network.d/ may exist. All files with the suffix ".conf" from
   this directory will be merged in the alphanumeric order and
   parsed after the main file itself has been parsed. This is useful
   to alter or add configuration settings, without having to modify
   the main configuration file. Each drop-in file must have
   appropriate section headers.

一个例子...

您尚未包含 Netplan YAML 配置文件,因此我将使用以下内容:

$ cat /etc/netplan/00-install-config.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: true

在此例中,网络接口是eth0。您需要eth0在以下命令中用您的接口替换 。

1. 创建插入目录

当解析 Netplan YAML 文件时,会创建以下文件:/run/systemd/network/10-netplan-eth0.network

插入目录名称需要与此文件名匹配,并添加“.d”后缀:

sudo mkdir /etc/systemd/network/10-netplan-eth0.network.d

2.创建配置文件:

在此目录中,创建一个以“.conf”结尾的配置文件。文件名称不重要,只要有后缀即可。

sudo touch /etc/systemd/network/10-netplan-eth0.network.d/10-netplan-eth0.network.conf

3.编辑如下:

# Drop-in configuration for 10-netplan-eth0.network
[Network]
KeepConfiguration=dhcp-on-stop

4.重新启动systemd-networkd

sudo systemctl restart systemd-networkd

为了让其他人了解完整,我将包含有关密钥的部分KeepConfiguration=

systemd.网络(5)

   KeepConfiguration=
       Takes a boolean or one of "static", "dhcp-on-stop", "dhcp".
       When "static", systemd-networkd will not drop static
       addresses and routes on starting up process. When set to
       "dhcp-on-stop", systemd-networkd will not drop addresses and
       routes on stopping the daemon. When "dhcp", the addresses and
       routes provided by a DHCP server will never be dropped even
       if the DHCP lease expires. This is contrary to the DHCP
       specification, but may be the best choice if, e.g., the root
       filesystem relies on this connection. The setting "dhcp"
       implies "dhcp-on-stop", and "yes" implies "dhcp" and
       "static". Defaults to "dhcp-on-stop" when systemd-networkd is
       running in initrd, "yes" when the root filesystem is a
       network filesystem, and "no" otherwise.

相关内容