How to configure port forwarding in VirtualBox for NAT Networking

configure port forwarding in virtualbox command line. virtualbox port forwarding to host. virtualbox port forwarding range. vmware port forwarding. virtualbox ssh connection refused. ssh into virtualbox from another computer. vmware player port forwarding. virtualbox port forwarding from guest to host. ssh using putty from windows to virtual machine running on virtualbox . configure port forwarding in virtualbox in NAT Network. configure nat port forwarding in oracle virtualbox.

 

In Oracle VirtualBox we have two types of NAT Networking Mode.

  • NAT
  • NAT Network

NAT is much like a private network behind a router, the virtual machine is invisible and unreachable from the outside internet. You cannot run a server this way unless you set up port forwarding.

I will assume that you already have Oracle VirtualBox installed in your environment.

 

Configure Port Forwarding in VirtualBox for NAT

In NAT mode, the guest network interface is assigned to the IPv4 range 10.0.x.0/24 by default where x corresponds to the instance of the NAT interface +2. So x is 2 when there is only one NAT instance active. In that case the guest is assigned to the address 10.0.2.15, the gateway is set to 10.0.2.2 and the name server can be found at 10.0.2.3.

 

Configure or change default NAT subnet in VirtualBox

This subnet is internally defined for NAT networking Mode. You can also change this subnet to your own value
You can change the default NAT network for individual virtual machine using below command on your Windows Machine under the home directory of VirtualBox

C:Program FilesOracleVirtualBox>VBoxManage modifyvm "VM name" --natnet1 "192.168.100.0/24"

This command would reserve the network addresses from 192.168.100.0 to 192.168.100.254 for the first NAT network instance of “VM name“. The guest IP would be assigned to 192.168.100.15 and the default gateway could be found at 192.168.100.2.

Verify the new IP Address assigned to your interface

# ip addr show dev enp0s3
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:d5:cb:b6 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.15/24 brd 192.168.100.255 scope global dynamic noprefixroute enp0s3
       valid_lft 86138sec preferred_lft 86138sec
    inet6 fe80::a00:27ff:fed5:cbb6/64 scope link
       valid_lft forever preferred_lft forever

 

Revert the NAT subnet value to default in VirtualBox

Again to remove your custom changes for NAT Network and reverting to default subnet

C:Program FilesOracleVirtualBox>VBoxManage modifyvm "VM name" --natnet1  default

 

Configure port forwarding for NAT using CLI

 

Add port forwarding rule for guest having IP with DHCP CLient

You will need to know which ports on the guest the service uses and to decide which ports to use on the host. You may want to use the same ports on the guest and on the host. You can use any ports on the host which are not already in use by a service.

For example, to set up incoming NAT connections to an ssh server in the guest, use the following command:

C:>cd C:Program FilesOracleVirtualBox
IMPORTANT NOTE:

The virtual machine must be in powered off state before you configure port forwarding in VirtualBox

After I navigate to VirtualBox home directory, execute below command to configure port forwarding in VirtualBox for NAT

C:Program FilesOracleVirtualBox>VBoxManage modifyvm "CentOS8" --natpf1 "centos8,tcp,,2222,,22"

In the above example, all TCP traffic arriving on port 2222 on any host interface will be forwarded to port 22 in the guest. The protocol name tcp is a mandatory attribute defining which protocol should be used for forwarding, udp could also be used. The name centos8 is purely descriptive and will be auto-generated if omitted. The number after --natpf denotes the network card, as with other VBoxManage commands.

To remove this forwarding rule, use the following command:

C:Program FilesOracleVirtualBox>VBoxManage modifyvm "CentOS8" --natpf1 delete "centos8"

 

Add port forwarding rule for guest having Static IP

If for some reason the guest uses a static assigned IP address not leased from the built-in DHCP server, it is required to specify the guest IP when registering the forwarding rule, as follows:

C:Program FilesOracleVirtualBox>VBoxManage modifyvm "VM name" --natpf1 "guestssh,tcp,,2222,10.0.2.19,22"

This example is identical to the previous one, except that the NAT engine is being told that the guest can be found at the 10.0.2.19 address.

To forward all incoming traffic from a specific host interface to the guest, specify the IP of that host interface as follows:

C:Program FilesOracleVirtualBox>VBoxManage modifyvm "VM name" --natpf1 "guestssh,tcp,127.0.0.1,2222,,22"

This example forwards all TCP traffic arriving on the localhost interface at 127.0.0.1 through port 2222 to port 22 in the guest.

 

Configure port forwarding for NAT using GUI

