10/5/2014 - 7:42 AM

Centos on Mac with Virtual Box

Centos on Mac with Virtual Box

If you want to learn how to administer a Linux server or have a need to test your application on a specific Linux distribution, a good point of start is to virtualize it locally using VirtualBox. This post explains how get CentOS up and running on OS X. Our goal here is to install minimal Linux server and access it for administration via ssh from your Host operating system. We will also enable the Guest system to act like a web server.

If you don't have VirtualBox already go to download page and install it.

To get an image of CentOS go here and choose the version you want (I went for 6.5) by clicking on x86_64 then pick one of the mirror servers for downloading the desired image (my choice was CentOS-6.5-x86_64-minimal.iso).

Instalation steps.

Run VirtualBox

  • Clik New.
  • Choose the Name of you choice, Linux for Type, Red Hat 64 bit for Version.
  • Give at least 1024 MB of memory.
  • Check Create a virtual hard drive now
  • Hard drive filetype: VDI.
  • Storage on physical hard drive: Dynamically allocated.
  • File location and size: leave 8 GB for the size.

Go to Settings

  • System -> Processor: set 2 cores.
  • Mount the operating system disk image of Cent OS minimal that you have downloaded earlier: select storage tab, select Empty in Storage Tree frame. In Attributes section browse to your iso by clicking the cd icon.
  • Click start (the green arrow) - this will start the installation process in graphical mode.
  • Choose the first option.
  • Skip the media test.
  • Choose the language.
  • Check the basic storage devices button.
  • The next screen will ask you to discard data - click yes.
  • Provide a server name and a root password.
  • Choose replace existing linux in one of the following screens.
  • Reboot after installation.

After rebooting log in in the terminal with the root user and the password you created during installation.

Connection to the Internet

If you run ifconfig in the Guest console, the only network interface listed is lo (loopback device). The first action to take in this case will be setting up another two interfaces: one for accessing the Internet, second for connecting the Guest Linux from your Host OS via ssh.

To enable eth0 (access to the network) edit /etc/sysconfig/network-scripts/ifcfg-eth0 and change ONBOOT to yes. Restart the network service by running: service network restart. You can check your internet connection with ping (all must be done as root, not via sudo).

Host to Guest ssh connection

Shut down your Guest VM.

Go to VirtualBox -> Preferences, pick the Network and Host-only Network tab. Click plus on the right hand (Add host-only network) to create vboxnet0. Then go to your Guest VM settings, tab Network -> Adapter 2. Click Enable Network Adapter and choose Host-only Adapter. Now boot up your Guest VM, navigate to /etc/sysconfig/network-scripts/ and create a new file ifcfg-eth1 with the following content:


Run ifup eth1.

The last thing will be opening port 22. Go to the Guest VM settings - Network - Adapter 1 - port forwarding - add and set Host and Guest ports to 22.

Now you should be able to connect to your Guest VM from your Host OS terminal: ssh root@ with the root password.

From now on you can administrate the CentOS server without switching to the VirtualBox instance console.

Enable the Guest VM to act like a web server.

First you have to open port 80 on your Guest machine. To to this edit the file /etc/sysconfig/iptables and add the following line: -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT. Restart the network service: service network restart. To test the server you can use python's SimpleHTTPServer: create a new folder for storing your http content, for example /var/www and place there a new index.html file with a content of your choice. Now run python -m SimpleHTTPServer 80. This command will start a http server listening on port 80. If you navigate a browser to you should be able to see the content of your index.html file.

To take the burden of remembering the IP number off your memory, let's create an easy to remember host name, both for ssh and http access. Edit the /etc/hosts file (on your Host) and attribute the IP an easy to remember name, for example: centos.local.

Now you can access your Guest VM by ssh root@centos.local and check the web server using the http://centos.local url.

If you've come so far, it means that you have succesfully installed your Linux distribution. Now you are ready to dig for more information about how to install the technology stack of your choice.