Monday, February 20, 2012

"Network is unreachable" CentOS 6.2 as guest in VirtualBox.

Since this is the first post here I will explain that I have started working on a project and am going to use this blog to document progress and post various problems I run into with the solutions that worked for me.

The project I am working on involves using multiple distributions of Linux, and since I don't want to multi-boot I am using VirtualBox to develop specific things for each distro. I am running Windows 7 Ultimate 64 bit on the host machine.

After I had installed CentOS 6.2 in a virtual machine I ran into trouble with connecting to the internet. I tried using NAT, bridged, and host-only options in the network settings for the VM and still no such luck. After some googling and banging my head on my keyboard trying to find a solution I had come up with nothing that had worked. I eventually came across a post in the Debian forums that gave a couple of commands that I punched into the shell, more specifically the route command.

The output of the route command was as follows:


Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth0


... or something to that extent.
The issue was that I was behind a router and it wasn't reaching external networks. I was able to ping other VMs and my host machine and from both the guest and the host, but not able to ping google.ca. I would get a message telling me "network is unreachable".

The problem was solved when I changed the configuration a little bit in /etc/sysconfig/network-scripts/ifcfg-eth0. I appended the following lines to the file. I might also note here that when you configure your ifcfg file to work behind a router, the IP that you choose to use (I use a static internal IP for consistency, and it might be wise if you will be using them as virtual hosts) needs to be in the same network as your router. My router gateway is at 192.168.0.1 and this VM is using the static IP 192.168.0.19.

The entire file in it's working state is:


TYPE=Ethernet
DEVICE=eth0
BOOTPROTO=
NM_CONTROLLED=yes
NETMASK=255.255.255.0
USERCTL=yes
ONBOOT=yes
MACADDR=00:00:00:00:00:00
IPADDR=192.168.0.19
PEERDNS=yes
GATEWAY=192.168.0.1
NETWORK=192.168.0.0


After editing the fie and running /etc/init.d/network restart I was able to ping google and update/install things through yum.

That's what worked for me, if you've run into the same problem and were able to solve it in a different way please do let me know how you were able to solve it.

EDIT:
Upon setting up yet another VM and running into this same problem using Bridged Networking in Virtualbox, I was able to ping local machines, but not external machines on the internet or domain names.
To fix this issue and be able to resolve domains, I added this line to /etc/resolv.conf:

nameserver 8.8.8.8

After this I was able to resolve domains. There's probably another fix for that to do with the settings I configured previously, perhaps not. That's what worked for me and for those of you who don't know, 8.8.8.8 is one of the IPs for google's public DNS servers.

13 comments:

  1. Dear Matt,

    Thanks very much for the information on this post. It helped me in reaching out the Internet from my VBox CentOS guest too. By default the version 6.2 for some reason has manual networking mode and setting static IP address, one may miss out the PEERDNS=YES entry which ignores the DNS entries in resolv.conf file.

    I guess, best quick configuration of network adapter for first time is using the command: system-config-network, however this still doesn't set the PEERDNS value to Yes.

    Thanks again. I can now crack on with further test :)

    Suneit

    ReplyDelete
  2. cool you saved me some time.. thanks

    ReplyDelete
  3. its works, thanks saved my time

    ReplyDelete
  4. Thanks a lot! Fixed it within a couple of minutes. Cheers Simone.

    ReplyDelete
  5. Thanks, it worked for me. It just needs to be clear that the Network adapter be set to Bridged Adapter.

    ReplyDelete
  6. Next time, no need to change or edit any file, just make sure that the network interface of your system through virtualbox is set on NAT and open your command line and type these two commands:
    ifconfig eth0 down
    dhclient eth0

    it should work. it has worked for me.

    ReplyDelete
    Replies
    1. Worked nicely. Any way to have this happen when the VM boots rather than having to run it manually every time?

      Delete
    2. This works only if you have installed a full install. I am usually working with minimal installs (command line only) so eth0 doesn't exist. This is my fix for that situation.

      Delete
    3. Perhaps I was wrong. I just tried it in a minimal set up and I don't know if it's because I configured it beforehand or not, but it does work. I'll have to try it with a fresh install when I next have one.

      Delete
    4. I tried this on minimal setup without any other configuration and it worked fine, FYI. Also to start on boot you can add "dhclient eth0" to:

      /etc/rc.d/rc.local

      Delete
    5. This works for me...so simple

      Delete
  7. I have given static ip for centos in virtual machine, but i was not able to ping to my windows host machine or connect to internet. What setting you did to ping from guest to host machine from centos. But i am able to ping from host machine to centos. Can you please give me some suggestion.

    ReplyDelete
  8. Thanks for saving my sanity.

    Centos can be a prick sometimes. Centos-minimal does not have nano, wget or system-config-network-tui - the screen that allows easy network configuration. This is because it has a lot of dependencies which are not needed for a server/minimal install - for example, wireless stuff.

    After 3 hours of googling, testing and figuring out virtualbox networking methods, centos packaging policies, I can finally set my virtual machine's IP address so that it stays between reboots - that is by using the /etc/sysconfig/network-scripts/ifcfg-eth0 method.

    This does not need me to put any startup entries anywhere.

    ReplyDelete