You can also configure port forwarding in VirtualBox using the Virtual Machine's Settings page.

  • Click on Machine from the top panel menu of Oracle Virtual Box and select Settings
  • Next select Network from the left panel menu
  • Choose the Adapter using which you wish to configure port forwarding in VirtualBox
  • Click on Advanced
  • Next click on Port Forwarding to configure a rule. Here you can add/modify/delete a port forwarding rule for the respective vm
  • Click on plus sign on the left panel of the window and add the rule as per your requirement.
  • The Name can be any name, Protocol can be TCP or UDP depending upon the port type you plan to use on guest, Host Port can be any free port available on your host, Guest Port is the port to which you wish to connect from the host machine
  • In this example we are performing port forwarding to use SSH and connect to our guest Linux machine

How to configure port forwarding in VirtualBox for NAT Networking

 

 

Configure Port Forwarding in VirtualBox for NAT Network

The Network Address Translation (NAT) service works in a similar way to a home router, grouping the systems using it into a network and preventing systems outside of this network from directly accessing systems inside it, but letting systems inside communicate with each other and with systems outside using TCP and UDP over IPv4 and IPv6.

 

Configure port forwarding for NAT Network using CLI

To create a NAT network first navigate to the VirtualBox's home directory:

C:>cd C:Program FilesOracleVirtualBox

Next create a new NAT network and enable the same with a subnet value

C:Program FilesOracleVirtualBox>VBoxManage natnetwork add --netname natnet1 --network "192.168.15.0/24" --enable

Here, natnet1 is the name of the internal network to be used and 192.168.15.0/24 is the network address and mask of the NAT service interface. By default in this static configuration the gateway will be assigned the address 192.168.15.1, the address following the interface address, though this is subject to change.

To attach a DHCP server to the internal network, modify the example command as follows:

C:Program FilesOracleVirtualBox>VBoxManage natnetwork add --netname natnet1 --network "192.168.15.0/24" --enable --dhcp on

To add a DHCP server to an existing network, use the following command:

C:Program FilesOracleVirtualBox>VBoxManage natnetwork modify --netname natnet1 --dhcp on

To disable the DHCP server, use the following command:

C:Program FilesOracleVirtualBox>VBoxManage natnetwork modify --netname natnet1 --dhcp off

To start the NAT service, use the following command:

C:Program FilesOracleVirtualBox>VBoxManage natnetwork start --netname natnet1

If the network has a DHCP server attached then it will start together with the NAT network service.

To stop the NAT network service, together with any DHCP server:

C:Program FilesOracleVirtualBox>VBoxManage natnetwork stop --netname natnet1

To delete the NAT network service:

C:Program FilesOracleVirtualBox>VBoxManage natnetwork remove --netname natnet1

This command does not remove the DHCP server if one is enabled on the internal network.

Port-forwarding is supported, using the --port-forward-4 switch for IPv4 and --port-forward-6 for IPv6. For example:

C:Program FilesOracleVirtualBox>VBoxManage natnetwork modify --netname natnet1 --port-forward-4 "ssh:tcp:[]:1022:[192.168.15.5]:22"

This adds a port-forwarding rule from the host's TCP 1022 port to the port 22 on the guest with IP address 192.168.15.5. Host port, guest port and guest IP are mandatory.

To delete the rule, use the following command:

C:Program FilesOracleVirtualBox>VBoxManage natnetwork modify --netname natnet1 --port-forward-4 delete ssh

 

Configure port forwarding for NAT Network using GUI

You also an option to select NAT Network as a Network Mode in Oracle VirtualBox. But before we select Nat Network, we must have a network in place using which our virtual machine will get the DHCP leased IP.

To create a network for NAT Network

  • Click on File from the top panel menu and select Preferences
  • Next select Network from the left panel menu
  • On the right panel you will get an option to create a NAT Network
  • Click on the plus sign to add a new NAT Network. next select the newly created NAT Network and click on button which depicts Edit selected NAT Network
  • You can change your NAT Network Name here and assign a subnet value to be used for this network
  • Click on Supports DHCP to enable DHCP lease
  • Click on Port Forwarding to configure port forwarding in VirtualBox

How to configure port forwarding in VirtualBox for NAT Networking
 
Under Network give a name to the rule, use either TCP or UDP as the Protocol depending upon the port you plan to forward from the guest, the Host IP can be 127.0.0.1 or leave blank, Host Port can be any port which is not used by the host machine, Guest IP is the IP used by the guest machine, Guest Port is the port you wish to use from the guest node.
How to configure port forwarding in VirtualBox for NAT Networking
Here I have created port forwarding rule for port 22 (SSH) from the guest Linux VM

 

Lastly I hope the steps from the article to configure port forwarding in VirtualBox for NAT Networking was helpful. So, let me know your suggestions and feedback using the comment section.

 

References:
Oracle Virtual Box Networking

Leave a Comment

Please use shortcodes <pre class=comments>your code</pre> for syntax highlighting when adding code.