LoginSignup
40
31

More than 3 years have passed since last update.

Ubuntu netplan, systemd-networkd, systemd-resolved の設定

Last updated at Posted at 2018-02-10

2020/02/09追記:ディストリによっては(RHEL7以降/CentOS7系以降など) NetworkManager にて設定を行うことが推奨されています。影響範囲の広いデーモンゆえ、適宜どういった手段を採用するかはよく検討して取捨選択することを私はおすすめします。

2017/02/27追記:Ubuntu16を触っていて気が付いたのですが、16の段階ですでにsystemd-networkdとsystemd-resolvedはデフォルトでインストールされる様になっていた模様です。netplanだけがなかったのですが、Ubuntu16でもこれらを使って設定した方が何かと収まりいい感じがします。

Ubuntu17.10からは下記を使ってネットワーク周りの設定をするようになったとかで『てめーこのやろー!ゆるさねー』という心持ちになった方も多いと思うので、私が心穏やかに下記を使って静的にIPアドレスなどを設定した時のメモをここに置いておきます。

  • netplan
  • systemd-networkd
  • systemd-resolved

Netplan

こいつがUbuntu17.10のネットワーク周りの元締め的なサービスらしいです。

$ systemctl list-unit-files | grep -i net

netplan-wpa@.service                         static         
systemd-networkd-wait-online.service         enabled-runtime
systemd-networkd.service                     enabled-runtime
systemd-networkd.socket                      disabled       
network-online.target                        static         
network-pre.target                           static         
network.target                               static         

デフォルト設定は下記の通りです

01-netcfg.yaml(original)
$ cat /etc/netplan/01-netcfg.yaml.original 

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: yes

この設定ファイルには systemd-networkd や NetworkManager に対して『このNICはこんな風に設定しろよ。』っていう大まかな指示を記載するのだそうです。

とりあえず、私は下記のように設定しました(VirtualBoxの仮想マシンで作業しています)。

01-netcfg.yaml
$ cat /etc/netplan/01-netcfg.yaml

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: false
      gateway4: 10.0.2.1
      nameservers:
        addresses: [1.1.1.1,1.0.0.1]
    enp0s8:
      dhcp4: false

補足:私はVirtualBoxのNatNetworkを使っているので、enp0s3も静的に指定していますが、ただのNATの場合は dhcp4: yes のままで良いと思います。

上記の設定を完了したら、下記のコマンドで設定を反映します。

$ sudo netplan apply

参考URL:https://wiki.ubuntu.com/Netplan/Design

systemd-networkd

テメー名前なげーんだよ!とくに末尾のd!なんとなくテメーだけはぜったいゆるさねーからな!おぼえとけよコラァ!!だけどNetworkManagerは嫌いだからこれからはよろしくな!

素敵なお名前のこのサービスは、皆さん待望のservice(init.d)ではなくsystemdで動くnetworkみたいなものだと思います。

デフォルトの設定はないので、下記のように作ります。
この記事で作成するファイル名は [id:数字]-[Nic名].network です。
.network の他にも .link や .netdev というものが作成可能なようです。

01-enp0s3.network
$ cat /etc/systemd/network/01-enp0s3.network 

[Match]
Name=enp0s3

[Network]
Address=10.0.2.21/24
Gateway=10.0.2.1
DNS=1.1.1.1
02-enp0s8.network
$ cat /etc/systemd/network/02-enp0s8.network 

[Match]
Name=enp0s8

[Network]
Address=192.168.56.21/24

設定が完了したら下記のコマンドでsystemd-networkdをrestartします。

$ sudo systemctl restart systemd-networkd

だいたい下記のような感じになると思います

$ ip a s

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:a5:cf:9b brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.21/24 brd 10.0.2.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fea5:cf9b/64 scope link 
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:f3:b4:9c brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.21/24 brd 192.168.56.255 scope global enp0s8
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fef3:b49c/64 scope link 
       valid_lft forever preferred_lft forever

参考URL:http://lfsbookja.osdn.jp/7.10-systemd/chapter07/network.html

systemd-resolved

netplanとかで設定したnameserverとかDNS〜とはなんだったのか・・・

※注意※ もし、NetworkManagerを使う場合はこれを利用してはいけないそうです。

これまでは、/etc/resolv.confを直接編集していましたが、今後は systemd-resolved を使って名前解決の設定を行うようです。

$ systemctl list-unit-files | grep -i resolv

systemd-resolved-update-resolvconf.path      static         
dbus-org.freedesktop.resolve1.service        enabled        
systemd-resolved-update-resolvconf.service   static         
systemd-resolved.service                     enabled

この手順ではresolv.confは触りませんが、デフォルトのものだけ記載しておきます。

/etc/resolv.conf
$ cat /etc/resolv.conf

# This file is managed by man:systemd-resolved(8). Do not edit.
#
# 127.0.0.53 is the systemd-resolved stub resolver.
# run "systemd-resolve --status" to see details about the actual nameservers.
nameserver 127.0.0.53

下記が systemd-resolved のデフォルト設定ファイルになります。

/etc/systemd/resolved.conf(original)
$ cat /etc/systemd/resolved.conf.original 

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See resolved.conf(5) for details

[Resolve]
#DNS=
#FallbackDNS=
#Domains=
#LLMNR=yes
#MulticastDNS=yes
#DNSSEC=no
#Cache=yes
#DNSStubListener=ud

なんかDNSのところがコメントアウトされているので、コメントを外してgoogleさんcloudflareさんのDNSを参照するように設定します。

/etc/systemd/resolved.conf
$ cat /etc/systemd/resolved.conf

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See resolved.conf(5) for details

[Resolve]
DNS=1.1.1.1
FallbackDNS=1.0.0.1
#Domains=
#LLMNR=yes
#MulticastDNS=yes
#DNSSEC=no
#Cache=yes
#DNSStubListener=udp

設定を保存したら、下記のコマンドで systemd-resolved を再起動します。

$ sudo systemctl restart systemd-resolved

テキトーに $ sudo apt update とか掛けて動作するか、とか、rebootしても設定が変わらないか、とかが確認できたら完了です。

参考URL:https://kledgeb.blogspot.jp/2016/06/ubuntu-1610-7-dnssystemd-resolved.html

慣れてしまえば苦ではないし、systemdで動くnetworkサービスがあってよかったーという安堵すらあります。

補足

なんとなくだけど、boot時に
"ubuntu waiting for network configuration"
の上限値がunlimitになっているのが気になる。

これ、networkの設定ミスってたらOS起きてこなくなる気がしなくもない(未検証)。

40
31
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
40
31