nmcliでkeyfileへ移行する際にはまったこと。

はじめに

こんにちは。今回はRHEL9のネットワーク設定のお話です。

RHEL9ではNetworkManagerを利用してネットワークの設定・管理を行います。
設定されたインターフェイスごとのプロファイルはifcfg形式のファイル、またはkeyfile形式のファイルとしてそれぞれ別のディレクトリに保存されています。

現在、keyfile形式を利用することが推奨されており、ifcfg形式は非推奨です。

参考:
https://rheb.hatenablog.com/entry/2023/04/25/091647

そこで以前、ifcfg形式のプラグインからkeyfile形式のプラグインを利用する変更を行うためのNetworkManagerの設定を変更する記事を書きました。

詳しくはこちら:
https://cloud5.jp/networkmanager-keyfile/

しかし、ifcfg形式からkeyfile形式への移行が完了していないケースがあったため、補足的に今回の記事を書いた次第です。

以下、移行が完了しないケースとその対応方法を要約します。

NetworkManagerの設定ファイル内の[main]セクションでplugins=keyfile,ifcfg-rh,としてkeyfileを優先的に利用するように設定しているが依然としてifcfg-rhを有効化しており、なおかつ、ifcfgファイルが既に/etc/sysconfig/network-scripts/配下にある場合、NetworkManagerの設定を変更してsystemctl restart NetworkManagerにより設定の反映を行ってもifcfgが利用されたままになる。この場合、nmcli connection migrate ${connection-name}を実行する。**

です。

では実際に見ていきましょう。

ifcfgからkeyfileへの移行手順

環境

OS

[root@ip-10-0-2-46 ~]# cat /etc/redhat-release
Red Hat Enterprise Linux release 9.2 (Plow)

nmcli version

> [root@ip-10-0-2-46 ~]# nmcli -v
nmcli tool, version 1.42.2-8.el9_2

手順

NetworkManagerのデフォルト設定を確認します。
ifcfgのみが有効化されており、keyfileがコメントアウトされているのが分かります。

[root@ip-10-0-2-46 ~]# cat /etc/NetworkManager/NetworkManager.conf
# Configuration file for NetworkManager.
#
# See "man 5 NetworkManager.conf" for details.
#
# The directories /usr/lib/NetworkManager/conf.d/ and /run/NetworkManager/conf.d/
# can contain additional .conf snippets installed by packages. These files are
# read before NetworkManager.conf and have thus lowest priority.
# The directory /etc/NetworkManager/conf.d/ can contain additional .conf
# snippets. Those snippets are merged last and overwrite the settings from this main
# file.
#
# The files within one conf.d/ directory are read in asciibetical order.
#
# You can prevent loading a file /usr/lib/NetworkManager/conf.d/NAME.conf
# by having a file NAME.conf in either /run/NetworkManager/conf.d/ or /etc/NetworkManager/conf.d/.
# Likewise, snippets from /run can be prevented from loading by placing
# a file with the same name in /etc/NetworkManager/conf.d/.
#
# If two files define the same key, the one that is read afterwards will overwrite
# the previous one.

[main]
plugins = ifcfg-rh,
#plugins=keyfile,ifcfg-rh

[logging]
# When debugging NetworkManager, enabling debug logging is of great help.
#
# Logfiles contain no passwords and little sensitive information. But please
# check before posting the file online. You can also personally hand over the
# logfile to a NM developer to treat it confidential. Meet us on #nm on Libera.Chat.
#
# You can also change the log-level at runtime via
#   $ nmcli general logging level TRACE domains ALL
# However, usually it's cleaner to enable debug logging
# in the configuration and restart NetworkManager so that
# debug logging is enabled from the start.
#
# You will find the logfiles in syslog, for example via
#   $ journalctl -u NetworkManager
#
# Please post full logfiles for bug reports without pre-filtering or truncation.
# Also, for debugging the entire `journalctl` output can be interesting. Don't
# limit unnecessarily with `journalctl -u`. Exceptions are if you are worried
# about private data. Check before posting logfiles!
#
# Note that debug logging of NetworkManager can be quite verbose. Some messages
# might be rate-limited by the logging daemon (see RateLimitIntervalSec, RateLimitBurst
# in man journald.conf). Please disable rate-limiting before collecting debug logs!
#
#level=TRACE
#domains=ALL

ifcfg形式のファイルが存在します。

[root@ip-10-0-2-46 ~]# ls -lrt /etc/sysconfig/network-scripts/
total 8
-rw-r--r--. 1 root root 1244 Aug 16 12:46 readme-ifcfg-rh.txt
-rw-r--r--. 1 root root  184 Dec  2 04:31 ifcfg-eth0

