นี่คือบทความที่สี่เกี่ยวกับ VirtualBox management วันนี้จะสอนทุกอย่างที่ต้องรู้เกี่ยวกับ VirtualBox networking และ sharing
จะแสดงสามวิธีการ configure virtual machines และสามวิธี sharing data ระหว่าง host machine และ virtual machine หลัง master tutorial นี้ คุณจะรู้ทุกอย่างเกี่ยวกับการใช้ VirtualBox ด้วยความสนุกและมั่นใจ ตามมาถึง
Introduction
รายละเอียดเพิ่มเติม ควรอ่านบทความต่อไป จะให้ background ที่จำเป็นสำหรับติดตามวันนี้อย่างสบายและสนุก:
- How to install VirtualBox Guest Additions - Tutorial
- VirtualBox 3 is amazing!
- VirtualBox 3 Compiz slideshow
- DirectX in VirtualBox 3 - Pure joy is here
เช่นกัน ควรอ่านสามตอนแรกของ series นี้:
- How to clone disks in VirtualBox - Tutorial
- How to add hard disks in VirtualBox - Tutorial
- How to expand/shrink disks in VirtualBox - Tutorial
ตอนนี้มาเริ่มกัน
VirtualBox network options
สำหรับ virtual machines ที่ติดตั้งแล้ว คลิก Settings > Network นี่คือจุดเริ่มความสนุก Default view คือนี้ Virtual machine ใดๆ สามารถมี network adapters ได้สูงสุดสี่ตัว Enable ได้ selective ตามที่เหมาะส่วนใหญ่คนต้องการแค่ตัวเดียว
Adapter Type กำหนด virtualized hardware ที่ VirtualBox จะ expose ให้ virtual machine ถ้ามีปัญหา adapter type ใดลองอีกตัว PCnet-FAST III เป็น default selection
ยังมี PCnet-FAST II สำหรับเครื่องเก่า และ Intel PRO/1000 cards สามประเภท รวมถึงสอง Server versions ควร useful สำหรับ production environment สำหรับ home users เลือกอะไรก็ได้ transparent
ส่วนน่าสนใจที่สุดคือ Attached to: section category นี้กำหนดว่า network adapter จะ interface กับ physical hardware ที่มีอยู่อย่างไร Different setups ให้ markedly different results
Network types
มีสี่ตัวเลือก: NAT (default), Bridged, Internal network, และ Host Only เห็นว่า Not attached ก็เป็น type แต่ไม่ใช้ได้จริง
Network Address Translation (NAT)
NAT หมายถึง virtual machines จะมี private IP addresses ที่ไม่ routable จากภายนอก
ตัวอย่าง: Host คือ 192.168.1.1 VirtualBox NAT device จะเป็น 10.0.2.1 ดังนั้น virtual machines จะได้ address ใน range 10.0.2.x เนื่องจากไม่มีอะไร route access ไป machines ใน 10.0.2.x/24 subnet พวกมัน inaccessible จาก host
Setup นี้ useful เมื่อคุณไม่สนใจ IP addresses ของ guests แต่ละตัวแยกกัน แต่ไม่ดีถ้าต้องการ forwarding หรือ expose services ไป external world ไม่เหมาะกับ sharing ผ่าน network access
Pluses: simplicity & seclusion
Minuses: no route to virtual machines, no network sharing
Bridged Adapter
Bridged Adapter หมายถึง virtual machine ที่รันจะพยายาม obtain IP address จาก source เดียวกับที่ currently active default network address ได้ IP มา ดังนั้นจึงเรียกว่า bridged เพราะสองอย่างเชื่อมต่อกัน
ถ้ามี active network device มากกว่าหนึ่ง สามารถเลือกว่าจะ bridge กับอันไหน ในกรณีนี้ใช้ wireless adapter wlan0
ตัวอย่าง: Host lease address 192.168.1.100 จาก router Virtual machine lease address 192.168.1.103 จาก router สองเครื่อง share same network และ standard rules ทั้งหมด apply สำหรับ practical purposes virtual machine เป็น IP address อีกตัวบน LAN ของคุณ
Setup นี้ไม่ทำงานถ้า device (switch, router, ISP ฯลฯ) ไม่อนุญาต lease มากกว่าหนึ่ง IP address ดังนั้น computers with direct Internet access อาจไม่ใช้ Bridged networking ได้
Pluses: ยืดหยุนจัดการ network port forwarding และ services enabled อนุญาต network sharing แบบคลาสสิก
Minuses: อาจไม่ทำงานกับ direct Internet access (ต้องการ router) ยากต่อ new users expose machines ไป network มี security implications
Host-only Adapter
Host-only Adapter น่าสนใจ มากกว่า Bridged Adapter แต่ใช้ dedicated network device ชื่อ vboxnet0 lease IP addresses
Host machine เป็น de-facto VirtualBox router ด้วย IP 192.168.56.1 Adapter ไม่ในใช้ถ้าไม่มี virtual machines รัน Host-only setup แต่เมื่อมันขึ้น adapter นี้ serve IP addresses ให้ virtual machines สร้าง internal LAN ภายใน network ของคุณ
ตัวอย่าง: Host IP 192.168.56.1 Virtual machine IP 192.168.56.101
คล้าย VMware Server VMware Server มี virtual adapters vmnet1 และ vmnet8 ใช้ assign NAT และ host-only IP addresses ให้ guests แต่ต่างจาก VirtualBox NAT adapter VMware Server always bridges default network device บน hosts ดังนั้นคุณมี direct network access ไป NAT-ed machines คุณไม่มี luxury นี้บน VirtualBox (yet)
แตการเพิ่ม vboxnet0 ใน VirtualBox 3 ทำให้ network usage 簡單มากใน product ปนоминаลนี้ ถ้าอยาก recall trouble ที่ผม deal กับ earlier releases VirtualBox ดู VMGL tutorial ผมต้อง manually configure ทุกอย่าง BTW คุณสามารถเปลี่ยน default IP address allocation ได้
Very importantly โปรดสังเกตว่าใช้ Host-only adapter ไม่หมายความว่า guests จะมี Internet access จริงๆ แล้วไม่มี vboxnet0 ไม่มี default gateway เพื่อทำให้ vboxnet0 serve queries นอก local network คุณต้อง configure ให้ใช้ adapter อื่น enable forwarding และ reconfigure firewall rules ท้ายที่สุดคุณจะได้ Bridged networking เลย зачем bother?
Host-only Adapter useful สำหรับสร้าง private networks ที่ machines ต้องการ access กัน แต่ไม่จำเป็นต้องออกนอก subnet
Pluses: useful สำหรับ noisy software testing, penetration testing อนุญาต classic network sharing via IP address
Minuses: ยากเข้าใจเท่า Bridged networking สำหรับ new users ไม่มี Internet access ใน virtual machines อาจ introduce security risk ให้ machines อื่นบน private network
Internal network
Internal network ไม่น่าสนใจในมุมมองผม คล้าย Host-only + NAT แต่ networking เกิดขึ้นภายใน virtual network ของ guest machines โดยไม่มี access ให้ host บวกไม่มี real NAT สิ่งที่ได้คือ private LAN สำหรับ guests เท่านั้น ไม่มี access สู่โลกภายนอก
Sharing in VirtualBox
ตอนเข้าใจ networking options มาแล้ว ลอง sharing มีสองวิธี share data ใน VirtualBox: direct network access ใช้ IP addresses หรือใช้ Shared Folders feature
ส่วนตัวชอบ network solution เพราะเป็นวิธีที่ things work ใน real world ส่ง requests ไป machines อื่นผ่าน network ถ้ามี sharing services เช่น NFS หรือ Samba listening และ authenticated properly จะได้ list of shares และ exchange files ได้ อีกวิธีคือ FTP หรือ SSH อีกครั้งใช้ classic network channels
Shared Folders คือ feature ที่คุณสร้าง folder บน host แล้ว mount เข้าไปใน virtual machines ทำงานกับ all network setups และใช้ internal VirtualBox sharing server ให้ guest access shared data
Sharing via network
กล่าวไปก่อนหน้านี้ sharing via network possible เฉพาะ Bridged และ Host-only network มาดูสองตัวอย่าง ขั้นตอนแรก allow sharing ใน guest machine
จากนั้น access virtual machine ผ่าน IP address
On Windows:
บน Windows ใช้ Start > Run > \\XXX.XXX.XXX.XXX แทน triplets of Xs ด้วย actual IP ของ virtual machine
On Linux:
พิมพ์ smb://XXX.XXX.XXX.XXX ใน address bar ของ file manager smb:// ย่อมาจาก Samba network protocol triplets of Xs คือ IP address ของ guest machine
กับ Bridged networking ดูแบบนี้:
กับ Host-only ดูแบบนี้:
Ignore title Windows shares Samba มักใช้ share กับ Windows hosts NFS ใช้มากกว่าสำหรับ Linux machines Samba preferred เพราะทำงานกับทั้งคู่ รายละเอียด Samba sharing ดู tutorial นี้
Shared Folders
P.S. ทั้งหมดข้างต้น identical สำหรับ Windows และ Linux guests เพื่อนนี้ ผมตัดสินใจ取最后一组 screenshots demonstrating Shared Folders บน Windows machine
Shared Files ค่อนข้าง simple setup ขั้นแรก configure folder บน host ที่อยาก share สำหรับ virtual machine ใดๆ เปิด Settings menu ไป Shared Folders
ใน right pane จะมี list of all shared folders บน machine เพิ่มและลบได้ตามต้องการ little icons ขวาใช้สำหรับนั้น
Folder Path:
นี่คือ actual physical path ที่อยาก share บน Windows จะเป็น C:\shared บน Linux จะเป็น /home/roger/shared
Folder Name:
นี่คือ name of the share ที่ guests จะเห็น ใช้ names without spaces เช่น Shared-folder หรือ Banana name นี้สามารถ identical กับ actual folder ที่ share แต่ไม่จำเป็น แต่โปรดสังเกต นี่ไม่ใช่ path! คือ symbolic name สำหรับ virtual machines ใช้ เพราะพวกมันไม่เห็น physical hardware ของคุณ
เมื่อสร้าง path แล้ว boot virtual machine
On Windows:
ถ้า virtual machine เป็น Windows host คุณสามารถ access network share ผ่าน command line หรือ Tools > Map Network Drive ใน Explorer menu
บน command line ใช้ net use command
net use
net use คือ command สำหรับ mount network drives
\\vboxsvr\share-name คือ path to the share \\vboxsvr คือ VirtualBox sharing server share-name คือ actual folder name จากก่อนหน้า ถ้าคุณตั้งชื่อ share Rambo path จะอ่าน \\vboxsvr\Rambo
Command จะกลายเป็น:
net use h: \\vboxsvr\Rambo
Via Tools menu ใน Explorer ดูแบบนี้:
สามารถ check Reconnect at logon เพื่อไม่ต้อง repeat task ทุกครั้งที่ power on virtual machine เมื่อคลิก Finish จะมี new drive ภายใต้ My Computer เปิด new drive letter นี้จะนำคุณเข้า Shared folder
On Linux:
ทำได้ด้วย simple command ใน terminal window:
mount -t vboxsf share mount_point
vboxsf คือ pseudo-filesystem type ใช้ mount Shared Folder abstract translation layer สำหรับ virtual sharing service ที่ VirtualBox ใช้
share คือ name ของ folder จากก่อนหน้า จำได้ไหม name ไม่ใช่ path! เช่น share 叫 Banana share = Banana
mount_point คือ directory ใดๆ ใน Linux tree ที่อยาก mount Shared folder ไป เช่น /home/roger/shares-go-here
ดังนั้น command ของเรา:
mount -t vboxsf Banana /home/roger/shares-go-here
และทุกอย่างใน shared folder บน host จะ visible ภายใน virtual machine โดยไปที่ path ข้างต้น
เพื่อ automate task เพิ่ม mount option ใน /etc/fstab menu อีกครั้งรายละเอียด how to do this ดู Linux commands tutorial ของผม
BTW สังเกต help ใน Settings menu อธิบาย how to achieve this!
และนี่คือทั้งหมด
สรุป
ตอนนี้คุณรู้ทุกอย่างที่ต้องรู้เกี่ยวกับ VirtualBox network & sharing Windows, Linux, bridged, host-only, NAT, sharing via network, sharing via Shared Folders คุณมีทั้งหมดแล้ว หวังว่าคุณ enjoy tutorial นี้
Stay tuned for more good stuff
Cheers.