Red Hat Training

A Red Hat training course is available for RHEL 8

2.6. 使用 nmstatectl 配置具有静态 IP 地址的以太网连接

使用 nmstatectl 工具通过 Nmstate API 配置以太网连接。Nmstate API 确保设置配置后结果与配置文件匹配。如果有任何失败,nmstatectl 会自动回滚更改以避免系统处于不正确的状态。

先决条件

  • 服务器配置中存在物理或虚拟以太网网络接口控制器(NIC)。
  • nmstate 软件包已安装。

步骤

  1. 创建包含以下内容的 YAML 文件,如 ~/create-ethernet-profile.yml

    ---
    interfaces:
    - name: enp1s0
      type: ethernet
      state: up
      ipv4:
        enabled: true
        address:
        - ip: 192.0.2.1
          prefix-length: 24
        dhcp: false
      ipv6:
        enabled: true
        address:
        - ip: 2001:db8:1::1
          prefix-length: 64
        autoconf: false
        dhcp: false
    routes:
      config:
      - destination: 0.0.0.0/0
        next-hop-address: 192.0.2.254
        next-hop-interface: enp1s0
      - destination: ::/0
        next-hop-address: 2001:db8:1::fffe
        next-hop-interface: enp1s0
    dns-resolver:
      config:
        search:
        - example.com
        server:
        - 192.0.2.200
        - 2001:db8:1::ffbb

    这些设置使用以下设置为 enp1s0 设备定义一个以太网连接配置文件:

    • 静态 IPv4 地址 - 192.0.2.1,子网掩码为 /24
    • 静态 IPv6 地址 - 2001:db8:1::1,子网掩码为 /64
    • IPv4 默认网关 - 192.0.2.254
    • IPv6 默认网关 - 2001:db8:1::fffe
    • IPv4 DNS 服务器 - 192.0.2.200
    • IPv6 DNS 服务器 - 2001:db8:1::ffbb
    • DNS 搜索域 - example.com
  2. 将设置应用到系统:

    # nmstatectl apply ~/create-ethernet-profile.yml

验证

  1. 以 YAML 格式显示当前状态:

    # nmstatectl show enp1s0
  2. 显示 NIC 的 IP 设置:

    # ip address show enp1s0
    2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
        link/ether 52:54:00:17:b8:b6 brd ff:ff:ff:ff:ff:ff
        inet 192.0.2.1/24 brd 192.0.2.255 scope global noprefixroute enp1s0
           valid_lft forever preferred_lft forever
        inet6 2001:db8:1::fffe/64 scope global noprefixroute
           valid_lft forever preferred_lft forever
  3. 显示 IPv4 默认网关:

    # ip route show default
    default via 192.0.2.254 dev enp1s0 proto static metric 102
  4. 显示 IPv6 默认网关:

    # ip -6 route show default
    default via 2001:db8:1::ffee dev enp1s0 proto static metric 102 pref medium
  5. 显示 DNS 设置:

    # cat /etc/resolv.conf
    search example.com
    nameserver 192.0.2.200
    nameserver 2001:db8:1::ffbb

    如果多个连接配置文件同时处于活动状态,则 nameserver 条目的顺序取决于这些配置文件中的 DNS 优先级值和连接类型。

  6. 使用 ping 程序来验证这个主机是否可以向其它主机发送数据包:

    # ping <host-name-or-IP-address>

其他资源

  • nmstatectl(8) 手册页
  • /usr/share/doc/nmstate/examples/ 目录