# Ubuntu18.04:Netplanによるネットワーク設定

**URL:** https://forum.ficusonline.com/t/topic/315
**Category:** Server
**Created:** [2019 年 8 月 6 日午後 1:57 UTC](https://forum.ficusonline.com/t/topic/315 "2019-08-06T13:57:33Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2019 年 8 月 6 日午後 1:57 UTC](https://forum.ficusonline.com/t/topic/315/1 "2019-08-06T13:57:33Z")

</div>

Ubuntuサーバのネットワーク設定をSSH接続した外部端末からターミナル経由で行う場合には _/etc/network/interfaces_ファイルにネットワークインターフェイス毎に設定を記述し、サービスデーモンを再起動する必要がありました。Ubuntu18.04以降、このネットワーク設定をNetplanにより行うことが出来るようになったようです。

Netplanはネットワーク設定ユーティリティです。  
[netplan.io](http://netplan.io): [https://netplan.io/](https://netplan.io/)

> **[How to Use the Netplan Network Configuration Tool on Linux - Linux.com](https://www.linux.com/learn/intro-to-linux/2018/9/how-use-netplan-network-configuration-tool-linux)**
>
> For years Linux admins and users have configured their network interfaces in the same way. For instance, if you’re an Ubuntu user, you could either configure the network connection via the desktop GUI or from within the /etc/network/interfaces file....

 ![netplan](https://forum.ficusonline.com/uploads/default/original/1X/0b3d30d3a6eab76678255c41657bf868c67a4950.png)

ネットワーク設定は **/etc/netplan/xxx.yaml** ファイルに記述します。ネットワークインターフェイス毎にファイルを作成した場合、読み取り順序はファイル名に依存します。conf01.yamlはconf02.yamlに優先してロードされます。

静的IPアドレスを割り当てる場合の記述例

```auto
network:
    version: 2
    renderer: networkd
    ethernets:
       network_interface(device): --->これはifconfigコマンドで確認
       dhcp4: no
       addresses: [192.168.1.230/24] ---->ネットマスクは/24の箇所24=>255.255.255.0(8bitx3)
       gateway4: 192.168.1.254
       nameservers:
          addresses: [8.8.4.4,8.8.8.8]

```

**ex)**

```auto
network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      dhcp4: no
      addresses:
        - 192.168.121.221/24
      gateway4: 192.168.121.1
      nameservers:
          addresses: [8.8.8.8, 1.1.1.1]

```

**`renderer`**

- **Ubuntu Serverの場合 : `networkd (systemd-networkd)`**
- **Ubuntu Desktopの場合 : `NetworkManager`**

記述後ファイルを保存してテストします。

```auto
$ sudo netplan try

```

エラーが出なければ次のコマンドで設定を反映させます。

```auto
$ sudo netplan apply

```

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2019 年 8 月 7 日午前 2:00 UTC](https://forum.ficusonline.com/t/topic/315/2 "2019-08-07T02:00:09Z")

</div>

**Netplanの設定例 /etc/netplan/xxx.yaml**

[https://netplan.io/examples](https://netplan.io/examples)

[Introduction - Netplan documentation](https://netplan.readthedocs.io/en/stable/examples/)

**NetworkManagerによる設定を使用（Ubuntu18.04のデフォルト）**

```auto
network:
  version: 2
  renderer: NetworkManager

```

NetworkManagerのcliで設定

```auto
$ sudo nmtui

```

**DHCPによるIPアドレス付与+マルチインターフェイス**

```auto
network:
  version: 2
  ethernets:
    enred:
      dhcp4: yes
      dhcp4-overrides:
        route-metric: 100
    engreen:
      dhcp4: yes
      dhcp4-overrides:
        route-metric: 200

```

**WPA（パーソナル）ワイヤレス・ネットワーク**

```auto
network:
  version: 2
  renderer: networkd
  wifis:
    wlp2s0b1:
      dhcp4: no
      dhcp6: no
      addresses: [192.168.0.21/24]
      nameservers:
        addresses: [192.168.0.1, 8.8.8.8]
      access-points:
        "network_ssid_name":
          password: " **********"
      routes:
          - to: default
            via: 192.168.0.1

```

**シングルインターフェイスにマルチアドレス付与**

```auto
network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0:
     addresses:
       - 10.100.1.38/24
       - 10.100.1.39/24
     routes:
       - to: default
         via: 10.100.1.1

```

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2020 年 6 月 2 日午前 2:40 UTC](https://forum.ficusonline.com/t/topic/315/3 "2020-06-02T02:40:28Z")

</div>

同一LAN内でローカルドメイン名で他のPCにアクセス出来ない場合、アクセスするPCのDNSをLANルータに指定します。

```auto
$ sudo nmtui

```

 ![nmtui_edit](https://forum.ficusonline.com/uploads/default/original/1X/5fcc9558aaa22afba54685683be68ef1c964c9f6.png)

 ![nmtui_wired](https://forum.ficusonline.com/uploads/default/original/1X/aa3992ca15036d85b9b4bfba42582d15452c02dc.png)

ルータのDHCPにより割り当てられるIP群の先頭アドレス（ゲートウェイ）をDNSに指定 (192.168.0.1) グローバルDNSはルータ内で設定 (1.1.1.1, 8.8.8.8)

 ![nmtui_connection](https://forum.ficusonline.com/uploads/default/original/1X/19c521a68eb1ac05e328ac9cd5942d4dbd3eda6c.png)

ネットワークマネージャの再起動

```auto
$ sudo service NetworkManager restart

```

nslookup, pingコマンドで確認

```auto
$ nslookup
> examplepc.lan
Server: 127.0.0.53
Address:	127.0.0.53#53

Non-authoritative answer:
Name:	orangepipc.lan
Address: 192.168.0.5

```

```auto
$ ping examplepc.lan

```

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2022 年 7 月 18 日午前 8:43 UTC](https://forum.ficusonline.com/t/topic/315/4 "2022-07-18T08:43:55Z")

</div>

## netplan generate: `gateway4` has been deprecated, use default routes instead

> <https://unix.stackexchange.com/questions/681220/netplan-generate-gateway4-has-been-deprecated-use-default-routes-instead>

以下の箇所を置き換えます。

```auto
gateway4: 192.168.10.1

```

置き換え後

```auto
routes:
      - to: default
        via: 192.168.10.1

```

下記設定例を参照のこと。

> [@Ubuntu18.04:Netplanによるネットワーク設定](https://forum.ficusonline.com/t/topic/315/2):
>
> Netplanの設定例 /etc/netplan/xxx.yaml [https://netplan.io/examples](https://netplan.io/examples)[Introduction - Netplan documentation](https://netplan.readthedocs.io/en/stable/examples/) NetworkManagerによる設定を使用（Ubuntu18.04のデフォルト） network: version: 2 renderer: NetworkManager Netwo…
