21

It confuses me about NAT networking, but I can use VMware and Parallels Desktop to do that.

I knew NAT how it works, just need an explain about this situation.

The following is from official documents. Why HOST can't connect GUEST with NAT. enter image description here

6
  • 1
    Are you talking about VirtualBox or VMWare? They are completely different softwares.
    – user766703
    Jan 5, 2018 at 7:08
  • 1
    Why need NAT between VM and host ? It's not logical.
    – Overmind
    Jan 5, 2018 at 7:17
  • 1
    @ThePirateBay In my opinion, these virtual platforms should have the same behavior with NAT mode. Does it depend on different softwares?
    – TonyY
    Jan 5, 2018 at 7:33
  • 1
    @Overmind I just curious about I always use default mode(NAT) at VMware and Parallels, and I can use HOST to connect GUEST. When I change to use Virtualbox, it didn't work, then I must change to Bridged or Host-only mode.
    – TonyY
    Jan 5, 2018 at 7:46
  • 1
    "Does it depend on different softwares?" - I can't tell, as the VMWare is not open-source, but in general, yes, software can change NAT behavior. Therefore, the software you're using is an important information.
    – user766703
    Jan 5, 2018 at 7:48

5 Answers 5

26

If you want to connect to the guest machine from the host, using NAT Network:

First, create a NAT Network - create the network in the VirtualBox Manager, File -> Preferences -> Network, and give it a name.

Assign that named NAT Network to each of the guest machines in their Network configurations in VirtualBox.

Make a note of what IP address is assigned to your guest machines when you run them. It will be something like 10.0.2.15 or 10.0.2.4. In Posix systems, run "ifconfig" and see what ip addresses show up for em0. Make a record of which address goes to which guest machine.

Edit the NAT Network's port forwarding. In VirtualBox manager, go to File -> Preferences -> Network and click the edit button for the NAT Network you are using. Click on Port Forwarding. Add rules that forward ports on your host machine to ip address + port on the guests (You don't need to set the IP address for your host machine, that's optional).

4
  • 5
    after you add that rule you need to restart VirtualBox (and thus your VM) - virtualbox should then be listening on port 2222 and you connect to your guest via your hosts IP, not the guest IP e.g. 192.168.1.1:2222 (not 10.0.2.4:2222 ! )
    – wal
    Jun 22, 2019 at 12:01
  • see also this image of the port forwarding (which was rejected as an edit to this answer by the nasty powers-that-be)
    – wal
    Jun 22, 2019 at 13:55
  • This worked for me using Visual Studio remote debugger within the VM. I didn't have to restart the VM. Oct 13, 2023 at 14:55
  • I would recommend setting the host IP to "127.0.0.1". Otherwise your host is listening on 0.0.0.0, which (barring firewalls) would allow other hosts on the host's network to connect to the VM, which might not be what you want.
    – dovetalk
    Jan 17 at 22:38
16

I thought about it for a bit and realized an ideal solution that is easy to implement.

Add a second interface!!

One is the NAT interface for the VM to talk to the Internet. The other is a host-only interface. In my case, I'm installing a CentOS guest OS. The new interface shows up, both get their IPs on the respective networks. The host-only doesn't establish a default route, so no conflicts. It just works. I access the guest OS openly from the host using the IP on the host-only network that is defined within VirtualBox.

6
  • I confirm this works on VirtualBox 5.2.20 with a CentOS 6 and Windows 7 VM (testing old IEs). As long as the adapters are on the same named "Host-only Adapter", it should just work.
    – spyle
    Jan 9, 2020 at 21:20
  • No luck for me with VirtualBox for MacOS and Ubuntu Bionic guest. The guest is given an IP address via DHCP in the range 192.168.56.0/24, but there is no way to reach this network from the host. There is no way to edit host-only networks in the preferences pane. Jan 14, 2020 at 21:23
  • Thanks for the answer. It is the simplest solution of all Jul 9, 2020 at 13:01
  • 1
    Best answer! Others don't work. Jan 15, 2021 at 12:44
  • Simplest operable answer. Adding forwarded ports also consumes the host's ports. Mar 22, 2021 at 13:28
11

To connect from HOST to GUEST VM with NAT networking, just add a port forward.

After that, you can access the forwarded port like a local port:

ssh root@localhost

enter image description here

1
  • I would recommend setting the host IP to "127.0.0.1". Otherwise your host is listening on 0.0.0.0, which (barring firewalls) would allow other hosts on the host's network to connect to the VM, which might not be what you want.
    – dovetalk
    Jan 17 at 22:38
8

It seems the official documentation has been improved to make it clearer of which direction the communication occurs, as I discovered from this answer:

Overview of networking modes

0

Was bitten by this 2 years ago. For the sake of simplicity i wanted to have all my guests on the same subnet as the VirtualBox interface of the host, to SSH or VNC into any of them. And also have them see eachother, and access Internet (client-only).

As i have about 10 guest VMs for development, on a laptop host that frequently changes from a network to another, i couldn't use bridging (wanted to use fixed-IPs for all the guests for easy access from host).

NAT network was nearly perfect, except no communication between guests and host.

Host-only network was nearly perfect, except no internet access for the guests.

I eventually managed to get a working configuration with one minimal Debian VM acting as DNSmasq with two interfaces, and a bridge of those two interfaces. Well, until last week i just toggled off the virtualbox network interface because of conflict with another software, and when re-enabling it, host to guest access wouldn't work at all anymore.

After 2 hours managed to get back either internet access to guests, or guests access between them and not to the host.

Might maybe try as a last resort Host-Only for all guests, and the gateway VM doing NAT, albeit it'd be very ugly to have both two NATs daisy-chained, and the guests having different addresses between them and from the host.

You must log in to answer this question.

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