Using VirtualBox Host-only Networking to run servers in your lap

Since the initial blog about VirtualBox and Networking, there have been lots of people asking how to run multiple server vm's on their laptops, allowing the host to also connect to these servers too. As it happens, I recently needed just such a configuration myself so thought I'd share how I did it...

On my Oracle Linux laptop, I wanted to set up a private network within my host on which I would run:

  • A Oracle Linux server running Oracle VDI which includes a MySQL database, Apache webserver, and other stuff;
  • A Windows Server 2012 providing DNS, DHCP and Active Directory;
  • I also wanted my Linux laptop to be able to reach these guest machines on the private network, too. 

Note that this had to be isolated to within my host machine because I was setting up a new Active Directory Domain (example.com) and we didn't want the Windows Server dishing out DHCP addresses to everyone in the office. But we did want the Linux Server to be able to talk with the Windows Server for directory services and name services.

So logically this looked like:

Network

Creating the Windows Server VM

I used the VirtualBox Manager to create a vm of OS type "Windows 2012 (64-bit)" but before running it for the first time, I modified the Network configuration of the guest to use the VirtualBox Host-only Ethernet Adaptor:

Host Only

I planned to use this Windows server to deliver DHCP addresses for the private host-only network, so I disabled the built-in DHCP server via the Preferences...Network dialog in the VirtualBox Manager (all this can be done form the command line too BTW). Like this:

DHCP Off

And in the interests of full disclosure, here are my private adapter settings too:


I then installed Windows Server 2012 giving it a static IP address of 192.168.245.110 and name ad.example.com.

After initial install I added extra roles to make the server be an Active Directory Domain Controller, DNS Server and DHCP Server:

AD Services

Creating the Linux Server

The Linux server is an Oracle Linux (6.3) server. Again, I set the newly created vm to use a Host-only network (as above), and installed Oracle Linux, giving it  a static IP address: 192.168.245.111 - vdi1.example.com, and set DNS to point to the AD server ad.example.com. 

This all worked swimmingly, and both machines could see each other and use each other's services:

Ping and nslookup

 N.B. the servers running in these vm's are full blown instances so watch out for security settings and the like which block connections between them and the host.

The Host as a member of example.com 

The great thing about host-only networks is that the host itself sits on this network and so can partake in the fun.

The host sees this network just as another NIC:

$ ifconfig vboxnet0
vboxnet0  Link encap:Ethernet  HWaddr 0A:00:27:00:00:00  
          inet addr:192.168.245.1  Bcast:192.168.245.255  Mask:255.255.255.0
          inet6 addr: fe80::800:27ff:fe00:0/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2425 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 b)  TX bytes:643528 (628.4 KiB)

Note that the IP address the host uses is configured when you set up the host-only network above.

Using Multiple NICs

I wanted to update the Windows and Linux guests using Software Update, but to do that I needed access to the Internet, which my host only adaptor did not provide for me. One way of doing this is to temporarily switch from Host-only to NAT networking, do the update, then switch back again. And VirtualBox lets you do this while the VM is running which is very cool.

But for my Linux VM I wanted something a bit more permanent. So I created a second Bridged virtual network adaptor so that my Linux VM had an address on my host's network as well as the example.com host-only network. To do this you do have to shutdown the guest OS, and then reconfigure the VM like this:

2 Adaptors

The guest simply then sees this as another interface: 

$ ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:31:23:9F  
          inet addr:hidden  Bcast:hidden  Mask:255.255.255.0
          inet6 addr: hidden Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:563846 errors:0 dropped:0 overruns:0 frame:0
          TX packets:360395 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:347709416 (331.6 MiB)  TX bytes:260792184 (248.7 MiB)
eth1      Link encap:Ethernet  HWaddr 08:00:27:4D:34:8B  
          inet addr:192.168.245.111  Bcast:192.168.245.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe4d:348b/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:468955 errors:0 dropped:0 overruns:0 frame:0
          TX packets:387661 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:354834569 (338.3 MiB)  TX bytes:104217032 (99.3 MiB)

Things to watch out for 

Firewalls

Server Operating Systems typically come as "secure by default" so watch out for firewalls blocking connections. As your host-only network is private anyway you could simply turn the firewall off, e.g. on Linux:

/etc/init.d/iptables stop 

Nameserver woes 

One issue that had me scratching my head for some time was that after I added a second interface to my VDI server, my name resolution stopped working. Eventually I figured it out: Linux has a feature called NetworkManager which detects new networks and reconfigures the system to use them. One of the results of this "reconfiguration" is an overwrite of the /etc/resolv.conf file which points to the nameservers. In my case this meant that the VDI server was no longer using the AD server for DNS. Linux experts could probably tell me how to elegantly fix this, but I found 2 solutions myself:

  1. Disable the Network Manager, so it will not restart at next boot. (sledgehammer)

    chkconfig NetworkManager off

  2. Use entries in /etc/hosts and ensure that /etc/nsswitch.conf has the line:

    hosts: files dns 
    

 Conclusion

I now have a very cool setup on my laptop which enables to play around with Oracle VDI, MySQL, Apache, Active Directory, and all the other services that Linux and Windows Servers offer, all without disturbing anyone else on the network.

Desktop

Hope this is useful to someone out there.

- FB




From:https://blogs.oracle.com/fatbloke/entry/virtualbox_vms_with_multiple_vnics



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章