LoginSignup
3
3

More than 5 years have passed since last update.

virt-manager使う際のport forwarding設定

Last updated at Posted at 2013-09-07

Forwarding ports to KVM clientsというのもあるようだ

端末 IP
サーバー 2.2.2.2
サーバー上のlxc 192.168.111.2

の場合、 2.2.2.2:10080 のアクセスを 192.168.111.2:80 にフォワーディングしたい。

  • virt-manager起動

接続の詳細 -> 仮想ネットワーク

仮想ネットワーク"network_routing"の作成

  1. 編集メニュー -> 接続の詳細 -> 仮想ネットワーク
  2. 新しい仮想ネットワークの作成 -> Forward
  3. 仮想ネットワークの名前の指定 -> ネットワーク名network_routing -> Forward
  4. IPv4アドレス領域の指定 -> ネットワーク 192.168.111.0/24 -> Forward
  5. DHCP範囲の選択 -> DHCPを有効に -> ON, 開始-> 192.168.111.128, 終了 -> 192.168.111.254
  6. 物理ネットワークへの接続 -> 物理ネットワークにフォワード ,宛先 -> いずれかの物理デバイス, モード -> ルーティング -> Forward
  7. ネットワークを作成する準備ができました -> 完了

最初からあるdefaultのネットワークは自動起動をOFFにしておいて大丈夫です。

サーバー上のlxcのIP固定設定

  • /var/lib/lxc/コンテナ名/rootfs/etc/network/interfaces
iface eth0 inet static
    address 192.168.111.2
    netmask 255.255.255.0
    gateway 192.168.111.1
    dns-serch local
    dns-nameserver 8.8.8.8 8.8.4.4
  • /var/lib/lxc/コンテナ名/config にlxc用の設定を記載しても変更できません。

サーバー(2.2.2.2)にてiptablesでforwarding設定

/etc/iptables.lxc.forwarding.sh
#!/bin/sh
# 変数
PUBLIC_IP=2.2.2.2
LXC_IP=192.168.111.2
HOST_PORT=10080
GUEST_PORT=80
# ルーティング
sudo iptables -t nat -A PREROUTING -p tcp -d $PUBLIC_IP --dport $HOST_PORT -j DNAT --to-destination ${LXC_IP}:${GUEST_PORT}
sudo iptables -t nat -A POSTROUTING -j MASQUERADE
# 確認
sudo iptables-save
$ sudo chmod +x /etc/iptables.lxc.forwarding.sh
$ sudo /etc/iptables.lxc.forwarding.sh

外部からtelnet 2.2.2.2 10080で応答があることを確認。

サーバー起動時にiptablesに追加されることを確認。
$ sudo vim /etc/rc.local
/etc/iptables.lxc.forwarding.sh
exit 0
3
3
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
3
3