2
Service:netplan
Jeffery edited this page 2026-06-11 10:03:47 +08:00
This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

簡介

netplan 是 Ubuntu 的網路設定工具,以 YAML 描述網路介面。本文記錄多網卡環境下,透過路由權重(metric)控制預設對外流量走哪張網卡的設定範例。metric 數值越小優先權越高。

設定檔通常位於 /etc/netplan/,修改後以 netplan apply 套用。

多網路權重設定

雙 DHCP

兩張網卡都使用 DHCP,透過 route-metriceth1100)優先於 eth0200)作為對外路由。

network:
  version: 2
  ethernets:
    eth0:
      dhcp4: true
      dhcp4-overrides:
        route-metric: 200
    eth1:
      dhcp4: true
      dhcp4-overrides:
        route-metric: 100

靜態 + 動態

ens18 使用靜態 IPmetric 200),ens19 使用 DHCPmetric 100),對外流量優先走 metric 較小的 ens19

network:
  version: 2
  ethernets:
    ens18:
      addresses:
      - "172.25.43.200/20"
      nameservers:
        addresses:
        - 168.95.1.1
        - 8.8.8.8
        search: []
      routes:
      - to: "0.0.0.0/0"
        via: "172.25.32.1"
        metric: 200
    ens19:
      dhcp4: true
      routes:
      - to: "0.0.0.0/0"
        metric: 100