32

I'm trying simple port forwarding with VirtualBox and it appears VirtualBox.exe is listening on the host to the port, but not actually forwarding the connection to the guest. I've tried on multiple ports, and I've restarted VirtualBox several times. Guest to host connectivity, on the other hand, is fine. Here's the info:

Host: Win7 x64
Guest: Win7 x64
VirtualBox version: VirtualBox 4.2.10
Firewalls: All firewalls on host and guest disabled.

Virtual Box > Machine Settings > Network
 Adapter 1: 
  Attached to: NAT
  Cable Connected : True
  Port Forwarding:
   Rule 1: TCP, HostIP=127.0.0.1, HostPort=8081, GuestIP=127.0.0.1, GuestPort=8081

On Host:
[VirtualBox.exe]  TCP    127.0.0.1:8081         IBM-6KT5OPCF76P:0      LISTENING

On Guest:
I never get the connection request on port 8081 or any others I've tried.

Any ideas? I'd like to avoid using VirtualBox's bridged adapter for multiple reasons. I was under the impression that NAT port forwarding was relatively straightforward with VirtualBox.

Thanks!

Roberto Olivares
  • 1,053
  • 1
  • 11
  • 19

10 Answers10

46

Paravirtualized Network* (virtio-net) is the key

  1. select your vm that you want for port forwarding.

    • NOTE: Make sure the vm is in stop mode.
  2. Select “Settings"

  3. Select “Network"
  4. Select any free Adapter. Lets say "Adapter 2"
  5. Enable "Enable Network Adapter"
  6. In Attached to dropdown, select "NAT"
  7. Select "Advanced"
  8. In Adapter Type: Select “Paravirtualized Network (virtio-net)” [ This is important ]
  9. Select “Port Forwarding"
  10. In Right hand part of the “port forwarding” dialog box, select “+"
  11. Provide the

    • Name: Anything you want. example for ssh, say “ssh"
    • Protocol: Type of protocol [ for ssh: TCP ]
    • Host IP: provide hostname from which host you want to connect to [ over here: 127.0.0.1 ]
    • Host Port: On what port of that Host you want to connect to remote port [ example: 60022 ]
    • Guest IP: Leave it Blank
    • Guest Port: To what port you want to connect from the above host. [ for ssh, the default 22 ]
Develop4Life
  • 7,581
  • 8
  • 58
  • 76
Amit
  • 461
  • 1
  • 4
  • 2
  • 3
    My issue was that I was trying to use port 22 on the host. When I changed it to a higher port (8080) it worked. – John Dec 08 '15 at 07:02
  • 9
    **8. In Adapter Type: Select “Paravirtualized Network (virtio-net)” [ This is important ]** You are absolutely correct - when I switched from the default "Intel PRO/1000 MT Desktop" emulator to the virtio-net emulator, port forwarding magically started working. I can't find this documented anywhere, though - how did you know that? – Joshua Davies Aug 03 '16 at 18:35
  • @JoshuaDavies ppl have the same mind set some times. That was i exactly thinking :) to ask him or know. I have been in the same situation like ur . – Develop4Life Dec 05 '17 at 13:23
  • Maybe I'am a bit late. I have a similar problem and this only works for me if the VM gets his IP address from DHCP. Anyone knows why? – ricoderks Mar 07 '18 at 09:14
  • 2
    I'm the same as John. Do not set 22 for the host port, it doesn't work. When I changed it to `2222` and hit save, immediately I could connect to port 2222. No machine reboot needed or anything. – Christopher Done Apr 26 '18 at 09:22
  • @John probably because host was also serving the SSH service on port 22, then there was a port conflict. Make sure sshd service is disabled in the host if trying to use port 22 as host port – felipeek Nov 16 '18 at 16:17
20

You've pointed guest ip in wrong manner it should be ip of guest (if you're statically assign ip address to guest) or leave it empty. Note you're pointed host ip as 127.0.0.1 it means that port you're forwarding will be accessible only from port (if it isn't what you're desired it should be empty as well).

