netplan

最後更新: 2023-01-16

前言

它是個 Network configuration utility

使用 yaml 格式去設置 Backend (networkd / NetworkManager)

目錄

 


安裝

 

apt-get install netplan.io

Backend

  • networkd               # Ubuntu (systemd-networkd)
  • NetworkManager      # RHEL (NetworkManager)

ie.

network:
  version: 2
  renderer: networkd
  ...

 


Info & Status

 

# info      Show available features

netplan info

netplan.io:
  website: "https://netplan.io/"
  features:
  - dhcp-use-domains
  - auth-phase2
  - ipv6-mtu
  - modems
  - sriov
  - openvswitch
  - activation-mode
  - eswitch-mode
  - infiniband
  - regdom
  - vrf
  - vxlan
  - dbus-config
  - generate-just-in-time        # 開機時根據現有 DEVICEs 去建立設定檔
  - generated-supplicant         # generation of a supplicant configuration file for wireless
  - default-routes               # specifying the gateway and metric

 

netplan status

     Online state: offline
    DNS Addresses: 127.0.0.53 (stub)
       DNS Search: .

●  1: lo ethernet UNKNOWN/UP (unmanaged)
      MAC Address: 00:00:00:00:00:00
        Addresses: 127.0.0.1/8
                   ::1/128

●  4: eth0 ethernet UP (unmanaged)
      MAC Address: 00:16:3e:84:83:41
        Addresses: fe80::216:3eff:fe84:8341/64 (link)
           Routes: fe80::/64 metric 256

 


Try & Apply

 

try: Try to apply a new netplan config to running system, with automatic rollback

 * Bridge 必須用 apply 直接套用設定.

 


Useful Configure Options

 

optional

true = An  optional  device is not required for booting.

This settings only supported by networkd

i.e.

  ethernets:
    eno0:
      dhcp4: true
      optional: true

activation-mode

By default, Netplan brings up any configured interface if possible.

  • manual, to hand over control over the interface state to the administrator
  • off to force the link in a down state at all times

 


Disable IPv6 on netplan

 

nic.yaml

network:
  ethernets:
    ens4:
      link-local: []
      addresses:
      - 192.168.11.18/24
      ...

 

 


Configure Summary

 

  • 多 IP
  • Bridges

/etc/netplan/name.yaml            # permission 要係 600 !!

# 簡單設定

network:
  version: 2
  ethernets:
    eth0:
      dhcp4: false
      addresses: [192.168.123.17/24]
      routes:
              - to: default
                via: 192.168.123.1
      nameservers:
        addresses: [192.168.123.1, 8.8.8.8]
        search: [local]

# 多 IP 設定

network:
  version: 2
  ethernets:
    ens4:
      addresses:
      - 192.168.88.241/24
      - 192.168.88.244/24
...

 * 當多過一個設定時, 一共有兩個 format

A)

addresses:
- 192.168.88.241/24
- 192.168.88.244/24

B)

addresses: [192.168.88.241/24, 192.168.88.244/24]

 

 

# Bridges 設定

# dummy bridge (no nic)

  bridges:
    br0:
      dhcp4: no

Notes

netplan status

...
1 inactive interfaces hidden. Use "--all" to show all.

netplan status

●  3: br0 bridge DOWN/UP (networkd: br0)
      MAC Address: 32:7f:fe:15:43:5d

# More Settings (parameters & addresses)

  bridges:
    br0:
      dhcp4: no
      parameters:
        stp: false
        forward-delay: 0
      addresses: [192.168.200.254/24]

# Bridge with nic & route (用 bridge 去取代原來的 NIC )

network:
  version: 2
  renderer: networkd
  ethernets:
    enp2s0:
      dhcp4: no
  bridges:
    br0:
      macaddress: 01:23:45:00:7b:0a
      interfaces:
       - enp2s0
      parameters:
        stp: false
        forward-delay: 0
      addresses: [192.168.123.10/24]
      routes:
       - to: default
         via: 192.168.123.1

Notes

 * 在 Bridges 情況下 "netplan try" 無效, 只能 "netplan apply" 之後等 rollback

 * 最好自定 MAC address, 否則 clone 出來的 instance 可能會撞 MAC address

  • 01:23:45       <- 沒公司佔用的 MAC Address 段
  • 7b                 <- 所在 IP network(123)
  • 0a                 <- 它是 VM 的 IP 尾數 (10)

 

 

Creative Commons license icon Creative Commons license icon