---
description: 'Sumber: https://travishorn.com'
---

# Tutorial Port Forwarding Pada Virtual Box

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689175385943/ac9425df-420a-4872-af4d-1fa11ac19f0c.png?auto=compress,format\&format=webp)

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689175419221/b97f7655-e4a6-4216-8a09-aa0d01e3bf68.png?auto=compress,format\&format=webp)

Click the **Settings** button.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689175438134/3a394e14-f810-459e-b5a0-1937fe32190a.png?auto=compress,format\&format=webp)

Click **Network** from the pane on the left.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689175469964/cd00b10b-fe85-4301-b942-883b383809c5.png?auto=compress,format\&format=webp)

Under the **Adapter 1** tab, click **Advanced**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689175499533/6a74320b-5449-4be4-bbc7-f5f2abd4e582.png?auto=compress,format\&format=webp)

Click **Port Forwarding**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689175524438/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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689175551536/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**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689175576667/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**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689175603725/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**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689175666336/a502d0f9-9da0-4ff0-be77-f84ab221b5f6.png?auto=compress,format\&format=webp)

Leave everything else blank.

Click **OK**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689175695100/ca6ceb4c-791e-449f-9ec0-91da184b30ce.png?auto=compress,format\&format=webp)

Click **OK** again.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689175716071/83cd6d02-37f7-4f49-a247-c715eac7194e.png?auto=compress,format\&format=webp)

### Using the Forwarded Port <a href="#heading-using-the-forwarded-port" id="heading-using-the-forwarded-port"></a>

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

```bash
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:

```bash
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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689176151212/1a7f1411-beee-449c-99f5-f8f12d75eeba.png?auto=compress,format\&format=webp)

### Other Ports to Forward <a href="#heading-other-ports-to-forward" id="heading-other-ports-to-forward"></a>

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 <a href="#heading-an-alternate-solution-setting-up-a-bridged-adapter" id="heading-an-alternate-solution-setting-up-a-bridged-adapter"></a>

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689175385943/ac9425df-420a-4872-af4d-1fa11ac19f0c.png)

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689175419221/b97f7655-e4a6-4216-8a09-aa0d01e3bf68.png)

Click the **Settings** button.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689175438134/3a394e14-f810-459e-b5a0-1937fe32190a.png)

Click **Network** from the pane on the left.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689175469964/cd00b10b-fe85-4301-b942-883b383809c5.png)

Select **Bridged Adapter** from the **Attached to** dropdown menu.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689176508964/f980fab2-ac72-4027-99b4-01119b2657ad.png?auto=compress,format\&format=webp)

Click **OK**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689175716071/83cd6d02-37f7-4f49-a247-c715eac7194e.png)

#### Using a Bridged Adapter <a href="#heading-using-a-bridged-adapter" id="heading-using-a-bridged-adapter"></a>

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`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1689177064936/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.

```bash
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.
