<link rel="canonical" href="https://www.start-beyblade.com/<$ArticleId$>" />

<meta name="robots" content="noindex,noarchive,follow">

try,try,try

基本的に備忘録です

Ubuntu 16.04 のスタティックルートの設定でハマった話

数年振りの技術的な話です。
Linuxサーバで何かやるとか久しぶりでした。
私が使っていたのはまだCentOSが6の時代。
CentOSが7になって色々変わってしまったのでUbuntuを使ってみることにしたら見事にはまってしまいました。

Ubuntuでスタティックルートを永続的に設定する方法は主に2つあります

1つ目 /etc/network/interfaces に記述する

私はこの方法をおススメします。
スタティックルートを設定するインターフェイスpost-up の後ろに設定を記述する方法です。

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto enp0s3
iface enp0s3 inet static
    address 192.168.3.203
    netmask 255.255.255.0
    gateway 192.168.3.1
    dns-nameservers 192.168.3.1

auto enp0s8
iface enp0s8 inet static
    address 10.0.0.203
    netmask 255.255.255.0
    post-up route add -net 172.16.0.0 gw 10.0.0.201 netmask 255.255.255.0 dev enp0s8

2つ目 /etc/network/if-up.d/static-routes に記述する

※こちらはおススメしません。

$ cat static-routes 
#!/bin/sh
route add -net 172.16.0.0 gw 10.0.0.201 netmask 255.255.255.0 dev enp0s8

実行権を付与して

$ sudo chmod +x /etc/network/if-up.d/static-routes

反映すればスタティックルートが適用されます。

sudo /etc/network/if-up.d/static-routes

ここまではいいんです。きちんと適用されます。
ただ /etc/init.d/networking restartエラーになります。

$ sudo /etc/init.d/networking restart
[....] Restarting networking (via systemctl): networking.serviceJob for networking.service failed because the control process exited with error code. See "systemctl status networking.service" and "journalctl -xe" for details.
 failed!

もし他に設定変更する項目があってインターフェイスを再起動する必要があってもエラーになります。
しかも /etc/network/if-up.d/static-routes のファイルを消してネットワークをリスタートしてもエラーになります。
私はサーバを再起動しました…。

ということでUbuntu16.04でスタティックルートを設定する場合は /etc/network/interfaces に記述することをおススメします。