主機網路連線到VirtualBox虛擬機器的方法

feelpurple發表於2016-07-28
在VirtualBox虛擬機器裡面搭建了資料庫,想在虛擬機器外面的主機作業系統中通過資料庫客戶端工具連線到虛擬機器裡面的資料庫。
在"設定"裡面配置"網路"選項

在"網路"裡面增加一塊網路卡2,使用"Host-Only"連線方式

在虛擬機器裡面配置網路
[root@localhost ~]# vim /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=localhost.localdomain
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1
# Please read /usr/share/doc/initscripts-*/sysconfig.txt
# for the documentation of these parameters.
GATEWAY=192.168.56.1
DEVICE=eth1
BOOTPROTO=none
NETMASK=255.255.255.0
TYPE=Ethernet
HWADDR=08:00:27:1d:31:c1
IPADDR=192.168.56.200
IPV6INIT=no
ONBOOT=yes
USERCTL=no

虛擬機器eth1網路卡的ip地址要和虛擬機器外面主機裡面虛擬機器的網路IP要處於同一網段
C:\Users\jijia>ipconfig

Windows IP Configuration

Ethernet adapter Local Area Connection* 9:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Ethernet adapter Local Area Connection:

   Connection-specific DNS Suffix  . : gemalto.com
   IPv4 Address. . . . . . . . . . . : 10.162.104.198
   Subnet Mask . . . . . . . . . . . : 255.255.254.0
   Default Gateway . . . . . . . . . : 10.162.104.1

Ethernet adapter VirtualBox Host-Only Network:

   Connection-specific DNS Suffix  . :
   IPv4 Address. . . . . . . . . . . : 192.168.56.1
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :

重啟虛擬機器網路
[root@localhost ~]# service network restart
[root@localhost ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:27:57:C8  
          inet6 addr: fe80::a00:27ff:fe27:57c8/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 b)  TX bytes:258 (258.0 b)

eth1      Link encap:Ethernet  HWaddr 08:00:27:1D:31:C1  
          inet addr:192.168.56.200  Bcast:192.168.56.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe1d:31c1/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:22 errors:0 dropped:0 overruns:0 frame:0
          TX packets:27 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:2403 (2.3 KiB)  TX bytes:1802 (1.7 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:112 errors:0 dropped:0 overruns:0 frame:0
          TX packets:112 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:8748 (8.5 KiB)  TX bytes:8748 (8.5 KiB)

這樣,虛擬機器外面的主機就可以通過網路訪問虛擬機器裡面的網路了
C:\Users\jijia>ping 192.168.56.200

Pinging 192.168.56.200 with 32 bytes of data:
Reply from 192.168.56.200: bytes=32 time=1ms TTL=64
Reply from 192.168.56.200: bytes=32 time<1ms TTL=64
Reply from 192.168.56.200: bytes=32 time<1ms TTL=64
Reply from 192.168.56.200: bytes=32 time<1ms TTL=64

Ping statistics for 192.168.56.200:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 1ms, Average = 0ms

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2122695/,如需轉載,請註明出處,否則將追究法律責任。

相關文章