How to Configure Network Settings for a Virtual Machine?

Virtual Machines (VMs) have become an integral part of modern IT infrastructure. They allow you to run multiple operating systems on a single physical server, saving resources and simplifying management. One of the key aspects of working with virtual machines is configuring their network settings. The availability of VMs, their interaction with other systems, and data security depend on the correct network configuration. In this article, we will thoroughly examine various ways to configure network parameters for virtual machines, providing you with practical examples and recommendations.

Table of Contents

Basic Concepts of VM Networking

How to Configure Network Settings for a Virtual Machine? - Diagram illustrating the different networking modes (Bridged, NAT, Host-only) and their connectivity patterns.
Before proceeding with configuring the network settings for virtual machines, it’s important to understand the basic concepts and modes of VM networking. The main operating modes include bridged networking, NAT (Network Address Translation), and host-only networking. Each mode has its own characteristics and is designed for various use cases. Choosing the right mode depends on the availability requirements of the VM, its interaction with other systems, and security requirements.

Bridged Networking In bridged networking mode, the virtual machine receives its own IP address on the same network as the host machine. This means that the VM is directly connected to the physical network and can interact with other devices on the network as if it were a separate physical computer. This mode is suitable for cases where the VM needs to be accessible from the outside and have full access to the network.

Example: Imagine you have a physical server with the IP address 192.168.1.100. You configure a virtual machine in «bridged» mode. The VM gets the IP address 192.168.1.101. Now the VM can directly communicate with any other device on the 192.168.1.0/24 network.
VPS хостинг

Виртуальные серверы с гарантированными ресурсами

Выбрать VPS

NAT (Network Address Translation) In NAT mode, the virtual machine uses the IP address of the host machine to access the network. The host machine performs network address translation (NAT), hiding the internal IP address of the VM. This provides the VM with internet access but restricts direct access to the VM from the outside. This mode is suitable for cases where the VM requires internet access but does not require direct access from the external network.

Example: The host machine has the IP address 192.168.1.100 and is connected to the Internet. The virtual machine is configured in NAT mode and gets an internal IP address, for example, 10.0.2.15. When the VM accesses a website on the Internet, the host machine performs NAT, replacing the IP address 10.0.2.15 with 192.168.1.100. The response from the website returns to the host machine, which then redirects it to the VM.

Expert Tip: NAT mode provides additional security, as it hides the internal IP address of the VM from the external network. This can be useful for protecting the VM from attacks.

Host-Only Networking In «host-only» mode, the virtual machine can only interact with the host machine and other VMs connected to the same «host-only» network. This mode creates an isolated network that is inaccessible from the outside. It is suitable for cases where you need to create an isolated environment for testing or development.

Example: You create a virtual machine and configure it in «host-only» mode. The host machine is also connected to this «host-only» network. Now the VM and the host machine can communicate with each other but cannot access the Internet or other devices on the physical network.

Comparison Table of VM Network Modes:

ModeAccessibility from OutsideInternet AccessIsolationApplication
BridgedYesYesNoWhen the VM should be accessible from the outside and have full access to the network.
NATNo (via port forwarding)YesPartialWhen the VM requires internet access, but does not require direct access from the external network.
Host-OnlyNoNoFullWhen you need to create an isolated environment for testing or development.

Configuring VM Networking via the Graphical Interface

How to Configure Network Settings for a Virtual Machine? - Screenshots showing the network settings configuration in VMware Workstation or VirtualBox.
Most virtualization platforms, such as VMware Workstation, VirtualBox, and Hyper-V, provide a graphical interface for configuring the network settings of virtual machines. This method is the easiest and most convenient for beginners.

Configuring Networking in VMware Workstation To configure networking in VMware Workstation, follow these steps:

  • Select the virtual machine from the list.
  • Click «Edit virtual machine settings».
  • Go to the «Network Adapter» tab.
  • Select the desired network mode: «Bridged», «NAT», or «Host-only».
  • If necessary, configure additional parameters, such as the MAC address or VLAN.
