How to connect virtual machine to internet connection in VMware/VirtualBox


Written By - admin
Advertisement

There are various types of Network Mode available with Oracle VirtualBox and VMware Workstation Player. These two are the most used Virtualization Software by end users for performing R&D and testing.

Before we jump into out main topic on how to connect virtual machine to internet connection, you must have Oracle VirtualBox installed on your environment Server and also you must be familiar with the different types of Network Modes and which one would help us connect to internet using our Linux or Windows OS from the Virtual Machine.

I have written another article with detailed explanation of different network mode available with Oracle VirtualBox and VMware Workstation Player. So in this article I will be very brief and touching only those areas to cover our primary agenda to enable and connect virtual machine to internet connection.

 

Below are the Network Modes using which you must use to enable and connect virtual machine to internet connection

  1. Bridged Network
  2. NAT (Network Address Translation)

 

How Bridged Network Works?

With bridged networking, Oracle VM VirtualBox uses a device driver on your host system that filters data from your physical network adapter. This driver is therefore called a net filter driver. This enables Oracle VM VirtualBox to intercept data from the physical network and inject data into it, effectively creating a new network interface in software.

When a guest is using such a new software interface, it looks to the host system as though the guest were physically connected to the interface using a network cable. The host can send data to the guest through that interface and receive data from it. This means that you can set up routing or bridging between the guest and the rest of your network.

 

Advertisement

How NAT Networking works?

Network Address Translation (NAT) is the simplest way of accessing an external network from a virtual machine. Usually, it does not require any configuration on the host network and guest system.

A virtual machine with NAT enabled acts much like a real computer that connects to the Internet through a router. The router, in this case, is the Oracle VM VirtualBox networking engine, which maps traffic from and to the virtual machine transparently.

 

How to select/change Network Mode in Oracle Virtual Box?

  • Assuming you already have a running virtual machine with you, select the virtual machine.
  • Click on Machine from the top panel menu of Oracle Virtual Box and select Settings
  • Next select Network from the left panel menu
  • Choose the Adapter using which you wish to connect virtual machine to internet connection
  • Next from the drop down menu select either Bridged Adapter or NAT
  • Click on Ok and save the settings.

How to connect virtual machine to internet connection in VMware/VirtualBox

 

How to select/change Network Mode in VMware Workstation Player?

  • Again assuming that you already have a running virtual machine
  • Click on Player on the top menu, from the drop down menu select Manage and click on Virtual Machine Settings. Alternatively you can also press "Ctrl + D" to open the virtual machine settings
  • Choose the Network Adapter using which you wish to connect virtual machine to internet connection
  • Next select the Network Mode as NAT or Bridged
  • Click on Ok and save the settings.

How to connect virtual machine to internet connection in VMware/VirtualBox

NOTE:

If you are yet to create virtual machine then during the initial setup process you can select Bridged or NAT as the Network Mode for the Virtual network Adapter.

 

Connect Virtual Machine to Internet Connection using NAT

I would assume that you have selected NAT as the Network mode for your VM's Adapter already.
For NAT it is important that you leave your network settings to use DHCP. There are no manual changes required from the end user here for default settings.

On RHEL/CentOS environment verify your network configuration. Verify the BOOTPROTO directive of your interface configuration file

# egrep BOOTPROTO /etc/sysconfig/network-scripts/ifcfg-Ethernet_connection_1
BOOTPROTO=dhcp

Also you can verify the same using nmcli, to list the available network connections

# nmcli con show
NAME                   UUID                                  TYPE      DEVICE
Ethernet connection 1  30cb1359-cb34-4dae-b994-f4b3b39542d7  ethernet  enp0s3

Here we have a single connection "Ethernet connection 1", so verify the BOOTPROTO for this connection

# nmcli -p con show "Ethernet connection 1" | grep -i "ipv4.method"
ipv4.method:                            auto

As you see our IPv4 connection is set to auto, for a static ip this will be set to manual

My /etc/resolv.conf file

# cat /etc/resolv.conf
# Generated by NetworkManager
search golinuxcloud.com
nameserver 192.168.43.1

 

On Oracle VirtualBox

IMPORTANT NOTE:

There is a difference how NAT works in VirtualBox and VMware. In Oracle VirtualBox NAT is much like a private network behind a router, the virtual machine is invisible and unreachable from the outside internet. You cannot run a server this way unless you set up port forwarding. But in VMware Workstation Player you can directly connect to your virtual machine using NAT IP Address.

List the available interface and identify the interface for which you have configured NAT as Network Mode in the Settings

# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:d5:cb:b6 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic noprefixroute enp0s3
       valid_lft 86385sec preferred_lft 86385sec

In NAT mode, the guest network interface is assigned to the IPv4 range 10.0.x.0/24 by default where x corresponds to the instance of the NAT interface +2. So x is 2 when there is only one NAT instance active. In that case the guest is assigned to the address 10.0.2.15, the gateway is set to 10.0.2.2 and the name server can be found at 10.0.2.3.

 

You can change the default NAT network for individual virtual machine using below command on your Windows Machine under the home directory of VirtualBox

C:Program FilesOracleVirtualBox>VBoxManage modifyvm "VM name" --natnet1 "192.168.100.0/24"

This command would reserve the network addresses from 192.168.100.0 to 192.168.100.254 for the first NAT network instance of "VM name". The guest IP would be assigned to 192.168.100.15 and the default gateway could be found at 192.168.100.2.

# ip addr show dev enp0s3
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:d5:cb:b6 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.15/24 brd 192.168.100.255 scope global dynamic noprefixroute enp0s3
       valid_lft 86138sec preferred_lft 86138sec
    inet6 fe80::a00:27ff:fed5:cbb6/64 scope link
       valid_lft forever preferred_lft forever

