どうも、俺@週末です。
iptablesを使ってipマスカレードの設定めもめも。
ローカル回線しかないLinuxサーバでもきちんとデフォルトゲートウェイの設定をしてあげれば、ローカルサーバからグローバルのデータを取得することは出来ます。
・グローバルサーバ(192.168.0.10:ゲートウェイとなる)ローカル回線とグローバル回線
・ローカルサーバ(192.168.0.11) ローカル回線のみ
とします。
まずグローバル側の設定から始めましょう。
// グローバルサーバ
# iptables -L -t nat
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
普通はこんな感じ。
// グローバルサーバ
# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE -s 192.168.0.0/24
# iptables -L -t nat
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@ss79 cadox]# iptables -L -t nat
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
MASQUERADE all -- 192.168.1.0/24 anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ふむふむ、ちゃんと設定されている。
次に転送設定。
// グローバルサーバ
# iptables -P FORWARD ACCEPT
# service iptables save
かなりザックリですが。。
// グローバルサーバ やり方は色々ありますが、、
# vim /etc/sysctl.conf
----------------------------------
net.ipv4.ip_forward = 1
# sysctl -p
または
// グローバルサーバ
echo 1 > /proc/sys/net/ipv4/ip_forward
など。。
次にローカルサーバ側でデフォルトゲートウェイの設定を行います。
// ローカルサーバ
# route add default gw 192.168.0.10
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.0.0 * 255.255.255.0 U 0 0 0 eth1
xxx.xxx.xx.xx * 255.255.0.0 U 0 0 0 eth1
default 192.168.0.10 0.0.0.0 UG 0 0 0 eth1 // ←できてる
再起動しても設定がリセットされないように
// ローカルサーバ
# vim /etc/sysconfig/network
----------------------------
GATEWAY=192.168.0.10 # ←追加
これでOK!
以上でぇぇぇえっぇぇす。