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 192.168.0.20/32 dev tap0
# sudo ip link set tap0 up

Enable Ip Forward

# sudo sysctl net.ipv4.ip_forward=1

Add route:

#sudo route add -host 192.168.0.150 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 192.168.0.100 –dport 3333 -j DNAT –to 192.168.0.150:3389

eth1 – Ethernet connected to the router.
192.168.0.150 – IP of virtualmachine
192.168.0.20 – IP of tap
192.168.0.100 – IP of my real machine that receive the connections in 3333

After this configure manually your virtualmachine to:

IP: 192.168.0.150
Subnet Mask: 255.255.255.0
Default Gateway: 192.168.0.100

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

Hope this is usefull,
Matheus

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

Published inLinux

2 Comments

Leave a Reply

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