dshimizu/blog

アルファ版

Ubuntu 18.04 LTS のネットワーク設定がnetplanというものになっているのでその扱い方についてのメモ書き

Ubuntu 18.04 LTSを使っていたらネットワーク設定の方法がifup/downからnetplanというものへと置き換わり、サービスの管理もnetworkingからsystemd-networkd, systemd-resolvedというものに変わっていた。

厳密にいつから変わったのか追ってみるのと、Ubuntu 17.04 でDNSの設定でsystemd-resolvedを使うようになっており、17.10でネットワーク設定で netplan を使うように変わっていた。

  • ArtfulAardvark/ReleaseNotes - Ubuntu Wiki
  • Network configuration
    ifupdown has been deprecated in favor of netplan and is no longer present on new installs. The installer will generate a configuration file for netplan in /etc/netplan, which will set up the system to configure the network via systemd-networkd or NetworkManager. Desktop users will see their system fully managed via NetworkManager as it has been the case in previous releases, but Server users now have their network devices managed via systemd-networkd on new installs. This only applies to new installations.
    Given that ifupdown is no longer installed by default, its commands will not be present: ifup and ifdown are thus unavailable, replaced by ip link set $device up and ip link set $device down.
    The networkctl command is also available for users to see a summary of the network devices. networkctl status will display the current global state of IP addresses on the system; and networkctl status $device can display the details specific to a network device.
    For more information about netplan, please refer to the manual page using the man 5 netplan command.
    

ただ、一応従来の/etc/network/interfaces/etc/network/interfaces.d/以下のネットワーク設定ファイルと networkingサービスでも動く模様。(少なくともUbuntu16.04LTS->Ubuntu18.04LTSへアップグレードした際には従来の/etc/network/interfaces/etc/network/interfaces.d/+networkingサービスで動いた。)

Netplanの設定

現行のnetworkingサービスからnetplan + systemd-networkdへの移行を行ってみる。 現行のネットワーク設定(/etc/network/interfaces)を以下のようなものであるとする。

auto ens3
iface ens3 inet static
        address 192.168.1.10
        netmask 255.255.255.0
        network 192.168.1.0
        broadcast 192.168.1.255
        gateway 192.168.1.254
        # dns-* options are implemented by the resolvconf package, if installed
        dns-nameservers 192.168.1.3 192.168.1.4
        dns-search example.jp

上記のファイルを netplan の設定ファイルへ置き換える。 netplan の設定ファイルはYAML形式となっている。以下にサンプルがあるので必要に応じて参考にするのが良さそう。

設定ファイルの設置場所は /etc/netplan/*.yaml となる。 現行の設定をnetplanのYAMLファイル(/etc/netplan/01-netcfg.yaml)に書き換えると以下のようにな感じになる。

network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      dhcp4: no
      dhcp6: no
      addresses:
        - 192.168.1.10/24
      gateway4: 192.168.1.254
      nameservers:
        addresses: [192.168.1.3,192.168.1.4]
        search: [example.jp]

addresses:のところは/32ではなく、属しているネットワークのクラスまたはCIDRとするのが注意点な気がする。

netplanの設定ファイルを作成後、以下を実行する。

$ sudo netplan generate

すると、systemdで扱うための以下のような設定ファイルが生成される。

/run/systemd/network/10-netplan-ens3.network
/run/systemd/resolve/resolv.conf

ちなみに以下を実行すると上記設定ファイルが生成されて、かつネットワーク設定が反映される。

$ sudo netplan apply

これで設定のほうは完了。

networkingサービスは不要なので、OS起動時のnetworkingサービスの起動を無効化する。

$ sudo systemctl disable networking

続いて systemd-networkd をOS起動時に起動するように設定しておく必要がある。

$ sudo systemctl enable systemd-networkd.service
Synchronizing state of networking.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable networking
insserv: warning: current start runlevel(s) (empty) of script `networking' overrides LSB defaults (S).
insserv: warning: current stop runlevel(s) (0 6 S) of script `networking' overrides LSB defaults (0 6).
insserv: warning: current start runlevel(s) (empty) of script `networking' overrides LSB defaults (S).
insserv: warning: current stop runlevel(s) (0 6 S) of script `networking' overrides LSB defaults (0 6).

自動起動になっているか確認する。 enabled であれば問題ない。

$ sudo systemctl is-enabled systemd-networkd.service
enabled

上記は、netplanはデフォルトでは systemd-networkd から設定ファイル等を処理されるようになっているための対応。

  • Ubuntu Manpage: netplan - YAML network configuration abstraction for various backends
  •    Introduction
           Distribution  installers, cloud instantiation, image builds for particular devices, or any
           other way to deploy an operating system put its desired network  configuration  into  YAML
           configuration file(s).  During early boot, the netplan “network renderer” runs which reads
           /{lib,etc,run}/netplan/*.yaml and writes configuration to /run  to  hand  off  control  of
           devices to the specified networking daemon.
    
           · Configured  devices get handled by systemd-networkd by default, unless explicitly marked
             as managed by a specific renderer (NetworkManager)
    
           · Devices not covered by the network config do not get touched at all.
    
           · Usable in initramfs (few dependencies and fast)
    
           · No persistent generated config, only original YAML config
    
           · Parser supports multiple config files to allow  applications  like  libvirt  or  lxd  to
             package  up  expected  network  config (virbr0, lxdbr0), or to change the global default
             policy to use NetworkManager for everything.
    
           · Retains  the  flexibility  to  change  backends/policy  later  or  adjust  to   removing
             NetworkManager, as generated configuration is ephemeral.
    

そしてsystemd-networkdが、生成された以下のファイル読み込む。

/run/systemd/network/10-netplan-ens3.network
/run/systemd/resolve/resolv.conf

これらは一時的なファイルであるため、永続化させたいときは /etc/systemd/network/etc/systemd/resolve にファイルを置く。

ここまで終わったらOSを reboot する。

$ sudo reboot

再起動後、無事接続等できれば完了。

まとめ

Ubuntu 18.04 LTS ではネットワーク設定の管理が従来のifup/downでの管理から netplan へと変わっており、ネットワークサービスの管理がnetworkingから systemd-networkd, systemd-resolvedへと変わっている。(厳密には Ubuntu 17.04 から変わり始めている。) netplanでは設定ファイルがYAML形式に変わっている。

参考