参考程度に内容を確認してみます。

[root@ip-10-0-2-46 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
# Created by cloud-init on instance boot automatically, do not edit.
#
AUTOCONNECT_PRIORITY=999
BOOTPROTO=dhcp
DEVICE=eth0
HWADDR=06:6a:0e:ad:89:c5
ONBOOT=yes
TYPE=Ethernet
USERCTL=no

では、これからdrop-inファイルで設定を上書きしてkeyfileを有効化します。

# drop-inファイルを作成
cp /etc/NetworkManager/NetworkManager.conf /etc/NetworkManager/conf.d/load_keyfile.conf

# ifcfgのみの行をコメントアウト
sed -i 's/plugins = ifcfg-rh,/#plugins = ifcfg-rh,/' /etc/NetworkManager/conf.d/load_keyfile.conf

# keyfileのある行をアクティベート
sed -i 's/#plugins=keyfile,ifcfg-rh/plugins=keyfile,ifcfg-rh/' /etc/NetworkManager/conf.d/load_keyfile.conf

# 設定を反映させる
systemctl restart NetworkManager

読み込まれたか確認してみます。

journalctl -u NetworkManager
# /keyfileで検索してみるとkeyfile pluginがloadされた旨のメッセージが分かると思います。

しかし、keyfile形式のプロファイルはありません。

[root@ip-10-0-2-46 ~]# ls -lrt /etc/NetworkManager/system-connections/
total 0

ifcfg形式のプロファイルが存在しています。

[root@ip-10-0-2-46 ~]# ls -lrt /etc/sysconfig/network-scripts/
total 8
-rw-r--r--. 1 root root 1244 Aug 16 12:46 readme-ifcfg-rh.txt
-rw-r--r--. 1 root root  184 Dec  2 04:31 ifcfg-eth0

試しに名前を変えてみます。

# uuidの確認
[root@ip-10-0-2-46 ~]# nmcli con show
NAME         UUID                                  TYPE      DEVICE
System eth0  5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03  ethernet  eth0
lo           6d32ab39-7580-4d92-bc9c-759bc25b6499  loopback  lo

# 名前の変更
[root@ip-10-0-2-46 ~]# nmcli con modify 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 connection.id hogehoge

# 変更の確認
[root@ip-10-0-2-46 ~]# nmcli con show
NAME      UUID                                  TYPE      DEVICE
hogehoge  5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03  ethernet  eth0
lo        6d32ab39-7580-4d92-bc9c-759bc25b6499  loopback  lo

# ifcfgファイルに反映されています...。
[root@ip-10-0-2-46 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
# Created by cloud-init on instance boot automatically, do not edit.
#
AUTOCONNECT_PRIORITY=999
BOOTPROTO=dhcp
DEVICE=eth0
HWADDR=06:6A:0E:AD:89:C5
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
PROXY_METHOD=none
BROWSER_ONLY=no
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=hogehoge
UUID=5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03

といわけで、keyfileへの移行を完了させるために次のコマンドを実行します。

# migrateコマンドを実行
[root@ip-10-0-2-46 ~]# nmcli con migrate hogehoge
Connection 'hogehoge' (5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03) successfully migrated.

# ifcfg形式のプロファイルがなくなっています。
[root@ip-10-0-2-46 ~]# ls -lrt /etc/sysconfig/network-scripts/
total 4
-rw-r--r--. 1 root root 1244 Aug 16 12:46 readme-ifcfg-rh.txt

# keyfile形式のプロファイルが生成されています。
[root@ip-10-0-2-46 ~]# ls -lrt /etc/NetworkManager/system-connections/
total 4
-rw-------. 1 root root 240 Dec  2 05:36 hogehoge.nmconnection

# 中身を確認するとkeyfile形式のファイルであることが分かります。
[root@ip-10-0-2-46 ~]# cat /etc/NetworkManager/system-connections/hogehoge.nmconnection
[connection]
id=hogehoge
uuid=5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03
type=ethernet
autoconnect-priority=999
interface-name=eth0
timestamp=1701494611

[ethernet]
mac-address=06:6A:0E:AD:89:C5

[ipv4]
method=auto

[ipv6]
method=ignore

[proxy]

これにて無事ifcfgからkeyfileへの以降が完了しました。

おわりに

いかがだったでしょうか。個人的には結構まりポイントだったので今回記事を書いてみました。誰かのお役に立てたなら幸いです。
それではまたお会いしましょう!あでゅー!

Last modified: 2023-12-03

Author