66

I am having trouble connecting my CentOS 7 guest to the internet. My VirtualBox Network configuration is as follows:

Adapter 1: NAT; Adapter 2: Host-Only (vboxnet0)

vboxnet0 is connected set up with DHCP with default VirtualBox parameters.

In my CentOS Guest, I commanded ip a and got the following:

enp0s3: 10.0.2.15/24 (NAT adapter)
enp0s8: 192.168.56.102/24 (Host-Only adapter)

I can connect to my VirtualBox from my host machine:

ping 192.168.56.102

can transmit and receive packets.

However, my VirtualBox cannot access the internet:

ping www.google.com

outputs "unknown host www.google.com"

Here is my ifcfg-enp0s3:

TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
IPV4_FAILURE_FATAL=no
IPV6_INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=enp0s3
UUID=UUID HERE (sorry I cannot copy from my guest host yet, no Guest Additions and it was too long)
DEVICE=enp0s3
ONBOOT=yes

Why can't my guest connect to the internet?

1
  • in my case ( centos9), nmcli connection up <interface name> solved the issue. // nmcli connection up enp0s3.
    – Jisson
    Mar 12 at 9:52

4 Answers 4

101

As Gasim said:

Add the following to /etc/sysconfig/network-scripts/ifcfg-enp0s3

DNS1=8.8.8.8
DNS2=8.8.4.4
# Note this was set to no
ONBOOT=yes  

then reboot the machine.

7
  • 8
    also reboot after these steps
    – arahant
    Sep 27, 2017 at 21:21
  • 9
    Thanks, this awnser solved my problems. ONBOOT=yes did it for me, I didn't need the lines DNS1 and DNS2. Mar 24, 2018 at 10:58
  • Thanks @Tim Pizey You save my day. :) Mar 15, 2019 at 19:09
  • yes, as @SeanieO'Leary said. Only ONBOOK=yes was also needed for me. May 3, 2019 at 20:25
  • the beauty of linux
    – Ed_
    May 25, 2020 at 0:51
24

Network connection is not active by default. You can check this by running the command:

nmcli d

To edit the connection run the following command:

nmtui
  1. Edit a connection
  2. Choose your Ethernet connection
  3. Check the option "Automatically connect" as shown in the pic below:

    nmtui GUI

  4. systemctl restart network

  5. sudo shutdown -r now
5
  • 3
    This was my issue with VirtualBox and CentOS7. Checking the "Automatically connect" option fixed my issue. Thanks for the clear instructions... +1
    – swill
    Sep 24, 2019 at 13:34
  • Yep! That worked like a charm. Feb 7, 2020 at 20:40
  • 1
    I am unable to check the option, any idea why? Got to this screen highlighted the checkbox and clicked 'Enter' key. Still remains unchecked
    – Thanthu
    Aug 2, 2020 at 11:24
  • @Thanthu use "Space" key to check.
    – Jing He
    Apr 18, 2021 at 17:32
  • In particular, this is the issue if you installed centos 7 using the text only install, which does not enable the network by default. Saved me time today, thank you. Oct 23, 2021 at 19:33
7

Chances are that in /etc/resolv.conf you don't have any nameservers set, in which case you want to do:

vim /etc/resolv.conf

and add:

nameserver 8.8.8.8
nameserver 8.8.4.4

save it, and once back at the shell

do:

sudo reboot now to reboot and you should have internet access

4
  • 5
    Adding it to resolv.conf didn't work because it was being auto generated by Network Manager, so I added DNS1=8.8.8.8 and DNS2=8.8.4.4 to ifcfg-enp0s3 and it worked. But the weird thing is that, resolv.conf adds my router address as the nameserver and in my Router. My router's DNS are already Google DNS, so I don't know why it doesn't recognize the DNS when I set nameserver 10.0.2.1 (router's address)
    – Gasim
    May 16, 2015 at 11:53
  • That was going to be my next solution
    – td512
    May 16, 2015 at 11:54
  • Doens't help to me Jun 15, 2016 at 12:10
  • Why is this the accepted answer if it doesn't work? Nov 17, 2016 at 17:27
1

In VirtualBox for CentOS 7 VM, I faced the same issue.

You dont need to configure these files. The DNS is set via the Network Manager. On the bottom right hand corner if you see the two monitor thingy, just right click on that and connect the network adapter.

Even though you change the resolv.conf file by adding the DNS, the DNS set at your host machine/DNS set by your service provide over rides it.

And the resolv.conf gets updated as follows -

Generated by NetworkManager

search XXX#.XX.comcast.net

nameserver 75.75.76.76

nameserver 75.75.75.75

1
  • I am not using GUI. And you can change the DNS if you change the network adapter config files. That's the proper CentOS way. That's because CentOS uses a separate network manager (I think it is called Anaconda) to automatically set IP, DNS, and other network variables for network adapters.
    – Gasim
    Dec 12, 2016 at 0:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .