info home Info Homegeneral Generalwindows Windowsunix linux Linux/Unixdebian Debianmac os Mac OSapache Apachenginx Nginxiphone iPhone/iPod Touch

Tags

VirtualBox: How to access guest (CentOS) webserver from the host (Mac OS X) on NAT

CentOS VirtualBox

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:

$ 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:

ssh 192.168.56.101

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 system-config-firewall.

  sudo system-config-firewall

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.

firewall configuration tool

Navigate to http://191.168.56.101. It will show up.

First written in January 2013

Last Updated on 29 January 2013

If you liked this article, subscribe to our Feed, follow us on Twitter (@aruljohn) and/or join our Facebook Page.