1. netplan 自身配置文件/etc/netplan/50-cloud-init.yaml
network:
    version: 2
    renderer: networkd
    ethernets:
        ens3:
            addresses:
            - 154.204.42.134/24
            gateway4: 154.204.42.254
            match:
                macaddress: fa:16:3e:b9:65:3b
            nameservers:
                addresses:
                - 8.8.8.8
                - 8.8.4.4
            set-name: ens3

应用修改

sudo netplan apply
  1. networkd-dispatcher 配置启动脚本

up脚本/etc/networkd-dispatcher/routable.d/50-ifup-hooks

#!/bin/sh

for d in up pre-up; do
    hookdir=/etc/network/if-${d}.d
    [ -e $hookdir ] && /bin/run-parts $hookdir
done
exit 0

注:既遍历/etc/network/if-up.d/etc/network/if-pre-up.d 目录,执行下面的所以脚本

down脚本/etc/networkd-dispatcher/off.d/50-ifdown-hooks

#!/bin/sh

for d in down post-down; do
    hookdir=/etc/network/if-${d}.d
    [ -e $hookdir ] && /bin/run-parts $hookdir
done
exit 0

注:既遍历/etc/network/if-down.d/etc/network/if-post-down.d 目录,执行下面的所以脚本

  1. iptable启动和关闭示例。

/etc/network/if-pre-up.d 下新建iptable文件, 内容如下:

#!/bin/sh

iptables-restore < /etc/iptables.up.rules

/etc/network/if-post-down.d 下新建iptable文件, 内容如下:

#!/bin/sh

iptables-save > /etc/iptables.up.rules

标签: ubuntu

添加新评论