VBoxManage modifyvm "win" --natpf1 ",tcp,,8081,,8081"

perhaps this is something you've wanted to do. (please look here for more details)

vvlevchenko
  • 884
  • 7
  • 5
  • 4
    This appears to have solved the problem. My intention was to forward [localhost:8081 on host] to [localhost:8081 on guest]. As wlevchenko said, going to the Port Forwarding dialog and simply removing the "127.0.0.1" in both of the IP columns (and leaving it blank) fixed it. I did need to reboot the virtual machine for port forwarding to work. – Roberto Olivares Mar 25 '13 at 12:54
  • 2
    Actually port-forward changes shouldn't require VM reboot, what is really needed that guest shows any network activity, after pf rules has been changed. – vvlevchenko Mar 28 '13 at 07:22
11

It is not clear which guest OS you are using.

I have faced the same problem.

My host was MAC PC and guest was CentOS 7 on VirtualBox.
I enabled the NAT port forwarding from guest to host at VirtualBox level for both ssh port 22 and http port 80.

However I found that I could not connect to the Apache HTTP server on Centos 7 guest from my MAC PC host on http connection.

To fix it, I have to set the firewalld service to allow port 80 connections.

Andy
  • 2,493
  • 6
  • 37
  • 63
4

Faced similar issue on Mac host, Fedora guest setup. Was trying to access http web server running on port 8000 of guest machine from host machine. Opening port 8000/tcp with firewall-cmd on guest machine solved problem.

sudo firewall-cmd --zone=public --add-port=8000/tcp
Iqbal
  • 2,094
  • 1
  • 20
  • 28
2

I faced a similar issue and it turns out to be unsigned drivers in the network stack installed by some corporate proxy interception software called proxycap that were causing virtualbox to error when setting up port forwarding. Check you VM machine logs for the error message while setting up port forwarding and listing the unsigned drivers and uninstall the corresponding application.

alastairtree
  • 3,960
  • 32
  • 49
  • I saw those messages in the VBox.log too, but it's not the cause. I signed those ProxyCap dlls and NAT was still not working. Here's the explanation why VirtualBox NAT won't work with ProxyCap: https://forums.virtualbox.org/viewtopic.php?f=6&t=82910&p=488206#p488206 – miro Nov 10 '20 at 18:48
1

I connect to my Centos7 like this:

Virtual Box > Machine Settings > Network
Adapter 1: 
Attached to: NAT
Cable Connected : True
Port Forwarding:
  Rule 1: TCP, HostIP=127.0.0.1, HostPort=8081, GuestIP=10.0.2.15, GuestPort=8081

$> ssh -p8081 [change]@127.0.0.1

HA S
  • 1,129
  • 12
  • 10
1

In case someone else met same problem with me, I wrote my mistake here:

I am using CentOS 7 as host and Windows as guest.

The only problem block me was: I forgot to disable Windows firewall settings in guest machine. Once disable it, everything works fine.

Zhang Buzz
  • 10,420
  • 6
  • 38
  • 47
1

If anyone is having same issues, try to uninstall redirectors such as ProxyCap, proxifier, etc. VirtualBox NAT doesn't work with them. You won't find any error message in the logs and you won't see anything in the pcap file if you try to trace VirtualBox networking. Disabling ProxyCap is not enough, it has to be uninstalled or you need to reset your network config with "netsh winsock reset" but ProxyCap won't work for you anymore.

miro
  • 149
  • 3
  • 11
1

For GNU/Linux users, don't forget the first 1024 ports are restricted to the root user

If you try forwarding a port lower than 1024, it won't even appear as LISTENING with netstat.

Skippy le Grand Gourou
  • 6,976
  • 4
  • 60
  • 76
0

In my case the guest OS is CentOS8, the host OS is Ubuntu 22.04, the forwarded port was 1022 on the host, I changed it to 10022 then it worked.

user3366938
  • 1
  • 1
  • 1