DropVPS Team
Writer: Cooper Reagan
Managing Port Forwarding in VirtualBox

Table of Contents
Port forwarding is an essential tool in VirtualBox that allows external network traffic to be directed to specific services running on a virtual machine (VM). VirtualBox uses NAT (Network Address Translation) networking by default, which isolates the VM from the host machine and external networks. While this setup enhances security by keeping the VM protected, it also prevents external access to the services running within the VM.
To solve this, port forwarding maps a port on the host machine to a port on the VM, allowing traffic from the host to reach specific services, such as an SSH server or a web server, inside the VM. For example, you can set a rule that forwards traffic from port 2222 on the host to port 22 on the guest VM, enabling you to access an SSH service running inside the VM by connecting to the host’s IP at port 2222.
Port forwarding is commonly used in development environments where VMs need to be accessed for testing or when a VM needs to expose certain services to external users, all while maintaining the security of the VM and its internal network. This feature allows network traffic to be securely directed, ensuring that only the necessary ports are accessible from outside the VM.
Step-by-Step Guide to Port Forwarding
Setting up port forwarding in VirtualBox is straightforward, and it involves creating rules that map a port on your host machine to a port on your virtual machine (VM). This allows you to access services inside your VM from outside, such as SSH, HTTP, or other protocols. Here’s a step-by-step guide on how to set up port forwarding:
1. Shut Down the Virtual Machine
Before making any changes to the network settings, ensure the VM is powered off.
2. Open VM Settings
Right-click on the VM you want to configure and select Settings. This opens the configuration menu for that VM.
3. Navigate to the Network Tab
In the Settings menu, go to the Network tab, which is where you can configure network adapters.
4. Access Advanced Settings
Under the Adapter 1 section (or whichever adapter you’re using), click on the Advanced button to reveal additional settings.
5. Click on Port Forwarding
In the Advanced section, click on Port Forwarding to open the port forwarding rules dialog.
6. Add a New Rule
Click on the + icon to add a new port forwarding rule. A new entry will appear where you can specify the details of the port mapping.
7. Configure the Rule
- Name: Give your rule a name (e.g., SSH or HTTP).
- Protocol: Select the protocol (TCP or UDP).
- Host IP: Leave this blank to use the default.
- Host Port: Enter the port on the host machine that you want to use (e.g., 2222 for SSH).
- Guest IP: Leave this blank or use the default IP (10.0.2.15 for NAT).
- Guest Port: Enter the port number on the guest VM (e.g., 22 for SSH, 80 for HTTP).
8. Save the Rule
After entering the details, click OK to save the rule.
9. Start the VM
Power on the VM and ensure that the service you’re trying to forward (e.g., SSH or web server) is running on the guest VM.
10. Test the Connection
From the host machine, try connecting to the service using the host’s IP address and the port you’ve specified. For example, if you set up an SSH rule, you would use the following command to connect:
ssh -p 2222 [email protected]
This should successfully connect to the guest VM via the forwarded port.
By following these steps, you can easily set up port forwarding in VirtualBox, allowing you to access various services within your virtual machines securely.
Using VBoxManage for Port Forwarding
VBoxManage is a command-line tool that allows you to manage VirtualBox virtual machines (VMs) and their settings without using the graphical interface. This tool is particularly useful for automating tasks or when you prefer to work in a terminal environment. You can use VBoxManage to configure port forwarding for VMs that are using NAT networking, similar to how you would do it via the VirtualBox GUI.
Here’s a step-by-step guide on how to set up port forwarding using VBoxManage:
1. List Available Virtual Machines
The first step is to list all the VMs registered in VirtualBox. Use the following command:
VBoxManage list vms
This will display the names and UUIDs of all available VMs, which you will need for the next steps.
2. Check Current Port Forwarding Rules
If you want to check the current port forwarding settings for a specific VM, you can run:
VBoxManage showvminfo "VM Name" --details
This will show detailed information about the VM, including any active port forwarding rules under the NAT network adapter.
3. Add a Port Forwarding Rule
To add a new port forwarding rule, use the following command:
VBoxManage modifyvm "VM Name" --natpf1 "rule-name,tcp,,host-port,,guest-port"
Replace the following placeholders:
"VM Name": The name of your VM."rule-name": A descriptive name for the rule (e.g., SSH).host-port: The port on the host machine you want to use (e.g., 2222).guest-port: The port inside the VM you want to forward (e.g., 22 for SSH).
For example, to forward port 2222 on the host to port 22 on the guest (for SSH access), the command would be:
VBoxManage modifyvm "YourVMName" --natpf1 "ssh-rule,tcp,,2222,,22"
4. Verify the Rule
After adding the rule, verify that it was successfully added by running the showvminfo command again. The new rule should appear in the output.
5. Test the Port Forwarding
Once the VM is running and the port forwarding rule is in place, test the connection by accessing the service (e.g., SSH) from the host machine. For SSH, you can use:
ssh -p 2222 [email protected]
By using VBoxManage, you can quickly and efficiently set up port forwarding without the need for the graphical interface. This is ideal for batch processing or managing multiple VMs via scripts.