systemd-networkd配置静态IP与路由(多IP多网关多网卡)记录

参考:
Linux 上使用 systemd-networkd 服务配置策略路由
Debian切换到netplan管理网络
Linux 下使用 netplan 配置多网卡

需求:我有以下几个IP(IP纯虚构)↓

  1. Address:1.2.3.4 Gateway:1.2.3.1
  2. Address:2.2.2.2 Gateway:2.2.2.1
  3. Address:2000:2000:2000:2000:2000:2000:2000:2000 Gateway:2000:2000:2000:2000:2000:2000:2000:1

我有两张网卡↓

  1. eth0
  2. eth1

我需要在eth0上绑定IP1和IP3,在eht1上绑定IP2.

Debian:

#安装netplan
apt install netplan.io

#删除Debian的networking配置
rm  /etc/network/interfaces

#启用SystemdNetworkd 但是不用启动
systemctl enable systemd-networkd
systemctl enable systemd-resolved

#删除systemd-networkd的配置(如果有)
rm /etc/systemd/network/*

示例(/etc/netplan/00-default.yaml):

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
     dhcp4: false
     addresses:
      - 1.2.3.4/24
      - 2000:2000:2000:2000:2000:2000:2000:2000/48
     nameservers:
       addresses: [8.8.8.8,2001:4860:4860::8888]
     routes:
       - to: 0.0.0.0/0
         via: 1.2.3.1
         metric: 100
       - to: ::/0
         via: 2000:2000:2000:2000:2000:2000:2000:1
         metric: 100
     optional: true
    eth1:
     dhcp4: false
     addresses:
      - 2.2.2.2/24
     nameservers:
       addresses: [8.8.8.8]
     routes:
       - to: 0.0.0.0/0
         via: 2.2.2.1
         metric: 100
     optional: true

执行应用:

netplan apply

最后:

#删除或者禁用networking
#禁用
systemctl disable networking
#删除
apt purge -y --auto-remove ifupdown isc-dhcp-client