My host machine OS is Windows 10, and I install oracle VirtualBox on it. The guest OS in the VirtualBox virtual machine is Ubuntu 18.04.1. I use the Ubuntu OS normally for example code development. But one day when I start the Ubuntu virtual machine, I find the network does not work correctly.
I can not access the network, the error message is ubuntu connection failed, activation of network connection failed, I can not ping the domain name, when I ping a domain name (for example www.google.com), it will return the error message Name or service not known. And I can not ping the IP address of the domain even, the error message is network is unreachable. I struggled with all these errors for almost several hours, and I resolve this problem by following the below steps finally.
1. Enable Network Connection To Fix Activation Of Network Connection Failed.
- If you encounter a network issue in Linux OS such as Ubuntu, the first thing you need to do is to check whether your network configuration is correct or not.
- Open a terminal and run the below command.
~$ ifconfig enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.6 netmask 255.255.255.0 broadcast 192.168.1.255
- If you can not find the inet address in the console output, this means the network is not enabled correctly. You need to follow the below steps to enable the network first.
- Click Show Applications —> Settings icon to open the Ubuntu configuration panel.
- Click the Network menu item in the left panel. Then toggle the Wired button to ON in the right panel.
- Now run
$ifconfigcommand in terminal again, you should find the IP address that the DHCP server assigned to you.
2. Fix Name Or Service Not Known Issue.
- After configurations in step 1, you should get your virtual machine IP address, and you can also ping other IP addresses in your local network or internet website.
- But you may find you can not ping the domain name. When you ping a domain name you get an error message Name or service not known like below.
~$ ping www.google.com ping: www.google.com: Name or service not known
- When you turn on / off the wired network in step 1. Below DNS record will be written to /etc/resolve.conf, and the DNS record is invalid.
# Generated by NetworkManager nameserver 127.0.0.53
- This leads to the error Name or service not know when you ping the website domain in the terminal. And this DNS record is generated by the NetworkManager automatically.
- To resolve this problem, just run
$ sudo gedit /etc/resolve.confto change the nameserver IP to a valid one such as 126.96.36.199 ( google provide ). Then save the resolve.conf file.
- Now you can ping the website domain successfully.
3. Fix Invalid DNS Record nameserver 127.0.0.53 Permanently.
- After step 1 and step 2, you can ping the website domain name now.
- To avoid this error happening again, you can remove the invalid DNS record 127.0.0.53 and add a valid DNS record permanently in /etc/resolve.conf when you reboot the OS again.
- Run the below command to install Ubuntu resolvconf package.
$ sudo apt install resolvconf
- Modify file /etc/resolvconf/resolv.conf.d/tail and add a valid DNS record ( such as nameserver 188.8.131.52 ) in it. If the tail file does not exist then create it.
$ sudo gedit /etc/resolvconf/resolv.conf.d/tail
- Now when you boot the Ubuntu OS, the DNS record ( nameserver 184.108.40.206) in the tail file will be added at the end of file /run/resolvconf/resolv.conf, and /etc/resolv.conf will be a symbol link to this file.
4. How To Fix VirtualBox Virtual Machine connect: Network is unreachable” Error.
- I use the Oracle VirtualBox to create a virtual machine.
- My host os is Windows7, and the guest os is Ubuntu 18.
- I open the VM settings dialog, click the Network tab and select NAT in the Enable Network Adapter Attached to the drop-down list.
- But when I ping an IP address that exists on the internet, it shows the error message connect: Network is unreachable.
- Below is the output when I run the command ifconfig on the guest Ubuntu OS.
$ ifconfig enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet6 fe80::7bb4:e4e0:dc7d:16a9 prefixlen 64 scopeid 0x20<link> ether 08:00:27:c6:c0:0d txqueuelen 1000 (Ethernet) RX packets 32987 bytes 45350863 (45.3 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 11336 bytes 856368 (856.3 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 196 bytes 17046 (17.0 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 196 bytes 17046 (17.0 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
- You can get the reason from the output text of the ifconfig command.
- We can see that there is not the inet info in the ifconfig output, which means the enp0s3 interface does not have an IP address.
- So you can run the command sudo ifconfig enp0s3 <an_ip_address> to assign a static IP address to the interface enp0s3.
- If you want the enp0s3 interface to get a dynamic IP address from a DHCP server, you can run the command sudo dhclient enp0s3.