Search code examples
sshvirtualboxnat

Proxying a port for Virtualbox NAT is inconvenient - can't I provide the host with an IP instead?


I want to be able to SSH into a VM Guest of Virtualbox where the guests are sharing a NAT Network.

LocalNat Portforwarding (See https://www.pythian.com/blog/test-lab-using-virtualbox-nat-networking/ Set Up Portforwarding) is inconvenient vs. having an IP address on the NAT for the host.

Port forwarding requires me to keep specifying the port, e.g. in scp -P 2222 from-file localhost: and it messes with SSH keys as localhost now has two host identities, my laptop and the VM's ssh-rsa key.

Rather than port-forward, is there not a way of just adding another IP for my Virtualbox host?

Thanks, Martin.


Solution

  • You can set up a host-only network in addition to the NAT network. A host-only network is a local network which can connect to both the host and to individual VMs. The host and the VMs can communicate with each other through it.

    1. Using the virtualbox GUI, go to Virtualbox manager > File > Preferences > Network and set up a host-only network. Enable the DHCP server. You could use these settings:

      • host adapter address is 192.168.56.1
      • DHCP server address is 192.168.56.100
      • Both masks are 255.255.255.0
      • The server address range is 192.168.56.101-192.168.56.254

    This gives you the addresses from ...56.2 through ...56.99 to use as static addresses. You can manually assign them to VM interfaces if you like.

    1. After setting up this network, you should see a virtual interface on your host system with the correct IP address (the one assigned to the adapter).
    2. Now, go to network settings for the VM. Add a new network adapter. Set "attached to" to the "host-only adapter", and the name to the host-only network that you set up earlier.
    3. Start the VM. It should see the host-only adapter in addition to whatever adapters it was using before. If it's a modern operating system, it'll probably query the DHCP server and set up the interface on its own. Alternately, from inside the VM OS, you could manually assign static addresses to these interfaces.

    You can assign a host-only adapter to a VM in addition to its existing NAT adapter. In the past I've had a windows VM and an Ubuntu Linux VM set up this way. Both VMs and the host had no trouble communicating with each other as well as the Internet.