Blog Post

VirtualBox Multiple TCP Port Forwarding

When I need to test some Django code, I find it much easier to just run or install a copy of Turnkey Linux and grab the appliance that I need. They have a few different types of appliances and if you need to do any code testing for Django, Rails, Drupal, and more, I suggest you give this a shot if you don’t want to sit there and configure a server just for testing.

One thing with the Turnkey Django appliance, there are 2 HTTP ports. One is 80 for the Django administration interface and the other is 10000 for the Webmin administration interface. One problem I just ran across was how do I forward 2 TCP ports with Virtual Box. Easy, just do this:

$ VBoxManage setextradata <vbox guest name> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/apache/HostPort" 8888
$ VBoxManage setextradata <vbox guest name> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/apache/GuestPort" 80
$ VBoxManage setextradata <vbox guest name> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/apache/Protocol" TCP

To connect to your guest OS via Firefox, you would go to the following URL in your web browser: http://localhost:8888

This works great for one port, but what if I need 2 HTTP ports? Then try this:

$ VBoxManage setextradata <vbox guest name> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guesthttp/HostPort" 8889
$ VBoxManage setextradata <vbox guest name> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guesthttp/GuestPort" 10000
$ VBoxManage setextradata <vbox guest name> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guesthttp/Protocol" TCP

To connect to your guest OS via Firefox to this port, look above on how I explained the connection via port 8888. Just change the 8888 to 8889.
Adding more than this, well that I will have to dig in to further, but for this case, I only needed to forward the 2 HTTP/Apache ports.

Now if you would like to SSH into your guest, try this:

$ VBoxManage setextradata <vbox guest name> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort" 2222
$ VBoxManage setextradata <vbox guest name> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort" 22
$ VBoxManage setextradata <vbox guest name> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol" TCP

You can now SSH into your guest box by typing the following at the command line:

$ ssh -p2222 username@localhost

Or you could add the following to your ~/.ssh/config:

Host guest
        Hostname localhost
        Port 2222
        User username

And now to connect you would just do:

$ ssh guest
This entry was posted in Application and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • sage

    Or you could just use Host Interface Networking for bridging AND let the VM grab a preset IP adress by using Static DHCP in your router, then do the forwarding in the router instead.

    Not that there’s anything wrong with your method, but it’s always good to know more than one.

  • Vadim P.

    What is this TurnkeyLinux thing about? Is it affiliated with Canonical or are they just posing as such?

  • @sage: thanks for that pointer! I will definitely look into that as I typically have multiple vm’s up and running.

    @Vadim: no, not affiliated with Canonical. They just provide already setup Ubuntu appliances which make life so much easier.

  • Vadim P.

    Thanks for the explanation. I was really wary of a website that was looking awfully close to ubuntu.com but failed to mention any affiliation to it. Now, I definitely won’t trusting them with any of my data or code :\

  • @Vadim: I know the devs of it, they are perfectly fine. No reason to break out the tin foil hat in order to avoid them 🙂 It is a very easy and slick setup. It makes developing on the platforms they offer much easier.

  • Hi, I’m one of the TurnKey Linux developers. I’d like to thank nixternal for standing up for us, but really I can understand Vadim’s suspicion. It’s unwise to blindly trust just anybody who puts together a software appliance. We have an entry on our FAQ that tries to address this concern (“Why should I trust the security integrity of a TurnKey appliance?”). Also, I’d like to point out that we sign all of our releases with our personal GPG keys and that it would be awfully dumb of us to compromise the integrity of our own releases given that it would inevitably be discovered and would destroy our reputations.

  • Oh and regarding the supposedly nefarious resemblance to the official Ubuntu site: we’re using the Drupal theme Canonical publicly released to make it easy for Ubuntu-related sites to share a common visual theme. One of the great things about Ubuntu is that its sponsor doesn’t try to “own” the project. Everyone is welcome to contribute to the project in the areas that interest them.

    We consider TurnKey Linux to be a specialized extension of the Ubuntu community (I.e., one that focuses on software appliances), similar to how Ubuntu is a specialized extension of the Debian community. Just to be on the safe side we also gave the Canonical guys a heads up before we launched to make sure we weren’t doing anything inappropriate. Hope that clears it up!

  • Song Bo

    Why doesn’t this work?

    how to name the http service,”django”?

    VBoxManage setextradata Ubuntu10.04 “VBoxInternal/Devices/pcnet/0/LUN#0/Config/django/Protocol” TCP
    VBoxManage setextradata Ubuntu10.04 “VBoxInternal/Devices/pcnet/0/LUN#0/Config/django/GuestPort” 8000
    VBoxManage setextradata Ubuntu10.04 “VBoxInternal/Devices/pcnet/0/LUN#0/Config/django/HostPort” 8000

  • Subscribe to nixternal.com

     Subscribe in a reader

    Or, subscribe via email:
    Enter your email address:

  • Archives


semidetached
semidetached
semidetached
semidetached
%d bloggers like this: