Saturday, November 2, 2013

How to Create a CentOS 6 Minimal Virtual Machine with VMware Player

UPDATE: This tutorial was written for CentOS 6. Creation of a CentOS 7 minimal virtual machine is very similar, but there are some differences. For CentOS-7-specific instructions, see CentOS 7 Minimal on VMware Player.

Today, a lot of pre-built VM images for specific purposes (e.g., LAMP, Drupal, Moodle, Joomla, etc.) are available. They can save a ton of time since you don't have to start from scratch (assuming, of course, that the images come from a reputable source). However, bear in mind that whoever builds those images always makes certain assumptions. For example, it is almost always assumed that in a LAMP stack the web server and the database server should be on the same virtual machine, which may or may not be what you want. Of course, you could take two identical LAMP images, remove the MySQL-related software packages from the first and all the Apache-PHP-related packages from the second and make the two work together. Still, starting from scratch is also an option, and in some cases (when the requirements of your application are very unique) the best option.

There may be a whole bunch of specific reasons why one would want to start with a minimal installation of an operating system, so I am not going to speculate about those, but, generally speaking, it allows to avoid software bloat and, therefore, maximize utilization of resources and reduce security risks. Bear in mind that minimal installs of most Linux distributions are just that - just enough OS to boot the system or slightly more. So, in order for it to actually do something useful, you will have to install additional software (again, what software to install depends on your specific needs, of course).

This post provides step-by-step instructions how to
  • create a VMware virtual machine from a CentOS-6.4-x86_64-minimal.iso (or CentOS-6.4-i386-minimal.iso) using VMware Player 6.0.1 on Windows 7;
  • bring the virtual machine to a minimally usable state, i.e. to be able to connect to CentOS mirrors in order to update your new system and install additional software packages;
  • install VMware Tools 9.6.1.
Other versions of the software involved in all this may require slightly different steps, but the general principles are the same.

Let's Get Started

First, download the CentOS-6.4-x86_64-minimal.iso (or CentOS-6.4-i386-minimal.iso) image from one of the CentOS mirrors. Save it anywhere on your local disk.
    Note: Shortly after I wrote this tutorial, CentOS 6.5 was released. So, now the ISO images you are going to download are CentOS-6.5-x86_64-minimal.iso or CentOS-6.5-i386-minimal.iso. I have not tested these instruction with the new release, but it is very unlikely that anything should be considerably different.

Pre-Configure Your Future Virtual Machine

Start VMware Player (download and install if you have not done so; remember that it is free only for non-commercial use). At the moment of this writing, the most recent version of VMware Player is 6.0.1 (VMs are compatible with VMware Workstation 10).

Click "Create a New Virtual Machine".

