Ubuntu 20.4 没有/etc/network/interfaces,配置网络需用Netplan

本文介绍在Ubuntu 20.04桌面版中,如何迁移至NetPlan进行网络配置,包括其原理、工作流程,以及如何通过YAML文件设置网络接口、WiFi、桥接等。学习如何使用NetPlan替代传统方式,通过示例配置了解关键概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近装了Ubuntu 20 桌面版,需要配置内网,一如既往地找/etc/network/interfaces去配置,发现没有。

于是上网查询,说桌面版现在使用的是netplan.io,官网打开后如下:
在这里插入图片描述

什么是NetPlan

Netplan is a utility for easily configuring networking on a linux system. You simply create a YAML description of the required network interfaces and what each should be configured to do. From this description Netplan will generate all the necessary configuration for your chosen renderer tool.

译文:

Netplan 是一个用于在 linux 系统上轻松配置网络的实用程序。您只需创建所需网络接口的 YAML 描述以及每个应配置的功能。根据此描述,Netplan 将为您选择的渲染器工具生成所有必要的配置。

NetPlan 如何工作

在这里插入图片描述

How does it work?

Netplan reads network configuration from /etc/netplan/*.yaml which are written by administrators, installers, cloud image instantiations, or other OS deployments. During early boot, Netplan generates backend specific configuration files in /run to hand off control of devices to a particular networking daemon.

Netplan currently works with these supported renderers

译文:

它是如何工作的?

Netplan 从 /etc/netplan/*.yaml 读取网络配置,这些配置由管理员、安装程序、云映像实例化或其他操作系统部署编写。在早期启动期间,Netplan 在 /run 中生成后端特定配置文件,以将设备控制权移交给特定的网络守护进程。

Netplan 目前可与这些受支持的渲染器一起使用。

也就是说,NetPlan 其实最终还是使用NetworkManager 和 System-networkd 完成网络配置,只不过配置方式改为更直观的yaml。

配置项

Netplan为yaml文件设计了一系列配置字段,这些字段满足详见Netplan | Backend-agnostic network configuration in YAML

配置示例

需求不一样,配置也不一样,以下是官方列出的常用配置示例:

network:
  version: 2

  # if specified globally, can only realistically have that value, as
  # networkd cannot render wifi/3G. This would be shipped as a separate
  # config.d/ by desktop images
  # it can also be specified by-type or by-device
  renderer: network-manager
  ethernets:
    # opaque ID for physical interfaces with match rules
    # only referred to by other stanzas
    id0:
      match:
        macaddress: 00:11:22:33:44:55
      wakeonlan: true
      dhcp4: true
      addresses:
        - 192.168.14.2/24
        - 2001:1::1/64
      lom:
        # example for explicitly setting a backend (default would be networkd)
        match:
          driver: ixgbe
        # you are responsible for setting tight enough match rules
        # that only match one device if you use set-name
        set-name: lom1
        dhcp6: true
      switchports:
        # all cards on second PCI bus
        # unconfigured by themselves will be added to br0 below
        match:
          name: enp2*
        mtu: 1280
    wifis:
      all-wlans:
      # useful on a system where you know there is
      # only ever going to be one device
      match: {}
      access-points:
        "Joe's home":
          # mode defaults to "managed" (client), key type to wpa-psk
          password: "s3kr1t"
      # this creates an AP on wlp1s0 using hostapd
      # no match rules, thus ID is the interface name
      wlp1s0:
        access-points:
          "guest":
            mode: ap
            channel: 11
            # no WPA config implies default of open
    bridges:
      # the key name is the name for virtual (created) interfaces;
      # no 'match' or 'set-name' attributes are allowed.
      br0:
        # IDs of the components
        # switchports expands into multiple interfaces
        interfaces: [wlp1s0, switchports]
        dhcp4: true
      routes:
        - to: 0.0.0.0/0
        via: 11.0.0.1
        metric: 3
      nameservers:
        search: [foo.local, bar.local]
        addresses: [8.8.8.8]

更多详细见Netplan | Backend-agnostic network configuration in YAML

命令

根据设计,配置完成后,Netplan 需要执行命令是配置生效。

  • netplan generate

    Use /etc/netplan to generate the required configuration for the renderers.

    使用 /etc/netplan 为渲染器生成所需的配置。

  • netplan apply

    Apply all configuration for the renderers, restarting them as necessary.

    应用渲染器的所有配置,并根据需要重新启动它们

  • netplan try

    Apply configuration and wait for user confirmation; will roll back if network is broken or no confirmation is given.

    应用配置并等待用户确认;如果网络中断或没有确认将回滚。

  • netplan get

    Merge and display all the current available configuration on the system.

    合并并显示系统上所有当前可用的配置。

  • netplan set

    Add new setting by specifying a dotted key=value pair like ethernets.eth0.dhcp4=true.

    通过指定带点的 key=value 对来添加新设置,例如 ethernets.eth0.dhcp4=true

  • netplan info

    Show available feature flags of the currently installed version as YAML.

    将当前安装版本的可用功能标志显示为 YAML。

  • netplan ip

    Retrieve IP information from the system.

    从系统中检索 IP 信息。

  • netplan help

    Show the help message.

    显示帮助信息。

更多详细,请移步官网。

当然,作为桌面版,可以在右上角找到网络设置功能,使用图形化配置来配置。

系列文章

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值