Thursday, November 11, 2010

Configure VirtualBox Host-to-Guest ssh port forwarding with NAT

My Host OS : WindowsXP
My Guest OS: is Linux Fedora Core.

By default, VirtualBox sets up new Guest with NAT (Network Address Translation) configuration.

NAT configuration forms a private network between Host and the Guest. The Host takes a role of a router that maps the incoming/outgoing traffic of a NAT Guest transparently.

The annoying fact, however, is that this private network is not visible even from the Host machine. With default settings I cannot ssh or sftp from WindowsXP Host to Linux Guest. None of the network services such as Apache web server, or ssh daemon, or my own services that I might want to run on the Guest are visible to the outside world, including the Host itself.

This is a major inconvenience that defeats the purpose of running various virtual Guests in a first place.

To overcome this obstacle, we need to enable NAT port forwarding.

Essentially, with port forwarding, we ask NAT engine to route external traffic that arrives on a certain port of a Host to a different port on Guest.

The ssh daemon that runs on my Linux Guest listens for incoming connection requests on port 22. We are going ask NAT to route external traffic that arrives on port 2222 to the Guest's 22.

The preparation steps:
  • Shut down Linux Guest
  • Exit VirtualBox application
  • Add VirtualBox path to %PATH% variable:
    • Bring System Properties dialog with My Computer->Properties->Advanced
    • Click on Environment Variables
    • Edit Path system variable and add C:\Program Files\Oracle\VirtualBox path.
To setup port forwarding: 

 c:\> VBoxManage modifyvm "salmon" --natpf1 "guestssh,tcp,,2222,,22"

Where:
  • 'tcp' is mandatory protocol type (could be 'udp' as well)
  • 'guestssh' is a description name that can be used to identify and remove the port forwarding
  • The first skipped generic parameter between commas of "tcp,,2222" is external interface IP. By default, all traffic that might arrive on port 2222 of every network interface (localhost, LAN, WiFi, etc.) is routed to port 22 of the Guest. However, you can be more restrictive, and say allow only local traffic from 127.0.0.1 to be routed to the Guest.
  • The second skipped generic parameter between commas of "2222,,22" is used to specify static IP address of the Guest if it is assigned one by VirtualBox in favor of dynamically-assigned DHCP address.
To remove port forwarding:

c:> VBoxManage modifyvm "salmon" --natpf1 delete "guestssh"

To test the connection:
  • Boot the Guest OS.
  • Login in to Linux and make sure the sshd daemon is running.
  • On Windows XP desktop,
    • Start WinSCP free scp/sftp client.
    • Connect to IP: 127.0.0.1 Port: 2222
You should be able to login now into your Guest and transfer files.

Source: VirtualBox Manual  

No comments:

Post a Comment