3

Mastering Port Forwarding in VirtualBox: Unlocking Connectivity

 9 months ago
source link: https://travishorn.com/mastering-port-forwarding-in-virtualbox-unlocking-connectivity
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Mastering Port Forwarding in VirtualBox: Unlocking Connectivity

Mastering Port Forwarding in VirtualBox: Unlocking Connectivity

In the realm of virtualization, VirtualBox stands as a powerful tool to create and manage virtual machines. Using it, you may often encounter scenarios where you need to establish communication between the host and virtual machines, and this is where port forwarding comes into play. In this comprehensive guide, we will explore the concept of port forwarding within the context of VirtualBox, providing you with a step-by-step configuration process, insights into connecting to your virtual machine, a list of commonly forwarded ports, and even an alternative solution using a bridged adapter. By the end, you'll be equipped with the knowledge and skills to set up and use forwarded ports on your virtual machines.

By default, virtual machines running inside VirtualBox use a virtual network adapter attached to NAT. This means that the machine is not accessible on your host network, but rather a virtual network inside of the host computer. Any time you need to connect to your virtual machine remotely (whether by SSH, HTTP, or another protocol), you'll need to make sure the appropriate port is forwarded from your host machine. Here's how to do that.

Open Oracle VM VirtualBox Manager on the host machine.

ac9425df-420a-4872-af4d-1fa11ac19f0c.png?auto=compress,format&format=webp

Click to select the virtual machine from the list on the left.

b97f7655-e4a6-4216-8a09-aa0d01e3bf68.png?auto=compress,format&format=webp

Click the Settings button.

3a394e14-f810-459e-b5a0-1937fe32190a.png?auto=compress,format&format=webp

Click Network from the pane on the left.

cd00b10b-fe85-4301-b942-883b383809c5.png?auto=compress,format&format=webp

Under the Adapter 1 tab, click Advanced.

6a74320b-5449-4be4-bbc7-f5f2abd4e582.png?auto=compress,format&format=webp

Click Port Forwarding.

072969a7-c4da-4be0-b64f-9aaaf75af639.png?auto=compress,format&format=webp

Click the Adds new port forwarding rule. button. It's on the right side.

d72cc229-0c58-4f20-b217-03f9e233e2dd.png?auto=compress,format&format=webp

In the table on the left, double-click Rule 1. This will allow you to edit the name of the rule. Type in the name of the service. For example, SSH.

10e76a0a-cc76-4e06-9d8e-b5c1a8db466d.png?auto=compress,format&format=webp

Under Host Port, enter the port number you want the host machine to listen on. This can be anything, but for simplicity, I recommend using the same as the guest port. For SSH, use port 22.

b2a05df8-2153-4240-aa18-84dcbd1cf3f1.png?auto=compress,format&format=webp

If you have multiple virtual machines which you will be SSHing into, or you already have an SSH server running on the host machine itself, you may want to enter a different Host Port. In that case, choose any number you like between 1024 and 49152. Just make sure it is not in use by any other service.

Under Guest Port, enter the port number that your virtual machine is listening on. For SSH, use port 22.

a502d0f9-9da0-4ff0-be77-f84ab221b5f6.png?auto=compress,format&format=webp

Leave everything else blank.

Click OK.

ca6ceb4c-791e-449f-9ec0-91da184b30ce.png?auto=compress,format&format=webp

Click OK again.

83cd6d02-37f7-4f49-a247-c715eac7194e.png?auto=compress,format&format=webp

Using the Forwarded Port

Now, if you want to SSH into the virtual machine from the host machine, you can use something like this:

ssh username@localhost

Make sure to change username to the actual username that is set up on your virtual machine.

If you want to connect to the virtual machine from some other machine than the host, try something like this:

ssh username@host_ip_address

Change username to the actual username and change host_ip_address to the host's actual IP address. On Windows, you can determine this from the ipconfig command.

Other Ports to Forward

Some common ports you may consider forwarding are...

  • Port 21 for FTP

  • Port 22 for SSH

  • Port 80 for HTTP traffic

  • Port 443 for HTTPS traffic

  • Port 3000 which is a common development port for things like Node.js

  • Port 3306 for MySQL/MariaDB access

An Alternate Solution: Setting up a Bridged Adapter

If you want to access the virtual machine but don't want to forward any ports, you could attach the virtual network adapter to a bridged adapter. This places the virtual machine directly on the network with your other devices. It will receive its own IP address via DHCP.

Open Oracle VM VirtualBox Manager on the host machine.

ac9425df-420a-4872-af4d-1fa11ac19f0c.png

Click to select the virtual machine from the list on the left.

b97f7655-e4a6-4216-8a09-aa0d01e3bf68.png

Click the Settings button.

3a394e14-f810-459e-b5a0-1937fe32190a.png

Click Network from the pane on the left.

cd00b10b-fe85-4301-b942-883b383809c5.png

Select Bridged Adapter from the Attached to dropdown menu.

f980fab2-ac72-4027-99b4-01119b2657ad.png?auto=compress,format&format=webp

Click OK.

83cd6d02-37f7-4f49-a247-c715eac7194e.png

Using a Bridged Adapter

First, you need to determine what IP address the virtual machine received via DHCP. This can most easily be done on the virtual machine.

On Linux, you can use ip addr show.

2e27d153-db84-4a58-a358-7fb5440d489a.png?auto=compress,format&format=webp

Now you can connect (via SSH, for example) to the machine using the virtual machine's IP address you just discovered.

ssh username@vm_ip_address

By unraveling the mysteries of port forwarding in VirtualBox, we have taken a significant stride toward optimizing our virtualization endeavors. We now understand how to seamlessly connect our virtual machines to the outside world. Through the step-by-step configuration process, we have simplified the process of port forwarding, making it accessible to users of all levels. We also explored some common ports, discovering which services can benefit from port forwarding, and even ventured into the realm of bridged adapters as an alternative solution.

Cover photo by Danielle Suijkerbuijk on Unsplash.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK