VirtualBox: How to access guest (CentOS) webserver from the host (Mac OS X) on NAT
Most of my clients use CentOS on the production server. So, I usually end up installing VirtualBox or VMWare Fusion on my work machine and a CentOS guest instance to simulate the production server.
I generally prefer bridged networking if there are no restrictions, so my virtual machine is assigned its own DHCP generated IP address.
But in some cases, if the MAC address of my host machine has to be registered and that's a hassle, the best thing to do is to set up NAT network. This way, the guest vm can access the Internet using the IP address assigned to the host. Internally, the guest will have its own private network, and won't be accessible by the host.
In order to make the guest accessible from the host, we'll need to enable a second network adapter Host-Only network adapter. To do that, follow these steps:
- Shutdown the CentOS guest vm.
- Go to VirtualBox → Preferences → select Network tab and create a Host-Only Network adapter.
- Click on (spacebar) or on "Edit host-only network" icon on the right side. Select the tab DHCP Server. You should see something like this: You can either set up your static IP or leave it at DHCP like I did.
- Go back to the main VirtualBox app and click on Settings. Select Network tab. Your first network adapter should be NAT. Click on Adapter 2 and change the adapter type to Host-only Adapter. Assign it to "vboxnet0" which we just created.
- Start the CentOS guest vm. When it boots up, start the terminal and run ifconfig as root:
$ sudo ifconfig eth0 Link encap:Ethernet HWaddr 08:00:27:F4:97:37 inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fef4:9737/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:21 errors:0 dropped:0 overruns:0 frame:0 TX packets:38 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:10899 (10.6 KiB) TX bytes:5314 (5.1 KiB) eth1 Link encap:Ethernet HWaddr 08:00:27:F7:DE:60 inet addr:192.168.56.101 Bcast:192.168.56.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fef7:de60/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2 errors:0 dropped:0 overruns:0 frame:0 TX packets:24 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:1180 (1.1 KiB) TX bytes:4747 (4.6 KiB)
Our guest has been assigned an IP address of 192.168.56.101 which can be used from the host. Let's try to access the guest from the host by ssh-ing into it using Mac OS's Terminal. - Open the Mac Terminal and type this:
- It should connect and ask you for the username/password so you can login.
If you have a web server running on the guest and can't access it, even though you're able to ssh into the guest, you will need to open ports 80 (and poossibly 443).
CentOS: Unable to access webserver, but able to connect via SSH
PROBLEM: After enabling NAT and Host-Only network, I'm able to access the guest CentOS vm via SSH (port 22) but not via browser.
To enable the webserver to be accessible by the host OS, run as root:
You should then enable port 80, port 433 and other ports that you'll be needing from within the host. Then, click on Apply and Reload.
Navigate to http://184.108.40.206. It will show up.
First written in January 2013
Posted on 29 Jan 2013
If you liked this article, subscribe to our Feed, follow us on Twitter (@aruljohn) and/or join our Facebook Page.