请选择 进入手机版 | 继续访问电脑版
MSIPO技术圈 首页 IT技术 查看内容

ubuntu netplan工具原理(网络配置、ip修改ip、固定ip)(NetworkManager)

2023-07-13

在这里插入图片描述

https://netplan.io/

文章目录

netplan工作原理

在这里插入图片描述

Netplan是一个用于配置Linux系统网络的工具。它的工作原理如下:

  1. 配置文件:Netplan使用YAML格式的配置文件来定义网络配置。默认情况下,配置文件位于/etc/netplan/目录下,以.yaml为后缀。可以创建多个配置文件,每个文件对应一个网络配置。

  2. 渲染器:Netplan使用渲染器来将配置文件转换为实际的网络配置。渲染器是根据操作系统的不同而不同的。在Ubuntu 18.04及更高版本中,默认的渲染器是systemd-networkd,也可以选择使用NetworkManager作为渲染器。

  3. 渲染过程:当系统启动时,Netplan会读取配置文件,并使用相应的渲染器将配置应用到系统中。渲染器会根据配置文件中的内容生成相应的网络配置文件,然后重新加载网络服务以应用新的配置。

  4. 更新配置:如果需要更改网络配置,可以编辑配置文件并保存。然后使用netplan apply命令来重新应用配置。Netplan会检测到配置文件的更改,并使用相应的渲染器来更新网络配置。

总结起来,Netplan通过配置文件和渲染器的配合工作,实现了对Linux系统网络配置的管理和应用。它提供了一种简单且统一的方式来配置网络,使得网络配置更加易于管理和维护。

netplan -h

官网介绍:https://netplan.readthedocs.io/en/stable/cli/

CLI命令行接口:

在这里插入图片描述

在这里插入图片描述

root@arnold-virtual-machine:~# netplan -h
usage: /usr/sbin/netplan  [-h] [--debug]  ...

Network configuration in YAML

optional arguments:
  -h, --help  show this help message and exit
  --debug     Enable debug messages

