netplanを使いこなせ。ただしyamlの話はしたくない。

投稿者: | 4月 22, 2022

実は、自分のLinux歴は30年くらいある。まだRedhatなんてリリースされておらず、Slackware全盛で、ブラウザなんてNCSA Mosicしかなかった。(ちなみにRedhatのversion 1はリリースされておらず、リリースはversion 2から。)というギドギドのおじさんである。

コマンドもだいぶ変化していて、ifconfig,route,nslookupというコマンドは、今はなく、別途インストールしないと使えない。ここいらは古いシステムを触る必要があるケースではまだ必要かもしれないけど、新しいディストリビューションを使う限りは不要な知識で、必要スキルレベル的にはヤングでナウ(飲み会の3次会ぐらいでないと発言するのを憚る死語)のレベルだと思う。

しかし、未だディストリビューションでネットワーク設定が統一されていない。設定方法も、設定のコマンドもすらも。
設定ファイルは他のページの記載に譲るとして、コマンドラインでネットワーク設定を変えてみた。

RedhatやCentOSでネットワーク設定を変更するためには、nmcli コマンドを使う。

nmcli c mod ens192 ipv4.addresses 192.168.11.200/24
nmcli c mod ens192 ipv4.gateway 192.168.11.1
nmcli c mod ens192 ipv4.method manual
nmcli c mod ens192 connection.autoconnect yes
nmcli c mod ens192 ipv4.dns 192.168.11.1,8.8.8.8
nmcli c mod ens192 ipv4.dns-search example.com
nmcli c down ens192 && nmcli c up ens192

Ubuntuだとnetplanコマンドを使う。この手のコマンドで変えられるので自動化向きだと思う。いずれにせよ、昭和、平成のおじさん(つまり自分)が得意とするviで変更する必要はない。(viコマンドはずーっと変わらず便利だが。。。)

閑話休題

netplanコマンドを調べてみるとyamlを変更しろと書いてあるページが多い。それじゃ/etc/network/interfacesを変えていたころと進化がない。やっぱりコマンドラインで変えたいというのが今回の趣旨。

netplanを叩いてみると

root@ent6-client:/etc/netplan# netplan
You need to specify a command
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

netplan applyは、yamlを変更したときに実行するのでよく使うが、get、info、ip、setというサブコマンドがある。

netplan info を実行してみた例。使える機能が記載されているだけか?

root@ent6-client:~# 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

netplan getを実行してみた例。どうやら現状で読み込まれているyamlの情報が表示されている。

DHCP環境 (dhcp-identifier: “mac”は自分で足している。このパラメータがないとすげー困るよね。。。)

root@ent1-lnx1:~# netplan get
network:
version: 2
ethernets:
ens192:
dhcp-identifier: "mac"
dhcp4: true

固定IP環境

全てのパラメータを入手

root@ent6-client:~# netplan get all
network:
version: 2
renderer: networkd
ethernets:
ens192:
addresses:
- "192.168.16.3/24"
nameservers:
addresses:
- 192.168.16.2
search:
- example.corp
gateway4: 192.168.16.1
dhcp-identifier: "mac"
dhcp4: false
dhcp6: false

ethernets.ens192をつけることでens192だけの情報とか

root@ent6-client:~# netplan get ethernets.ens192
addresses:
- "192.168.16.3/24"
nameservers:
addresses:
- 192.168.16.2
search:
- example.corp
gateway4: 192.168.16.1
dhcp-identifier: "mac"
dhcp4: false
dhcp6: false

ethernets.ens192.addressesとつけることで、IPアドレスだけの情報とか

root@ent6-client:~# netplan get ethernets.ens192.addresses
- "192.168.16.3/24"

が出てくる。

netplan ip を実行してみた例。DHCPで貰ってくる情報がリストされている。

root@ent1-lnx1:~# netplan ip leases ens192
# This is private data. Do not parse.
ADDRESS=192.168.11.95
NETMASK=255.255.255.0
ROUTER=192.168.11.1
SERVER_ADDRESS=192.168.11.1
NEXT_SERVER=192.168.8.4
BROADCAST=192.168.11.255
T1=40178
T2=72578
LIFETIME=86400
DNS=192.168.11.1
NTP=192.168.11.1
DOMAINNAME=local-128.example.corp
HOSTNAME=ent1-lnx1
CLIENTID=010050568c1d8f

さて、DNSのIPとサーチドメインを変えたい。まぁ、yamlを変更してnetplan applyすればいいのだが。コマンドラインでやりたい。できた。

root@ent6-client:~# netplan set network.ethernets.ens192.nameservers.addresses=[8.8.8.8]
root@ent6-client:~# netplan set network.ethernets.ens192.nameservers.search=[example.corp]
root@ent6-client:~# netplan get
network:
version: 2
renderer: networkd
ethernets:
ens192:
addresses:
- "192.168.16.3/24"
nameservers:
addresses:
- 8.8.8.8
search:
- example.corp
gateway4: 192.168.16.1
dhcp-identifier: "mac"
dhcp4: false
dhcp6: false

さらにnetplanの設定ファイルも修正されている。

root@ent6-client:~# cat /etc/netplan/99_config.yaml
network:
ethernets:
ens192:
addresses:
- 192.168.16.3/24
dhcp-identifier: mac
dhcp4: false
dhcp6: false
gateway4: 192.168.16.1
nameservers:
addresses:
- 8.8.8.8
search:
- example.corp
renderer: networkd
version: 2

なんとなく目的達成w

ちなみに参考にしたページ
http://manpages.ubuntu.com/manpages/impish/man8/netplan-set.8.html
ヘルプをよく嫁なんだろうな(爆)。ただ、基本がよくわかってないとヘルプを読んでもわからないということだった。

コメントを残す