• 主页
  • 相册
  • 随笔
  • 目录
  • 存档
Total 244
Search AboutMe

  • 主页
  • 相册
  • 随笔
  • 目录
  • 存档

Netplan

2020-05-06

1. Preview

多年以来 Linux 管理员和用户们以相同的方式配置他们的网络接口。例如,如果你是 Ubuntu 用户,你能够用桌面 GUI 配置网络连接,也可以在 /etc/network/interfaces 文件里配置。配置相当简单且可以奏效。在文件中配置看起来就像这样

1
2
3
4
5
6
auto enp10s0
iface enp10s0 inet static
address 192.168.1.162
netmask 255.255.255.0
gateway 192.168.1.100
dns-nameservers 1.0.0.1,1.1.1.1

尔后重启网络

  • sudo systemctl restart networking
  • sudo /etc/init.d/networking restart

但是现在,在某些发行版上(例如 Ubuntu Linux 18.04),网络的配置与控制发生了很大的变化。不需要那个 interfaces 文件和 /etc/init.d/networking 脚本,我们现在转向使用 Netplan.

1.1. 新的配置文件

如果你打算修改默认文件,我建议你先做一个备份

  • sudo cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bak

1.2. 配置静态 IP 地址

1
2
3
4
5
6
7
8
9
10
network:
Version: 2
Renderer: networkd
ethernets:
DEVICE_NAME:
Dhcp4: yes/no
Addresses: [IP/NETMASK]
Gateway: GATEWAY
Nameservers:
Addresses: [NAMESERVER, NAMESERVER]

1.3. 测试配置

上面的命令会在应用配置之前验证其是否有效。如果成功,Netplan 会尝试将新的配置应用到运行的系统上。如果新的配置失败了,Netplan 会自动地恢复到之前使用的配置。

  • sudo netplan try

1.4. 应用新的配置

netplan generate:以/etc/netplan配置为管理工具生成配置


如果确信配置文件没有问题,可以跳过测试环节并且直接使用新的配置

  • sudo netplan apply

2. Introduction

During early boot, the netplan “network renderer” runs which reads /{lib,etc,run}/netplan/*.yaml and writes configuration to /run to hand off control of devices to the specified networking daemon.

在早期启动时,netplan “网络渲染器 “会运行,读取/{lib,etc,run}/netplan/*.yaml,并将配置写入/run,将设备的控制权交给指定的网络守护进程。

Configured devices get handled by systemd-networkd by default, unless explicitly marked as managed by a specific renderer (NetworkManager)

已配置的设备默认由systemd-networkd处理,除非明确标记为由特定的渲染器(NetworkManager)管理,否则默认情况下由systemd-networkd处理。

3. General structure

The top-level node in a netplan configuration file is a network: mapping that contains version: 2 (the YAML currently being used by curtin, MaaS, etc. is version 1), and then device definitions grouped by their type, such as ethernets:, modems:, wifis:, or bridges:. These are the types that our renderer can understand and are supported by our backends.

4. Device configuration IDs

The key names below the per-device-type definition maps (like ethernets:) are called “ID”s. They must be unique throughout the entire set of configuration files.

If an interface is defined with an ID in a configuration file; it will be brought up by the applicable renderer. To not have netplan touch an interface at all, it should be completely omitted from the netplan configuration files.

每个设备类型定义映射下面的关键名称(如ethernets:)被称为 “ID “s。

如果一个接口在配置文件中定义了一个ID,那么它将被适用的渲染器调出。要想让 netplan 完全不接触到某个接口,应该从 netplan 配置文件中完全省略。

4.1. Physical devices

  • Examples: ethernet, modem(调制解调器), wifi
  • Examples: veth, bridge, bond

    这些设备完全由配置文件和网络栈控制。也就是说,这些设备是被创建的,而不是被匹配的。因此match:和set-name:对这些不适用,ID字段是创建的虚拟设备的名称

4.2. Virtual devices

  • Examples: veth, bridge, bond

    these devices are being created instead of matched. Thus match: and set-name: are not applicable for these, and the ID field is the name of the created virtual device.

5. Common properties for physical device types

  • match
    • name
    • macaddress
    • driver

5.1. example

  • all cards on second PCI bus:
    1
    2
    match:
    name: enp2*
1
2
3
4
5
6
7
network:
version: 2
ethernets:
ens3:
match:
macaddress: 52:54:00:f0:bc:02
dhcp4: true

In the old world you’d match by the name ens3 and not specify a MAC. This is not how netplan works - in netplan what matters is the match stanza.


And you can use DHCP and static IPs together - this will give an interface both static and dynamic address

6. 参考

  • Reference | Netplan
  • 如何在 Linux 上使用网络配置工具 Netplan - 知乎
  • Netplan by example
  • Operating System
  • Linux
  • Basic
shell脚本编程基础
实验:Linux服务器系统管理基础
  1. 1. 1. Preview
    1. 1.1. 1.1. 新的配置文件
    2. 1.2. 1.2. 配置静态 IP 地址
    3. 1.3. 1.3. 测试配置
    4. 1.4. 1.4. 应用新的配置
  2. 2. 2. Introduction
  3. 3. 3. General structure
  4. 4. 4. Device configuration IDs
    1. 4.1. 4.1. Physical devices
    2. 4.2. 4.2. Virtual devices
  5. 5. 5. Common properties for physical device types
    1. 5.1. 5.1. example
  6. 6. 6. 参考
© 2024 何决云 载入天数...