Thursday, November 6, 2014

How to get a static IP address in an Oracle VirtualBox VM

You probably know that all Oracle software is free to download and most come with a developer license. This means that you can use full versions of products to develop your applications.

Installing the software could be a bit difficult especially when you are trying new programs.
Therefore Oracle releases VirtualBox images witch contain complete installed servers with e.g. Oracle 12c and Application Express pre-installed.

Follow these links to the pre-build VM's:
Pre-Built Developer VMs (for Oracle VM VirtualBox)
Database Virtual Box Appliance / Virtual Machine (my current favorite)

When you start using Oracle VirtualBox images the need for more grows fast. In a short time you probably have multiple VM's with different Oracle versions.

Connection to these images starts becoming a bit of a problem because the images get their IP address from a DHCP server, embedded in the VirtualBox software. The first VM started gets the first address available, the second the next and so on. So if you start the VM's in a different order, your tnsnames entries won't point to the right databases anymore.

It's time to configure the virtual machines to have a static IP address. In this blog I will explain how you can do this.

Add or change the virtual network interface adapter

Open the VirtualBox software and edit the settings of the virtual machine you want to assign a static IP address to. It has to look something like this:


We use the virtual host-only adapter that was installed on the host by installing the VirtualBox software. This adapter has a private IP range and we are free to chose any IP address we want.


Add or change the network interface inside the virtual machine

Next, we have to start the virtual machine and add the network interface in the virtual machine. If it is already present we need to change the settings of it.

In the virtual machine settings we added network adapter 2 which is called eth1 inside the virtual machine. Login to the virtual machine and check the network settings with this Linux command:

cat /etc/sysconfig/network-scripts/ifcfg-eth1
If the file is not present we can copy the settings from the ifcfg-eth0 file:

cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth1

Be aware to change the hardware address after the copy. First we get the hardware address:

ip addr show dev eth1 | grep link/ether | awk '{ print $2 }'

We will use the output of this command in the ifcfg-eth1 file.

Edit the network configuration file for adapter 2 (ifcfg-eth1) with your favorite text editor (e.g. vi). Change the file until it looks like this, put your own mac address in the HWADDR line:

DEVICE=eth1
IPADDR=192.168.56.10
NETMASK=255.255.255.0
NETWORK=192.168.56.0
PREFIX=24
GATEWAY=192.168.56.1
BOOTPROTO=none
ONBOOT=yes
HWADDR=08:00:27:a7:5b:1d

You can adjust IP addresses and devices as you wish. But keep in mind that the DHCP server range starts with IP 192.168.56.101. You should not use IP addresses above 100.

To make these settings work, restart the network:

/etc/init.d/network restart

And voilĂ ! You can reach your server on its static IP address!