LoginSignup
48
46

More than 5 years have passed since last update.

Ubuntu 14.04のルーター化

Last updated at Posted at 2014-09-23

Network Routingの覚え書き

やりたいこと

nicが2つあるPCにUbuntuを入れてルータにしたい。

  • nic
    • eth0: 外に繋がっているインターフェース
    • eth1: 新規で切るセグメント

検証環境

  • Ubuntu Server 14.04

ネットワーク/インターフェースの定義

eth0, eth1 のインターフェースの設定を行う。
各ネットワークを定義。(file: /etc/network/interfaces)

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
  address 192.168.1.1
  netmask 255.255.255.0
  network 192.168.1.0
  broadcast 192.168.1.255
  gateway 192.168.1.100
  dns-nameserver 8.8.8.8

auto eth1
iface eth1 inet static
  address 192.168.2.100
  netmask 255.255.255.0
  network 192.168.2.0
  broadcast 192.168.2.255
  gateway 192.168.2.100
  dns-nameserver 8.8.8.8

IPフォーワードの有効化

デフォルトではIPフォワードは無効になっているので
有効化する必要あり。(file: /etc/sysctl.conf)

/etc/sysctl.conf
...(略)...
# コメントアウトする
net.ipv4.ip_forward=1
...(略)...

IPマスカレードの設定

このままでは、戻りの経路情報が不明であるために、192.168.2.0 内から外に向けたpingが失敗する。
そこでIPマスカレードを設定する.

$ sudo /sbin/iptables -t nat -A POSTROUTING -s 192.168.2.0/255.255.255.0 -j MASQUERADE

iptables 起動スクリプト作成

さきほどの設定を保存
$ sudo /sbin/iptables-save -c > /etc/iptables.rules

設定を読み込んで起動するスクリプトを作成。
(file: /etc/network/if-pre-up.d/iptables_start)

/etc/network/if-pre-up.d/iptables_start
#!/bin/sh
/sbin/iptables-restore < /etc/iptables.rules
exit 0

実行権限を付与
$ sudo chmod +x iptables_start

これで再起動すれば、最小ルーティング設定は完了。

※ 192.168.2.0 以下にさらにセグメントを掘り、192.168.1.0 からアクセスを通したい際はUbuntu Severのルーティングテーブルを更新する必要がある。

参考

その他便利だったツール

48
46
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
48
46