To remove your custom changes for NAT Network

C:Program FilesOracleVirtualBox>VBoxManage modifyvm "VM name" --natnet1  default

Now try to connect virtual machine to internet connection

# ping google.com
PING google.com (216.58.196.174) 56(84) bytes of data.
64 bytes from maa03s31-in-f14.1e100.net (216.58.196.174): icmp_seq=1 ttl=52 time=37.8 ms
64 bytes from maa03s31-in-f14.1e100.net (216.58.196.174): icmp_seq=2 ttl=52 time=35.0 ms
^C
--- google.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 3ms
rtt min/avg/max/mdev = 35.036/36.428/37.821/1.405 ms
NOTE:

In some cases if you are unable to connect virtual machine to internet connection using NAT then try switching your adapter type. Some times the Adapter Type can be the cause of the problem. In my case for VirtualBox I have to use virtio-net as the Adapter Type

 

How to change Adapter Type in VirtualBox?

  • To change Adapter Type you must power off the respective VM.
  • Next open the Settings of the VM.
  • Navigate to Network and select the Adapter for which you wish to change Adapter Type
  • From the drop down menu of Adapter Type select the preferred type. For me I have used Paravirtualized Network (virtio-net) as IntelPRO/1000 T Server(82543GC) was unable to connect to internet connection.

 

On VMware Workstation Player

Here my VM on VMware Player is on DHCP and is configured as NAT in the VM Settings and I am able to connect to the Virtual Machine from my Windows host laptop.

# ip addr show dev ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:23:13:e0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.118.128/24 brd 192.168.118.255 scope global dynamic noprefixroute ens33
       valid_lft 1472sec preferred_lft 1472sec
    inet6 fe80::20c:29ff:fe23:13e0/64 scope link
       valid_lft forever preferred_lft forever

My /etc/resolv.conf which gets automatically populated by NetworkManager

# cat /etc/resolv.conf
# Generated by NetworkManager
search localdomain golinuxcloud.com
nameserver 192.168.118.2

My default gateway

# ip route show
default via 192.168.118.2 dev ens33 proto dhcp metric 100

Check if you are able to connect virtual machine to internet connection

# ping google.com
PING google.com (172.217.26.206) 56(84) bytes of data.
64 bytes from maa03s23-in-f206.1e100.net (172.217.26.206): icmp_seq=1 ttl=128 time=38.9 ms
64 bytes from maa03s23-in-f206.1e100.net (172.217.26.206): icmp_seq=2 ttl=128 time=45.6 ms
^C
--- google.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 3ms
rtt min/avg/max/mdev = 38.940/42.278/45.616/3.338 ms

 

Connect Virtual Machine to Internet Connection using Bridged network

For Bridged Network Mode, you can use both DHCP or static IP for your Virtual Machine. I hope you have already changed the network Mode of your Virtual Machine Adapter to Bridged to be able to connect virtual machine to internet connection.

The steps to use Bridged Network is same for both Oracle VirtualBox and VMware Workstation Player

NOTE:

To have a static IP you must be aware of the subnet, gateway and DNS details which your router is using so that you can use the same for your system's IP configuration.

We will continue to use DHCP for demonstrating this article so verify your network configuration as explained earlier in this article

My new IP Address is now

# ip addr show dev enp0s3
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:d5:cb:b6 brd ff:ff:ff:ff:ff:ff
    inet 192.168.43.38/24 brd 192.168.43.255 scope global dynamic noprefixroute enp0s3
       valid_lft 3486sec preferred_lft 3486sec
    inet6 fe80::a00:27ff:fed5:cbb6/64 scope link
       valid_lft forever preferred_lft forever
# ip route
default via 192.168.43.1 dev enp0s3 proto dhcp metric 100

As you see the IP Address has changed, now I am getting the IP Address directly from the router which should be in the same subnet as from where my laptop is getting the IP as we are using the same router.

Below is the IP Address from my laptop using ipconfig command

Wireless LAN adapter WiFi:

   Connection-specific DNS Suffix  . :
   IPv6 Address. . . . . . . . . . . : 2401:4900:273c:d7cf:979:d5f9:4b34:4958
   Temporary IPv6 Address. . . . . . : 2401:4900:273c:d7cf:6cf5:88f5:2444:dadf
   Link-local IPv6 Address . . . . . : fe80::979:d5f9:4b34:4958%2
   IPv4 Address. . . . . . . . . . . : 192.168.43.168
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : fe80::72dd:a8ff:fe0a:8b6f%2
                                       192.168.43.1

So my laptop and virtual machine, both are using 192.168.0.1 as the default gateway.

Check if you are able to connect virtual machine to internet connection

# ping google.com
PING google.com (216.58.196.174) 56(84) bytes of data.
64 bytes from maa03s31-in-f14.1e100.net (216.58.196.174): icmp_seq=1 ttl=53 time=48.3 ms
64 bytes from maa03s31-in-f14.1e100.net (216.58.196.174): icmp_seq=2 ttl=53 time=46.7 ms
^C
--- google.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 2ms
rtt min/avg/max/mdev = 46.668/47.499/48.330/0.831 ms

 

NOTE:

You must login to the console of the virtual machine using VirtualBox/VMware and restart your network interface using "systemctl restart network" to get new address if you were using NAT or any other Network Mode.

 

Lastly I hope the steps from the article to connect virtual machine to internet connection on Oracle VirtualBox and VMware Workstation Player using Linux was helpful. So, let me know your suggestions and feedback using the comment section.

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

4 thoughts on “How to connect virtual machine to internet connection in VMware/VirtualBox”

Leave a Comment