Search notes:

Oracle Virtual Box: Network / Port forwarding (guest with NAT)

A guest with a «NET» can establish TCP/IP connections but cannot simply reached from the host or outside world.
In order to access a service running under a given port number, the guest must be configured with port forwarding. If so configured, the host listens on the specified port and forwards incoming TCP/IP connection requests to the specified port in the guest.
The following example forwards port 9999 on the host to 9090 on the guest:
When the guest is started, we can verify on the host that the host is indeed listening on the port 9999:
PS C:\> netstat -an | findstr 9999
  TCP    0.0.0.0:9999           0.0.0.0:0              LISTENING

PS C:> netstat -anb | select-string 9999 -context 0,1

>   TCP    0.0.0.0:9999           0.0.0.0:0              LISTENING
   [VirtualBoxVM.exe]
The port forwarding rule can be be queried on the command line:
PS C:\> VBoxManage showvminfo $vmname | findstr /c:"NIC 1 Rule"
NIC 1 Rule(0):   name = Rule 1, protocol = tcp, host ip = , host port = 9999, guest ip = , guest port = 9090

Add a port forwarding rule on the command line

A portforwarding rule can be added on the command line with VBoxManage modifyvm.
The following example adds a forwarding rule for SSH (port 22) which listens on port 2200:
VBoxManage modifyvm $vmName --natpf1 'port forwarding rule ssh,tcp,,2200,,22'
With the rule in place, I can connect to the guest from the host:
PS C: > ssh -p2200 rene@localhost
…

Index