Red Hat Training

A Red Hat training course is available for RHEL 8

2.4. nmstatectl を使用した静的 IP アドレスによるイーサネット接続の設定

Nmstate API を使用してイーサネット接続を設定するには、nmstatectl ユーティリティーを使用します。

たとえば、以下の手順では、以下の設定で enp7s0 デバイスの NetworkManager 接続プロファイルを作成します。

  • 静的 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

nmstatectl ユーティリティーを使用すると、設定後にその設定結果が設定ファイルと同じになります。何らかの障害が発生した場合には、nmstatectl は自動的に変更をロールバックし、システムが不正な状態のままにならないようにします。

手順では、YAML 形式でインターフェイス設定を定義します。または、JSON 形式で設定を指定することもできます。

前提条件

  • サーバーに、物理または仮想のイーサネットデバイスが設定されている。
  • nmstate パッケージがインストールされている。

手順

  1. 以下の内容を含む YAML ファイル (例: ~/create-ethernet-profile.yml) を作成します。

    ---
    interfaces:
    - name: enp7s0
      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: enp7s0
      - destination: ::/0
        next-hop-address: 2001:db8:1::fffe
        next-hop-interface: enp7s0
    dns-resolver:
      config:
        search:
        - example.com
        server:
        - 192.0.2.200
        - 2001:db8:1::ffbb
  2. 設定をシステムに適用します。

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

検証

  1. デバイスおよび接続の状態を表示します。

    # nmcli device status
    DEVICE      TYPE      STATE      CONNECTION
    enp7s0      ethernet  connected  enp7s0
  2. 接続プロファイルのすべての設定を表示します。

    # nmcli connection show enp7s0
    connection.id:              enp7s0
    connection.uuid:            b6cdfa1c-e4ad-46e5-af8b-a75f06b79f76
    connection.stable-id:       --
    connection.type:            802-3-ethernet
    connection.interface-name:  enp7s0
    ...
  3. 接続設定を YAML 形式で表示します。

    # nmstatectl show enp7s0

関連情報

  • nmstatectl(8) の man ページ
  • /usr/share/doc/nmstate/examples/ directory