DEV Community

Naval Kishor Upadhyay
Naval Kishor Upadhyay

Posted on

VM Networking Demystified: NAT, Bridged, Host-Only, Port Forwarding, and Multi-VM Private Networks

When you spin up a virtual machine (VM) in VirtualBox, VMware, or Vagrant, one of the first decisions you face is: How should this VM connect to the network?

Do you want it to access the internet? Should your host computer see it? Should two VMs talk to each other? Should others on your LAN be able to reach it like a normal PC?

This is where VM networking modes — NAT, NAT Network, Bridged, and Host-Only — come into play. And when you mix in port forwarding and multi-VM setups, things can get confusing fast.

This article will cut through that confusion and explain, in simple but thorough terms, how each mode works, when to use it, and how to combine them.


1. Why VM Networking Matters

A virtual machine is like a computer inside your computer. But unlike physical computers, it doesn’t automatically have a network connection.

Depending on your needs, you might want your VM to:

  • Download software updates from the internet.
  • Act like a server that’s reachable from other devices on your LAN.
  • Stay isolated, but still be accessible from your host machine.
  • Communicate with other VMs in a private lab environment.

The right networking mode ensures you get exactly what you need — no more, no less.


2. The Core Networking Modes Explained

Let’s go through the four main networking options you’ll encounter in most virtualization tools.


2.1 NAT (Network Address Translation)

How it works:

  • The VM “hides” behind your host’s IP address.
  • From the outside world, it looks like the traffic is coming from the host.

Capabilities:

  • Internet Access: ✅ Yes (via host’s connection).
  • Inbound Connections: ❌ No (blocked unless you configure port forwarding).
  • VM ↔ VM Communication: ❌ No (each VM gets isolated behind NAT).

Best Use Case:

Quick internet access for downloading packages or running updates, when you don’t need external access to the VM.


2.2 NAT Network

How it works:

  • Similar to NAT, but all VMs connected to the same NAT network share a private subnet.
  • They can talk to each other while still having internet access.

Capabilities:

  • Internet Access: ✅ Yes.
  • Inbound Connections: ❌ No (unless port forwarding).
  • VM ↔ VM Communication: ✅ Yes (within the NAT network).

Best Use Case:

When you’re simulating multi-VM environments (like app server + database) that still need to access the internet.


2.3 Bridged Adapter

How it works:

  • The VM connects directly to your physical network, just like another laptop or phone.
  • Your router gives it its own IP address.

Capabilities:

  • Internet Access: ✅ Yes.
  • Inbound Connections: ✅ Yes (VM has its own IP on LAN).
  • VM ↔ VM Communication: ✅ Yes (they’re all peers on the LAN).

Best Use Case:

When you want your VM to act like a real physical machine on your network — for example, testing a web server accessible to other devices.


2.4 Host-Only Network

How it works:

  • Creates a completely private network between the host and VMs.
  • No connection to the outside world.

Capabilities:

  • Internet Access: ❌ No (unless paired with another adapter).
  • Inbound Connections: ✅ Yes, from host.
  • VM ↔ VM Communication: ✅ Yes (inside the private network).

Best Use Case:

For isolated test labs where you don’t want VMs exposed to the internet. Perfect for experimenting without risk.

Diagram:


3. Port Forwarding: Accessing VMs Without Public IPs

With NAT and NAT Network, your VM doesn’t have a public-facing IP. That means you can’t just ssh user@vm_ip from your host or another computer.

This is where port forwarding comes in. It’s like a tunnel through the host machine:

  1. Set up forwarding: Map a host port (e.g., 2222) to the VM’s port (e.g., 22 for SSH).
  2. Client connects: From your laptop, run ssh -p 2222 user@host_ip.
  3. Host forwards: The host takes traffic from port 2222 and forwards it to the VM’s internal IP on port 22.
  4. VM responds: You’re inside the VM as if it had a public IP.

Diagram:

Why it matters:

  • Lets you connect securely to VMs without exposing them directly to your network.
  • Essential for NAT and NAT Network setups.

4. Connecting Multiple VMs Together

Sometimes, one VM isn’t enough. Maybe you want a web server talking to a database server, or a load balancer managing multiple app servers.

Here’s how you can do it:

  • NAT Network → Best option if you want VMs to communicate and reach the internet.
  • Host-Only Network → Good for completely isolated labs, where internet access is not required.
  • Bridged Network → Works if you want your VMs to behave like independent physical devices on your LAN.

Example Scenario:

  • VM1 = Web Server
  • VM2 = Database Server
  • Both attached to a NAT Network.
  • VM1 can query VM2, VM2 can respond, and both can still download updates from the internet.

5. Quick Comparison Table

Here’s the cheat sheet you’ll want to bookmark:

Mode Internet Access VM ↔ VM VM ↔ Host Reachable from LAN Best Use Case
NAT ✅ Yes ❌ No ⚠️ Only via Port Forwarding ❌ No Single VM with safe internet
NAT Network ✅ Yes ✅ Yes ✅ Yes (with setup) ❌ No Multi-VM labs + internet
Bridged ✅ Yes ✅ Yes ✅ Yes ✅ Yes Realistic server testing
Host-Only ❌ No ✅ Yes ✅ Yes ❌ No Isolated test setups

6. Wrapping It All Up

  • NAT → Great for single VMs needing internet.
  • NAT Network → Perfect for multi-VM setups with internet.
  • Bridged → Best when your VM should act like a real device on your LAN.
  • Host-Only → Ideal for isolated environments.
  • Port Forwarding → The secret sauce to access NAT’d VMs.
  • Private Networks → Enable complex multi-VM topologies without exposing them.

Once you understand these modes, you can build powerful virtual labs on your laptop that mimic real-world networks — whether you’re learning DevOps, testing servers, or simulating production environments.

Top comments (0)