On the next dialog box, select the "I will install the operating system later..." option (automatic installation doesn't work correctly because VMware Player assumes that you need a desktop system with GUI). Click "Next".

On the next dialog box, under "Guest operating system" select "Linux" and under "Version" - "CentOS 64-bit" (or "CentOS" if you are installing the 32-bit version). Click "Next".

On the next dialog box, give your virtual machine a more descriptive and recognizable name (if you have many virtual machines, you'll be glad you did) and, optionally, edit the location (on Windows it defaults to "C:\Users\YourUserName\Documents\Virtual Machines\VirtualMachineName"). Click "Next".

In most cases, you will hardly need 20 GB (default) of disk space for it. So, on the next dialog box, you can give it less or keep the default (don't worry - it doesn't pre-allocate disk space). There is hardly any reason to split even a 20 GB virtual disk into multiple files, so select "Store virtual disk as a single file". Click "Next".

On the next dialog box, click the "Customize Hardware..." button. You may (although don't have to) want to change the default configuration of the virtual network adapter. The default configuration is "NAT: Used to share the host's IP address", but if you would rather have your virtual machine behave more like a real computer on your network (here I assume that that's what you want), change it to "Bridged: Connect directly to the physical network". Again, assuming that you are building a virtual server, you can probably safely remove the "USB Controller", "Sound Card" and "Printer". Keep the CD/DVD drive (otherwise you won't be able to install the operating system and VMware Tools). You can reduce the memory to as little as 512 MB (maybe, even less). When finished, click the "Close" button.

Click "Finish".

Prepare to Install CentOS

Click "Edit virtual machine settings" (make sure the correct virtual machine is selected!).

Under "Hardware", select "CD/DVD". Under "Device status", make sure that the "Connect at power on" box is checked. Under "Connection", select the "Use ISO image file:" option, and then "Browse..." to where you saved the downloaded CentOS-6.4-x86_64-minimal.iso (or CentOS-6.4-i386-minimal.iso) image. Click the "OK" button.

Install CentOS

Click "Play virtual machine" (again, make sure the correct virtual machine is selected).

Your new virtual machine will boot from the virtual CD/DVD drive. When the boot options screen comes up, hit "Enter" to select "Install or upgrade an existing system" (default). The "fancy" installer requires at least 640 MB (or something like that) of memory, but, if you allocated less than that, the old-style installer will look a little uglier, but will do exactly the same. You will not be able to use your mouse - read the explanation on how to navigate the install wizard at the bottom of the blue screen. The install wizard itself is mostly self-explanatory with the exception of a couple of steps explained below.

Skip the media test.

At some point you may see a "Warning" that says something like "Error processing drive:... VMware, VMware Virtual S This device may need to be reinitialized...". Go ahead and "Re-initialize".

On the "Time Zone Selection" screen, uncheck (using the space bar) the "System clock uses UTC" option. Select the correct time zone and continue.

Create a root password. Don't forget it!!!

On the "Partitioning Type" screen, select "Use entire drive" (don't worry - it's the entire virtual drive).

On the "Writing storage configuration to disk" screen, select "Write changes to disk".

The installer then will proceed to install 199 packages, which may take a couple of minutes.

When finished, the installer will prompt you to reboot the virtual machine. Reboot.

Enable Networking and Update the System

Login as root with the password created during the installation.

By default, networking is not enabled. To make sure, run
You will see that the IP address of your virtual network adapter is Now, you need to edit /etc/sysconfig/network-scripts/ifcfg-eth0 (assuming that your virtual network card is eth0, which it most likely is). To do that, run
vi /etc/sysconfig/network-scripts/ifcfg-eth0

In ifcfg-eth0, change "ONBOOT=no" to "ONBOOT=yes" and "NM_CONTROLLED=yes" to "NM_CONTROLLED=no". "BOOTPROTO=dhcp" should be there by default (I am assuming that you want your virtual machine to get its IP address/es from your DHCP server/router). In order to save and exit the editor, hit "Esc", then - type :wq, then hit "Enter". Just to double-check, run
cat /etc/sysconfig/network-scripts/ifcfg-eth0
and see if your changes have been saved correctly.

Now, in order for your changes to take effect, run
service network restart

to make sure that your virtual network adapter has a usable IP address now.

Now you should be able to connect to a CentOS mirror and update your new system by running
yum update

Not to torture yourself with the vi editor, you may also install nano by running
yum install nano
While you're at it, you might as well install wget. It also might be a good idea to add the EPEL (Extra Packages for Enterprise Linux) repository:
rpm -Uvh epel-release-6-8.noarch.rpm
You don't really have to download the rpm. You can just run
rpm -Uvh
Obviously, for the 32-bit version - replace x86_64 with i386.
Now you can install htop, which - for me - is the main reason to add the EPEL repository.

Install VMware Tools

Boot up your new VM and login as root.

In order to be able to install and configure VMware Tools, you need to install some extra packages. With the current version of CentOS (6.4) and VMware Tools (9.6.1), the only missing package appears to be Perl (VMware Tools installer is a Perl script), which may not always be the case (see below). To install Perl run:
yum install perl

In the /mnt directory create cdrom subdirectory by running
mkdir /mnt/cdrom/

From the VMware Player menu select "Manage" > "Install VMware Tools". It should prompt you to mount the virtual CD drive in the guest. So, inside the VM, run
mount /dev/cdrom/ /mnt/cdrom/
You will get a message something like block device /dev/sr0 is write-protected, mounting read-only, which is fine.

Now, in the /opt directory create a subdirectory, e.g. vmwaretools, by running
mkdir /opt/vmwaretools/
and copy everything from the virtual CD drive into that directory:
cp /mnt/cdrom/* /opt/vmwaretools/
To make sure that everything was copied, you can run
ls /mnt/cdrom/
ls /opt/vmwaretools/
and compare the results (just in case).

You can unmount the virtual CD drive now:
umount /dev/cdrom/ /mnt/cdrom/

Go to the /opt/vmwaretools/ directory
cd /opt/vmwaretools/
and unpack the archive with VMware Tools
tar -xzvf VMwareTools*.tar.gz (the exact file name will depend on the version of VMware Player you are using).
This should create a subdirectory vmware-tools-distrib inside /opt/vmwaretools/ with a whole bunch of subdirectories and files in it.

Go to that directory:
cd /opt/vmwaretools/vmware-tools-distrib/
and run

Accept all the defaults during the installation until you get to the prompt whether you want to run /usr/bin/ Make a note of the name of the script and its location because, in case it fails now due to missing packages it depends on, you will have to install those missing packages and run it again. Now, you can let the install script run the configuration script for you.

There is hardly any reason to enable the folder sharing feature, so, when you get to that prompt, you can answer "no" (the default is "yes"). The same goes for the dragging and copying feature (the default is "yes") and printing (the default is also "yes"). If you want to, you may enable automatic kernel modules (the default is "no").

If the configuration script complains about some missing packages (the most likely suspects are: gcc, make, kernel-devel), install them and re-run
cd /usr/bin/
By the way, you will have to re-run this script every time the kernel is updated.

That's it. Continue building your lean and mean virtual machine, but before you begin experimenting, "clone" your VM in this state: should something go wrong, you won't have to start from scratch again. Bear in mind that, when you make a copy of you VM, network interface initialization on the copied VM will fail. It happens because VMware Player assigns a different MAC address to the virtual network card of the copied VM, of which the copied VM itself is unaware. A quick and dirty fix for that is to:
  • delete the file 70-persistent-net.rules located in the /etc/udev/rules.d/ directory,
  • delete the lines UUID=... and HWADDR=... in the file /etc/sysconfig/network-scripts/ifcfg-eth0 (assuming that your network card is eth0),
  • restart network service.

It is more than likely that you will want to run a web server (Apache or some other) on your virtual machine. By default, all ports are blocked by iptables, so don't get frustrated because the web server is "not working". Take a look to verify if that's the case with your VM:
iptables -L
If it is, just open port 80 (and 443 if you are going to use https; do the same for other ports that you need open):
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
Test the web server and, if everything works as expected, save the new iptables settings:
service iptables save

Here is another potential source of many hours of pain and frustration.

If you want to run Apache and PHP (or, I guess, any other web stack) on one server and your MySQL (or any other) database on another server, you might run into an extremely frustrating situation when, even though you have created all the necessary firewall rules on both of them (or, possibly, even disabled the firewalls completely) and can access the MySQL server from your desktop using all imaginable clients (directly via TCP, not through SSH), the web server still fails to connect to the database.

The thing is that, out of the box, SELinux does not allow httpd to access anything that is not on the machine where it (the web server that is) "lives". Sounds logical when you know it, but not easy to figure out when you don't.

So, to save you the pain and frustration I've just experienced myself, here is what you need to do. You have to explicitly tell SELinux to allow httpd to access the network:
setsebool -P httpd_can_network_connect on

Here is more on SELinux Booleans. You might also want to read the more systematic SELinux HowTo.


Jim Hubbard said...

Thank you for taking the time to write this tutorial. I just installed my first CentOS server in VMware and it went off without a hitch thanks to your tutorial.

Thanks again! I highly recommend this tut to others seeking to do the same.

Ira Portman said...

You are welcome, Jim!

Catriona said...

Really helpful tutorial, bookmarked for future ref! Totally new to windows/virtual machines, and very new to linux also. Successfully downloaded full desktop (not minimal) CentOS 6.5 in VMware player without any major difficulties. Installing VMware tools took a slightly different turn but nothing difficult.

Thank you!

Ira Portman said...

You are most welcome, Catriona! How was your VMware tools installation different from what I described?

fikret tursak said...

Here is nice video tutorial, installing CentOS 7 in VMware Workstation

Ira Portman said...

fikret tursak, as you may have noticed, this post is about creating a _minimal_ CentOS _6_ VM (the video you recommended is for CentOS 7 with GUI). There is a more recent version of this post for CentOS 7 (also minimal) here:
Thanks anyway.