Example: You want the virtual machine to be accessible from the outside and have its own IP address on the network. You select «Bridged» mode and specify to which network adapter of the host machine the VM should be connected.

Configuring Networking in VirtualBox Configuring networking in VirtualBox is similar to VMware Workstation:

  • Select the virtual machine from the list.
  • Click «Settings».
  • Go to the «Network» tab.
  • Select the adapter (Adapter 1, Adapter 2, etc.).
  • In the «Attached to» field, select the desired network mode: «Bridged Adapter», «NAT», «Host-only Adapter».
  • If necessary, configure additional parameters, such as the MAC address or adapter type.
Example: You want the virtual machine to have internet access but not be directly accessible from the outside. You select «NAT» mode. VirtualBox will automatically configure network address translation for the VM.

Configuring a Static IP Address in the Guest OS After selecting the network mode in the virtual machine settings, it’s often necessary to configure the IP address, subnet mask, gateway, and DNS server in the guest operating system itself. This can be done through the graphical interface or through the command line (see the next section).

Example for Linux (Ubuntu):

  • Open «Settings» -> «Network».
  • Select the connection (e.g., «Wired»).
  • Click on the gear icon.
  • Go to the «IPv4» tab.
  • Select «Manual» in the «Method» field.
  • Enter the IP address, subnet mask, gateway, and DNS servers.
  • Click «Apply».
Example for Windows:

  • Open «Control Panel» -> «Network and Internet» -> «Network and Sharing Center».
  • Click on the connection name (e.g., «Ethernet»).
  • Click «Properties».
  • Select «Internet Protocol Version 4 (TCP/IPv4)» and click «Properties».
  • Select «Use the following IP address» and enter the IP address, subnet mask, gateway, and DNS servers.
  • Click «OK».
Attention: Make sure that the IP address you assign to the virtual machine does not conflict with the IP addresses of other devices on the network.

Configuring VM Networking via the Command Line

Configuring the network of a virtual machine via the command line provides a more flexible and powerful way to manage network settings. This method is especially useful for automation, working with servers without a graphical interface, or when using scripts. Configuring the network via the command line depends on the operating system installed on the virtual machine.

Configuring Networking in Linux In most Linux distributions, the utilities ifconfig, ip, route and configuration files located in the directory /etc/network/interfaces (Debian/Ubuntu) or /etc/sysconfig/network-scripts/ (CentOS/RHEL) are used to configure the network.

Example (Debian/Ubuntu):

To configure a static IP address for the eth0 interface, edit the file /etc/network/interfaces:

# /etc/network/interfaces
auto eth0
iface eth0 inet static
    address 192.168.1.101
    netmask 255.255.255.0
    gateway 192.168.1.1
    dns-nameservers 8.8.8.8 8.8.4.4
Then restart the network interface:

sudo ifdown eth0 && sudo ifup eth0
Example (CentOS/RHEL):

To configure a static IP address for the eth0 interface, edit the file /etc/sysconfig/network-scripts/ifcfg-eth0:

# /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.1.101
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4
Then restart the network service:

sudo systemctl restart network
Using the ip utility:

The ip utility is a more modern alternative to ifconfig and provides more options for configuring the network.

Example: Assigning an IP address and subnet mask:

sudo ip addr add 192.168.1.101/24 dev eth0
Adding a gateway:

sudo ip route add default via 192.168.1.1
Attention: Changes made with the ip utility are not saved after the system reboots. To make the changes permanent, you need to edit the network configuration files.

Configuring Networking in Windows In Windows, the netsh utility is used to configure the network via the command line.

Example:

To configure a static IP address for the «Ethernet» interface, run the following commands in the command prompt with administrator privileges:

netsh interface ipv4 set address name="Ethernet" static 192.168.1.101 255.255.255.0 192.168.1.1
netsh interface ipv4 set dns name="Ethernet" static 8.8.8.8 primary
netsh interface ipv4 add dns name="Ethernet" 8.8.4.4 index=2
These commands set the IP address, subnet mask, gateway, and DNS servers for the «Ethernet» interface.

Automating Network Configuration with Scripts:

Configuring the network via the command line allows you to automate the process with scripts. This is especially useful when deploying a large number of virtual machines. For example, you can create a script that automatically configures the IP address, subnet mask, gateway, and DNS servers based on specified parameters.

Example (bash script for Linux):

#!/bin/bash

INTERFACE="eth0"
IP_ADDRESS="192.168.1.101"
NETMASK="255.255.255.0"
GATEWAY="192.168.1.1"
DNS1="8.8.8.8"
DNS2="8.8.4.4"

echo "Configuring network for interface $INTERFACE"

sudo ip addr add $IP_ADDRESS/$NETMASK dev $INTERFACE
sudo ip route add default via $GATEWAY

echo "Adding DNS servers to /etc/resolv.conf"
echo "nameserver $DNS1" | sudo tee /etc/resolv.conf
echo "nameserver $DNS2" | sudo tee -a /etc/resolv.conf

echo "Network configuration completed"

Troubleshooting Common VM Network Issues

When working with virtual machines, network problems often arise. Some of the most common issues include a lack of internet access, the inability to connect to the VM from the outside, or the inability of the VM and host machine to interact. In this section, we will look at the most common problems and how to solve them.

Lack of Internet Access If the virtual machine does not have internet access, check the following:

  • Network Mode: Make sure that «Bridged» or «NAT» mode is selected for the VM. If «Host-only» mode is selected, the VM will not have internet access.
  • IP address, subnet mask, gateway, and DNS server settings: Check that these parameters are configured correctly in the guest operating system. Make sure that the gateway points to the IP address of the router providing internet access and that the DNS servers are set to working DNS servers (e.g., 8.8.8.8 and 8.8.4.4).
  • DNS issues: Try pinging the IP address of a website (e.g., 8.8.8.8). If the ping is successful, the problem is most likely related to DNS. Check the DNS settings and make sure they are correct.
  • Firewall: Check that the firewall on the host machine or in the guest operating system is not blocking internet access for the VM.
Example: You have configured the VM in NAT mode, but it does not have internet access. You check the IP address, subnet mask, and gateway settings and find that the gateway is specified incorrectly. You correct the gateway, and the VM gets internet access.

Inability to Connect to the VM from the Outside If you cannot connect to the VM from the outside, check the following:

  • Network Mode: If «NAT» mode is selected for the VM, you need to configure port forwarding on the host machine.
  • Port Forwarding: Configure port forwarding on the host machine so that traffic arriving at a specific port on the host machine is forwarded to the corresponding port on the VM.
  • Firewall: Check that the firewall on the host machine or in the guest operating system is not blocking access to the ports to which forwarding is configured.
Example: You want to connect to an SSH server running on a VM in NAT mode. You configure port forwarding port 22 on the host machine to port 22 on the VM. Now you can connect to the SSH server on the VM by connecting to port 22 on the host machine.

Quote: «Properly configuring port forwarding is key to accessing services running on virtual machines in NAT mode.» — John Doe, Senior Network Engineer

Inability of the VM and Host Machine to Interact If the VM and the host machine cannot interact with each other, check the following:

  • Network Mode: If «Host-only» mode is selected for the VM, make sure that the host machine is connected to the same «host-only» network.
  • IP Addresses: Make sure that the IP addresses of the VM and the host machine are on the same subnet.
  • Firewall: Check that the firewall on the host machine or in the guest operating system is not blocking traffic between the VM and the host machine.
Example: You have configured the VM in «Host-only» mode, but you cannot access the web server running on the VM from the host machine. You check the IP addresses and find that the IP address of the host machine is not on the same subnet as the IP address of the VM. You change the IP address of the host machine, and you can access the web server on the VM.