Technical Recipes

How to set up static IP for centOS in VirtualBox

1. Download and install VirtualBox on Windows with centOS 6.3
 
    1.1 Download VirtualBox 4.2.6 for Windows hosts
 
        https://www.virtualbox.org/wiki/Downloads
 
    1.2 Go through the installation process and put an icon on the desktop and system tray
 
    1.3 Download the free VirtualBox image: CentOS 6.3 Gnome Desktop x86
 
        http://virtualboxes.org/images/centos/
 
        Size (compressed/uncompressed): 492 MBytes / 2.2 GBytes
        Link: http://sourceforge.net/projects/virtualboximage/files/CentOS/6.3/CentOS-6.3-x86.7z
        Active user account(s)(username/password): root/reverse, centos/reverse.
        Notes: GNOME desktop environement, install from LiveCD; Guest Additions NOT installed.
 
        Extract the 'CentOS 6.3 x86' directory with two files to a safe place which can be accessed from Windows Explorer and open it in VirtualBox Manager console. Or open VirtualBox and click the 'New' button in the top right, choose the best operating system/version that matches your install. For CentOS choose
		
        Operating System: Linux
        Version: Red Hat or Red Hat (64bit)
 		
        to install the Linux image.
 
    4) Set up memory, harddisk and other options which can always change later
    
2. Change VirtualBox network settings
 
    To enable static IP addresses on your VirtualBox VMs, you'll first need to setup a Host-Only network. The host-only network is a virtual network provided by VirtualBox that is visible only to the Host OS and its virtual machines.
 
    VirtualBox should setup a default host-only network during installation, click menu bar
 
    File -> Preferences -> Network
 
    you'll be able to add a Host-Only network if it doesn't exist.
	
    Attache to: Host-only Adapter
    Name: VirtualBox Host-Only Ethernet Adapter
	
    Double click the "VirtualBox Host-Only Ethernet Adapter", you can see the IPv4 Address and IPv4 Network Mask, as well as the DHCP Server Address and the Lower/Upper Address Bound. It does not matter if the Enable Server mark is checked.
    
    Now you have two network adapters - a NAT adapter for Internet connectivity; and a Host-Only adapter for the network we configured. When you create your new VM settings, VirtualBox should add a NAT adapter by default.
 
3. Configure static IP on Windows (Host-Only Network)
 
	Go to:
 
        Control Panel -> Network and Internet -> Network and Sharing Center -> Local Area Connection
 
	Click the Properties button. It will show you the Local Area Connection Properties dialog. Double click on the Internet Protocol Version 4 (TCP/IPv4) in the item box of the dialog window. Click the Use the following IP address radio button and enter the appropriate values for your static IP address. 
 
4. Configure static IP on CentOS 6.3
 
    4.1 Disable the Network Manager
    
    4.2 Keep the first ethernet adapter and the loopback adapter untouched
		
        # chattr +i /etc/sysconfig/network-scripts/ifcfg-eth0
        # chattr +i /etc/sysconfig/network-scripts/ifcfg-lo
		
    4.3 Find the system default network setting
 
        # ifconfig
 
        Record the following information:
		
        eth0:  HWADDR="xx:xx:xx:xx:xx:xx"
        IPADDR="192.168.1.101
        NETMASK=255.255.255.0
        eth1:  HWADDR="yy:y:yy:yy:yy:yy"
        IPADDR="192.168.56.101
        NETMASK=255.255.255.0
        
        If there is a Network Manager enabled, add the following line and save before run the above command:
 
        NM-CONTROLLED="no"
 
        The essential ingredient here is "NM_CONTROLLED=no". Without that setting, NetworkManager will treat your alias as a real device and mess up your network accordingly.
 
    4.4 Create from copying the ifcfg-eth0 to /etc/sysconfig/network-scripts/ifcfg-eth1 and modify ifcfg-eth1
		
        DEVICE="eth1"
        BOOTPROTO="static"
        ONBOOT="yes"
        HWADDR="yy:y:yy:yy:yy:yy"
        IPADDR=192.168.1.220
        NETMASK=255.255.255.0
        GATEWAY=192.168.1.1
        NM-CONTROLLED="no"
        
    4.5 Create more static IPs on the same network interface
		
        DEVICE="eth1:0"
        BOOTPROTO="static"
        ONBOOT="yes"
        HWADDR="yy:y:yy:yy:yy:yy"
        IPADDR=192.168.1.221
        NETMASK=255.255.255.0
        GATEWAY=192.168.1.1
        NM-CONTROLLED="no"
		
    4.6 Make sure the network file unchanged
		
        # vi /etc/sysconfig/network
        NETWORKING=yes
        HOSTNAME=centos.somedomain.local
 
        # vi /etc/resolv.conf
        nameserver 8.8.8.8
		
    4.7 Restart network service
		
        # /etc/init.d/network restart
		
    4.8 Verify the static IPs are available from the host
		
        > ping 192.168.1.220
		
5. Switch between "Host-Only Adatper" mode and "Bridged Adapter" mode
 
    If you need the VM being accessed either from host only or from other local network boxes, you can use the VMWare Player or Workstation to configure the static IP using the way the same as above, and switch between the two modes accordingly. However, they cannot be accessed by both the host and other local network devices at the same time.