Skip to content

Forwarding ports to a VirtualBox

Good afternoon,

If you want to forward ports to a virtualmachine in VirtualBox you should do this:

Create an interface "tap"

#sudo tunctl -u $USER

Set the tap address:

#sudo ip addr add dev tap0
# sudo ip link set tap0 up

Enable Ip Forward

# sudo sysctl net.ipv4.ip_forward=1

Add route:

#sudo route add -host dev tap0

Create nat:

# sudo iptables --flush
#sudo iptables -t nat --flush
#sudo iptables -t nat -A POSTROUTING --out-interface eth1 -j MASQUERADE
#sudo iptables -A FORWARD --in-interface eth1 -j ACCEPT

Redirecting port 3333 to 3389:

#iptables -t nat -A PREROUTING -i eth1 -p tcp -d --dport 3333 -j DNAT --to

eth1 - Ethernet connected to the router. - IP of virtualmachine - IP of tap - IP of my real machine that receive the connections in 3333

After this configure manually your virtualmachine to:

Subnet Mask:
Default Gateway:

I had problems with '--' so here is a file with the commands: Forward ports to a virtualmachine.

Hope this is usefull,

IPtables Tutorial
IPTables Port Redirect
VirtualBox, com nat
VBoxManage Port Forward

Published inLinux


  1. Pedro Pedro

    isso tudo na máquina virtual ou na real?

  2. A ideia é redirecionar coisas da internet por exemplo pra uma maquina virtual, entao como as informacoes na verdade chegam na maquina host voce precisa redirecionar do host pra vm.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.