Sunday, July 30, 2017

When virtual machine on virtualbox can't connect to internet

I am using CentOS 6.9 and Oracle Virtualbox in this article. I will write what you can do when virtual machine on virtualbox can't connect to internet.

Point to be checked 1
"Whether you selected Connect automatically during the set up of the virtual machine"


When you configured your virtual machine at the beginning, you should have seen "Configure Network".


Make sure "Connect automatically" is checked and click "Apply".


Point to be checked 2
"Whether the cable is connected to the virtual machine"


Open the VM Virtualbox manager and right-click on your virtual machine, and click "Setting".


Make sure your cable is connected. And also make sure your physical cable is connected to the host machine.


Point to be checked 3
"Whether your adapter is attached to Bridged adapter or NAT"


Open the VM Virtualbox manager and right-click on your virtual machine, and click "Setting".


Make sure your adapter is attached to "Bridged adapter" or "NAT".


If 1, 2 and 3 are checked to be ok, now check the following.


Point to be checked 4


Start your virtual machine and try this command:
$ sudo service network restart

If your configuration is properly set up, the following should appear:
Shutting down interface eth0:                            [OK]
Shutting down loopback interface:                    [OK]
Bringing up loopback interface:                        [OK]
Bringing up interface eth0:                                [OK]
Determining IP information for eth0... done.    [OK]

Also try this command:
$ sudo ifconfig -a

If this is displayed, you should be able to connect to internet.

If it fails determining IP and "eth0" is not displayed by ifconfig command, maybe wrong MAC address is registered in your virtual machine.

This is the MAC address used for eth0.

Correct MAC address can be checked from the following place. You can see the same MAC address is shown on this setting and the result of ifconfig command.

If MAC address is wrong (or probably wrong), change it from here:
$ sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0
and
$ sudo vi  /etc/udev/rules.d/70-persistent-net.rules

For 70-persistent-net.rules, ATTR{address} is the MAC address. Change it to correct value.
If you are using eth0 and never used eth1 but mysteriously eth1 is added in this file for some reason, change the name of eth1 to eth0 (eth1 is the new information and we want to use this as eth0) and delete the old eth0 one. (This often occurs after cloning your virtual machine. When new MAC address is detected to be being used, udev automatically change the information as eth1, which is actually unnecessary.)

By the way, 70-persistent-net.rules is automatically created even if you delete the file. If you mess up the file and have no idea how to fix the file, you can delete it and restart the virtual machine to re-create the file. Then change the setting written in the file again (if wrong information is written there).

(To delete the file and restart the virtual machine, run the following commands. These are unnecessary if you don't want to re-create 70-persistent-net.rules file.)
$ sudo rm  /etc/udev/rules.d/70-persistent-net.rules
$ sudo shutdown -r now



Firewall 


Check your firewall if it is unnecessarily blocking your virtual machine's network connection.
(Maybe disable your firewall for a while and try to connect to the internet again on the virtual machine. If you can connect, then it is because of the firewall. Turn on the firewall again and properly configure the firewall to allow your virtual machine's network connection.)