[HowTo] 虛擬化 KVM - 建立 ubuntu guest, bridge network 及 iptables port forwarding [論壇 - 伺服器架設]


正在瀏覽:   1 名遊客


 到底部   前一個主題   下一個主題  [無發表權] 請登錄或者註冊



[HowTo] 虛擬化 KVM - 建立 ubuntu guest, bridge network 及 iptables port forwarding
站長
註冊日期:
2005/6/10 9:50
來自 Taichung, Taiwan.
所屬群組:
網站管理員
已註冊使用者
等級: 19
HP : 0 / 450
MP : 139 / 17601
EXP: 3
離線
舊文分享,英文版。原文鍊結:http://geeky.name/content/kvm-build-ubuntu-guest-bridge-network-and-iptables-port-forwarding
英翻中版本稍候補上。

Once you install and setup KVM, configure kvm itself and system network bridge interface correctly on your Ubuntu server,
you can now starting to build a guest OS / disk image, run it, and use iptables to forward connections to those new VPSes.


Build guest:

HTTP_PROXY="Proxy_IP:Port" sudo ubuntu-vm-builder kvm hardy \
--dest 'DEST_DIR'\
--hostname 'GUEST_HOSTNAME'\
--ip 'GUEST_IP_YOU_WANT'\
--arch 'i386'\
--mem '256'\
--rootsize '4096'\
--swapsize '1024'\
--kernel-flavour 'virtual'\
--domain 'bluet.org'\
--mirror 'http://ftp.twaren.net/ubuntu'\
--components 'main,universe,multiverse,restricted'\
--addpkg ssh --addpkg vim --addpkg unattended-upgrades --addpkg acpid\
--name 'Default_User_Name' --user 'Default_User_Account' --pass 'Default_User_Password'\
--mask '255.255.255.0'\
--net '192.168.122.0'\
--bcast '192.168.122.255'\
--gw '192.168.122.1'\
--dns '192.168.122.1'\
--tmp '/dev/shm/'\
--libvirt qemu:///system ;


Modify that to fit your needs.

Forward "connections to specific port on HostOS" to a specific port on GuestOS (VPS):

Run these commands in HostOS:

$ sudo iptables -t nat -I PREROUTING -p tcp -d HostOS_IP --dport HostOS_Port -j DNAT --to GuestOS_IP:GuestOS_Port
$ sudo iptables -A FORWARD -p tcp -d GuestOS --dport GuestOS_Port -j ACCEPT


For example, if you want to ssh to your VPS, you can pick a spare (not-using by other application) port, and forward "connections to the specified port on HostOS" to the GuestOS's port 22.
Let's say, when HostOS is using IP 192.168.1.2, GuestOS is using IP 192.168.122.2, the choosen port on HostOS is 10022, you can do the following commands.

$ sudo iptables -t nat -I PREROUTING -p tcp -d 192.168.1.2 --dport 10022 -j DNAT --to 192.168.122.2:22
$ sudo iptables -A FORWARD -p tcp -d 192.168.122.2 --dport 22 -j ACCEPT



Finally, you got a configured VPS, you can start running it and ssh into it.

Start the VPS:
If your GuestOS' hostname is "vps01", you can do this to start running it:
$ virsh start vps01
And then ssh into it:
$ ssh -v 192.168.1.2 -p 10022


Refs: <a href="https://help.ubuntu.com/community/KVM">KVM document page on ubuntu.com</a>

2011/6/23 17:04
應用擴展 工具箱
回覆: [HowTo] 虛擬化 KVM - 建立 ubuntu guest, bridge network 及 iptables port forwarding
會員三級
註冊日期:
2009/7/8 12:46
來自 春田花花幼稚園
所屬群組:
已註冊使用者
等級: 15
HP : 0 / 371
MP : 96 / 10088
EXP: 87
離線
好文。

收下了。感謝。

2011/6/23 22:40
* Registered User # 44613 at Linux Counter
應用擴展 工具箱


 [無發表權] 請登錄或者註冊


可以查看帖子.
不可發帖.
不可回覆.
不可編輯自己的帖子.
不可刪除自己的帖子.
不可發起投票調查.
不可在投票調查中投票.
不可上傳附件.
不可不經審核直接發帖.