Available commands:
  
    help      Show this help message
    apply     Apply current netplan config to running system
    generate  Generate backend specific configuration files from /etc/netplan/*.yaml
    get       Get a setting by specifying a nested key like "ethernets.eth0.addresses", or "all"
    info      Show available features
    ip        Retrieve IP information from the system
    set       Add new setting by specifying a dotted key=value pair like ethernets.eth0.dhcp4=true
    rebind    Rebind SR-IOV virtual functions of given physical functions to their driver
    try       Try to apply a new netplan config to running system, with automatic rollback
root@arnold-virtual-machine:~# 

翻译

root@arnold-virtual-machine:~# netplan -h
用法:/usr/sbin/netplan  [-h] [--debug]  ...

YAML格式的网络配置

可选参数:
  -h, --help  显示帮助信息并退出
  --debug     启用调试消息

可用命令:
  
    help      显示此帮助消息
    apply     将当前netplan配置应用到运行系统
    generate  从/etc/netplan/*.yaml生成特定于后端的配置文件
    get       通过指定嵌套键(如"ethernets.eth0.addresses""all")获取设置
    info      显示可用功能
    ip        从系统中检索IP信息
    set       通过指定点分键值对(如ethernets.eth0.dhcp4=true)添加新设置
    rebind    将给定物理功能的SR-IOV虚拟函数重新绑定到其驱动程序
    try       尝试将新的netplan配置应用到运行系统,自动回滚
root@arnold-virtual-machine:~# 

命令释义

- help:显示netplan的帮助消息。

- apply:将当前netplan配置应用到运行系统。示例命令:netplan apply --debug

root@arnold-virtual-machine:~# netplan apply -h
usage: /usr/sbin/netplan apply [-h] [--debug] [--sriov-only] [--only-ovs-cleanup] [--state STATE]

Apply current netplan config to running system

optional arguments:
  -h, --help          show this help message and exit
  --debug             Enable debug messages
  --sriov-only        Only apply SR-IOV related configuration and exit
  --only-ovs-cleanup  Only clean up old OpenVSwitch interfaces and exit
  --state STATE       Directory containing previous YAML configuration
root@arnold-virtual-machine:~# 

root@arnold-virtual-machine:~# netplan apply -h
用法:/usr/sbin/netplan apply [-h] [--debug] [--sriov-only] [--only-ovs-cleanup] [--state STATE]

将当前的netplan配置应用到运行中的系统

可选参数:
  -h, --help          显示帮助信息并退出
  --debug             启用调试消息
  --sriov-only        仅应用与SR-IOV相关的配置并退出
  --only-ovs-cleanup  仅清理旧的OpenVSwitch接口并退出
  --state STATE       包含先前YAML配置的目录
root@arnold-virtual-machine:~# 

试了半天也不知道这个--state参数干嘛用的,我还以为是能另外指定一个目录下的/etc/netplan目录,结果不行。。。还是都放在/etc/netplan下吧。

看了半天也没看懂这个--state参数的作用。。。

在这里插入图片描述

- generate:从/etc/netplan/*.yaml生成特定于后端的配置文件。示例命令:netplan generate(一般用不到)

这个命令我们一般不用直接运行它。

https://netplan.readthedocs.io/en/stable/netplan-generate/

在这里插入图片描述

netplan generate -h
root@arnold-virtual-machine:~# netplan generate -h
usage: /usr/sbin/netplan generate [-h] [--debug] [--root-dir ROOT_DIR] [--mapping MAPPING]

Generate backend specific configuration files from /etc/netplan/*.yaml

optional arguments:
  -h, --help           show this help message and exit
  --debug              Enable debug messages
  --root-dir ROOT_DIR  Search for and generate configuration files in this root directory instead of /
  --mapping MAPPING    Display the netplan device ID/backend/interface name mapping and exit.

root@arnold-virtual-machine:~# netplan generate -h
用法:/usr/sbin/netplan generate [-h] [--debug] [--root-dir ROOT_DIR] [--mapping MAPPING]/etc/netplan/*.yaml 生成特定后端的配置文件

可选参数:
  -h, --help           显示帮助信息并退出
  --debug              启用调试消息
  --root-dir ROOT_DIR  在此根目录中搜索并生成配置文件,而不是 /
  --mapping MAPPING    显示 netplan 设备 ID/后端/接口名称的映射并退出。
  

执行netplan generate命令会根据/etc/netplan/*.yaml目录下的配置文件生成特定后端的配置文件。具体来说,它会将Netplan配置文件转换为网络管理工具(如NetworkManager、systemd-networkd等)所需的配置文件格式。

执行netplan generate命令后,Netplan会读取/etc/netplan/*.yaml目录下的配置文件,并根据配置文件中的内容生成相应的后端配置文件。生成的配置文件将存储在特定的目录中,具体位置取决于所使用的后端工具。

这个命令的目的是将高级的Netplan配置转换为底层网络管理工具所需的配置格式,以便实际应用网络配置。生成的配置文件可以通过后续的命令(如netplan apply)来应用到系统中,从而使网络配置生效。

直接执行netplan apply命令也是可以的,然而,有时候可能需要手动执行netplan generate命令,例如在修改了Netplan配置文件后,想要查看生成的后端配置文件的内容,或者想要检查配置文件是否正确生成。在这些情况下,执行netplan generate命令可以帮助您验证配置文件的生成过程,而不会直接应用配置到系统中。

- get:通过指定嵌套键(如ethernets.eth0.addressesall)获取设置。示例命令:netplan get ethernets.eth0.addresses(注意:这个命令是从/etc/netplan中的yaml文件获取合并的网络信息,而不是当前已生效的网络信息)

netplan get -h
root@arnold-virtual-machine:~# netplan get -h
usage: /usr/sbin/netplan get [-h] [--debug] [--root-dir ROOT_DIR] [key]

Get a setting by specifying a nested key like "ethernets.eth0.addresses", or "all"

positional arguments:
  key                  The nested key in dotted format

optional arguments:
  -h, --help           show this help message and exit
  --debug              Enable debug messages
  --root-dir ROOT_DIR  Read configuration files from this root directory instead of /
root@arnold-virtual-machine:~# 

root@arnold-virtual-machine:~# netplan get -h
用法:/usr/sbin/netplan get [-h] [--debug] [--root-dir ROOT_DIR] [key]

通过指定嵌套键(如“ethernets.eth0.addresses”或“all”)来获取设置

位置参数:
  key                  以点分隔格式指定的嵌套键

可选参数:
  -h, --help           显示帮助信息并退出
  --debug              启用调试消息
  --root-dir ROOT_DIR  从此根目录而不是/读取配置文件
root@arnold-virtual-machine:~# 
示例
netplan get --debug all

在这里插入图片描述

netplan get --debug ethernets

在这里插入图片描述

netplan get --debug ethernets.ens33.gateway4

在这里插入图片描述

- info:显示可用功能。示例命令:netplan info

netplan info -h
root@arnold-virtual-machine:~# netplan info -h
usage: /usr/sbin/netplan info [-h] [--debug] [--json | --yaml]

Show available features

optional arguments:
  -h, --help  show this help message and exit
  --debug     Enable debug messages
  --json      Output version and features in JSON format
  --yaml      Output version and features in YAML format
root@arnold-virtual-machine:~# 

root@arnold-virtual-machine:~# netplan info -h
用法:/usr/sbin/netplan info [-h] [--debug] [--json | --yaml]

显示可用的功能

可选参数:
  -h, --help  显示帮助信息并退出
  --debug     启用调试消息
  --json      以JSON格式输出版本和功能
  --yaml      以YAML格式输出版本和功能
root@arnold-virtual-machine:~# 
示例
root@arnold-virtual-machine:~# 
root@arnold-virtual-machine:~# netplan info
netplan.io:
  website: "https://netplan.io/"
  features:
  - generate-just-in-time
  - generated-supplicant
  - dbus-config
  - default-routes
  - auth-phase2
  - dhcp-use-domains
  - ipv6-mtu
  - modems
  - sriov
  - openvswitch
  - activation-mode
  - eswitch-mode
root@arnold-virtual-machine:~# 
root@arnold-virtual-machine:~# 
root@arnold-virtual-machine:~# 
root@arnold-virtual-machine:~# netplan info --debug
netplan.io:
  website: "https://netplan.io/"
  features:
  - generate-just-in-time
  - generated-supplicant
  - dbus-config
  - default-routes
  - auth-phase2
  - dhcp-use-domains
  - ipv6-mtu
  - modems
  - sriov
  - openvswitch
  - activation-mode
  - eswitch-mode
root@arnold-virtual-machine:~# 
root@arnold-virtual-machine:~# 
root@arnold-virtual-machine:~# netplan info --json
{
  "netplan.io": {
    "website": "https://netplan.io/",
    "features": [
      "generate-just-in-time",
      "generated-supplicant",
      "dbus-config",
      "default-routes",
      "auth-phase2",
      "dhcp-use-domains",
      "ipv6-mtu",
      "modems",
      "sriov",
      "openvswitch",
      "activation-mode",
      "eswitch-mode"
    ]
  }
}
root@arnold-virtual-machine:~# 
root@arnold-virtual-machine:~# 
root@arnold-virtual-machine:~# netplan info --yaml
netplan.io:
  website: "https://netplan.io/"
  features:
  - generate-just-in-time
  - generated-supplicant
  - dbus-config
  - default-routes
  - auth-phase2
  - dhcp-use-domains
  - ipv6-mtu
  - modems
  - sriov
  - openvswitch
  - activation-mode
  - eswitch-mode
root@arnold-virtual-machine:~# 

默认netplan info是以yaml格式显示结果的。

netplan info结果解释

netplan info命令用于显示可用的功能。下面是netplan info的结果解释:

  • netplan.io:这是Netplan的官方网站,可以在该网站上获取更多关于Netplan的信息。
  • features:这是Netplan支持的功能列表。
    • generate-just-in-time:Netplan可以在应用配置之前即时生成配置文件。
    • generated-supplicant:Netplan可以生成用于网络认证的配置文件。
    • dbus-config:Netplan可以通过DBus进行配置。
    • default-routes:Netplan可以配置默认路由。
    • auth-phase2:Netplan支持第二阶段的认证。
    • dhcp-use-domains:Netplan可以使用域名进行DHCP配置。
    • ipv6-mtu:Netplan可以配置IPv6的最大传输单元(MTU)。
    • modems:Netplan支持调制解调器配置。
    • sriov:Netplan支持SR-IOV(单根物理网络适配器的虚拟化)。
    • openvswitch:Netplan支持Open vSwitch(开源虚拟交换机)。
    • activation-mode:Netplan支持激活模式配置。
    • eswitch-mode:Netplan支持以太网交换机模式配置。

这些功能表示Netplan可以实现的不同网络配置选项。

- ip:从系统中检索IP信息。示例命令:netplan ip

netplan ip -h
root@arnold-virtual-machine:~# netplan ip -h
usage: /usr/sbin/netplan ip [-h] [--debug]  ...

Retrieve IP information from the system

optional arguments:
  -h, --help  show this help message and exit
  --debug     Enable debug messages

Available commands:
  
    help      Show this help message
    leases    Display IP leases
root@arnold-virtual-machine:~# 

root@arnold-virtual-machine:~# netplan ip -h
用法:/usr/sbin/netplan ip [-h] [--debug]  ...

从系统中获取IP信息

可选参数:
  -h, --help  显示帮助信息并退出
  --debug     启用调试消息

可用命令:
  
    help      显示此帮助消息
    leases    显示IP租约
root@arnold-virtual-machine:~# 
示例

不知道咋用

在这里插入图片描述

- set:通过指定点分键值对(如ethernets.eth0.dhcp4=true)添加新设置。示例命令:netplan set ethernets.eth0.dhcp4=true

netplan set -h
root@arnold-virtual-machine:~# netplan set -h
usage: /usr/sbin/netplan set [-h] [--debug] [--origin-hint ORIGIN_HINT] [--root-dir ROOT_DIR] key_value

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

positional arguments:
  key_value             The nested key=value pair in dotted format. Value can be NULL to delete a key.

optional arguments:
  -h, --help            show this help message and exit
  --debug               Enable debug messages
  --origin-hint ORIGIN_HINT
                        Can be used to help choose a name for the overwrite YAML file. A .yaml suffix will be appended automatically.
  --root-dir ROOT_DIR   Overwrite configuration files in this root directory instead of /
root@arnold-virtual-machine:~# 

root@arnold-virtual-machine:~# netplan set -h
用法:/usr/sbin/netplan set [-h] [--debug] [--origin-hint ORIGIN_HINT] [--root-dir ROOT_DIR] key_value

通过指定以点分隔的键值对(如ethernets.eth0.dhcp4=true)来添加新设置

位置参数:
  key_value             以点分隔的键值对,格式为key=value。值可以为NULL以删除键。

可选参数:
  -h, --help            显示帮助信息并退出
  --debug               启用调试消息
  --origin-hint ORIGIN_HINT
                        可用于帮助选择覆盖的YAML文件的名称。将自动附加.yaml后缀。
  --root-dir ROOT_DIR   在此根目录中覆盖配置文件,而不是在/目录中
root@arnold-virtual-machine:~# 
示例
设置dhcp4用yes或no,true和false都可以
root@arnold-virtual-machine:/etc/netplan# 
root@arnold-virtual-machine:/etc/netplan# cat 01-network-manager-all.yaml 
# Let NetworkManager manage all devices on this system
network:
  version: 2
  ethernets:
    ens33:
      dhcp4: no
      addresses: [192.168.1.141/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4]
  renderer: NetworkManager
root@arnold-virtual-machine:/etc/netplan# 
root@arnold-virtual-machine:/etc/netplan# 
root@arnold-virtual-machine:/etc/netplan# netplan set --origin-hint 01-network-manager-all ethernets.ens33.dhcp4=false
root@arnold-virtual-machine:/etc/netplan# 
root@arnold-virtual-machine:/etc/netplan# cat 01-network-manager-all.yaml 
network:
  ethernets:
    ens33:
      addresses:
      - 192.168.1.141/24
      dhcp4: false
      gateway4: 192.168.1.1
      nameservers:
        addresses:
        - 8.8.8.8
        - 8.8.4.4
  renderer: NetworkManager
  version: 2
root@arnold-virtual-machine:/etc/netplan# 

设置gateway4
root@arnold-virtual-machine:/etc/netplan# 
root@arnold-virtual-machine:/etc/netplan# cat 01-network-manager-all.yaml 
network:
  ethernets:
    ens33:
      addresses:
      - 192.168.1.141/24
      dhcp4: false
      gateway4: 192.168.1.1
      nameservers:
        addresses:
        - 8.8.8.8
        - 8.8.4.4
  renderer: NetworkManager
  version: 2
root@arnold

相关阅读

手机版|MSIPO技术圈 皖ICP备19022944号-2

Copyright © 2023, msipo.com

返回顶部