LoginSignup
237

More than 1 year has passed since last update.

Ubuntu 20.04 LTSで固定IPアドレスの設定

Last updated at Posted at 2018-04-20

Ubuntu 20.04 LTS Serverでの固定IPアドレス設定。
17.10から、IPアドレスの変更が/etc/network/interfacesをいじる方式からNetplanへ変更になっているのでメモ。(18.04 LTSも)

Netplan | Backend-agnostic network configuration in YAML

IPアドレス変更

OSインストール直後、設定ファイルのパスには、
(Ubuntu 20.04LTSの場合)
Live DVDからインストールした場合は/etc/netplan/00-installer-config.yaml

(Ubuntu 18.04LTSの場合)
Live CDからインストールした場合は/etc/netplan/50-cloud-init.yaml
mini.isoからインストールした場合は/etc/netplan/01-netcfg.yaml

が作成されてますが、【Ubuntu】 /etc/netplan/50-cloud-init.yamlを編集するの止めろにある通り、これらのファイルは触らない方が良いです。

公式サイトのドキュメント( https://ubuntu.com/server/docs/network-configuration )によれば、/etc/netplan/99_config.yaml を作成し設定を上書きするのが正式なようです。(コメントにてご指摘ありがとうございます。)

/etc/netplan/99_config.yaml を作成しIPアドレスを設定します。
以下は設定例。

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      dhcp6: false
      addresses: [192.168.1.70/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [192.168.1.1, 8.8.8.8, 8.8.4.4]

KVMやLXDなどで、物理マシンの外に公開するサーバーを建てたい場合にはブリッジの設定をすると思う。その場合には下記のように記述します。(そして仮想nicをbr0に差し込む)

事前準備

設定をする前にブリッジ機能の有効化をしておく

$ sudo apt install -y bridge-utils
$ brctl show
ブリッジ構成時の設定ファイル
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      dhcp6: false
  bridges:
    br0:
      interfaces: [eth0]
      dhcp4: false
      dhcp6: false
      addresses: [192.168.1.70/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [192.168.1.1, 8.8.8.8, 8.8.4.4]
      parameters:
        forward-delay: 0
        stp: false
      optional: true
NICを冗長化しつつVLANを作り更にブリッジを構成した際の設定ファイル(/etc/netplan/99_config.yml)

メモ:netplan applyでは上手く反映されず、OS再起動で接続できるようになった。

network:
  version: 2
  renderer: networkd

  ethernets:
    enp3s0: {}
    eth0: {}

  bonds:
    bond0:
      interfaces:
        - enp3s0
        - eth0
      parameters:
        mode: balance-rr

  vlans:
    vlan129:
      id: 129
      link: bond0
    vlan52:
      id: 52
      link: bond0

  bridges:
    br0:
      interfaces: [vlan129]
      dhcp4: false
      dhcp6: true
      addresses: [192.168.129.10/23]
      gateway4: 192.168.129.254
      nameservers:
        addresses: [192.168.128.1, 8.8.8.8, 8.8.4.4]
    br1:
      interfaces: [vlan52]
      addresses: [192.168.52.21/24]

IPアドレス設定変更の反映

netplan applyを実行して反映します。再起動不要です。

$ sudo netplan apply

tryコマンド

リモートから設定を変更するときに自信があるならばapplyで一気に反映させてもいいけど、失敗して疎通不能になるのは困る場合、反映後指定秒数以内に[Enter]が押せなかったら、元の設定に戻してくれる機能がある。(但し、ブリッジやボンディングには対応していない。timeoutを設定しない時のデフォルトは120秒)
IPアドレスを変更するときなどは、IPアドレスを変えて再度sshしても別な端末になりEnterが押せないので、tmux等仮想端末上からnetplanコマンドを叩いてssh再接続後アタッチ(tmux a)しなおすのが良いかも。

$ sudo netplan try --timeout 10
Do you want to keep these settings?
Press ENTER before the timeout to accept the new configuration
Changes will revert in  1 seconds
Reverting.

IPアドレスの確認

ifconfigコマンドがデフォルトでインストールされなくなったので、ipコマンドで確認。

$ ip addr

(2020-05-28 自分用にメモ追記)
ネットワークの状態を詳細に確認するなら、下記コマンドが便利。

$ networkctl status -a

フックスクリプト

interfacesファイルの頃は、pre-up , up , down , post-downにコマンドを書いていたが、Netplanにはそういった機能は無く、例えばrendererにnetworkdを指定した場合、networkdが呼び出すnetworkd-dispatcherにそういった設定を追加する。

sudo apt install -y networkd-dispatcher
sudo mv ap_start.sh /etc/networkd-dispatcher/routable.d/
sudo mv ap_stop.sh /etc/networkd-dispatcher/off.d/

(参考)https://netplan.io/faq#use-pre-up-post-up-etc-hook-scripts
(参考)https://roy-n-roy.github.io/Raspberry%20Pi/Network/

参考

How To Configure Static IP Address on Ubuntu 20.04/18.04/19.10
Network - Configuration | Server documentation | Ubuntu
Netplan reference (←WiFi、ブリッジの設定等もある)
【Ubuntu】 /etc/netplan/50-cloud-init.yamlを編集するの止めろ

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
What you can do with signing up
237