11

I have a rails server on virtual machine and I need to access localhost from host. I've tried both NAT (by port forwarding - port 80 and 3000 on the guest side) and bridged adapter but nothing seems to work.

In both cases I was able to access port 80 but I've failed trying to connect to localhost on port 3000 (so there was no problem in accessing xampp server).

Please let me know if you have any idea how one can access localhost from host while server is on guest side.

I have linux (debian) on guest and Windows 7 on host side - I use VirtualBox.

9

There are at least three separate components at play here, each of which need to be configured correctly.

Rails binding address

When Rails (or another server application) opens to receive network connections, it will bind to both an IP and port. If an incoming request doesn't match both of these, it won't accept the connection. From rails server --help:

Usage: rails server [mongrel, thin etc] [options]
-p, --port=port                  Runs Rails on the specified port.
                                 Default: 3000
-b, --binding=IP                 Binds Rails to the specified IP.
                                 Default: localhost
...

So by default, Rails will only accept requests sent to its localhost (loopback) network adapter (called lo). Since this adapter is only accessible from within the same computer, the only requests it will accept will be from within the Debian VM itself: good and secure for testing, but not useful for production, or even testing from a different computer.

If you want your Rails to be accessible from any other computer (including your Windows 7 host), you will need to tell Rails to bind to whichever IP address Debian has that is accessible from the outside (see below).

However, an easier way is to tell Rails to bind to the special IP address 0.0.0.0, which means any network adapter. So, starting your Rails server like so:

rails server -p 80 -b 0.0.0.0

will make your website accessible to anyone who can see your VM on the network.

This is likely what has still messed you up even if you had the other two components working.

VM IP address

You need to point your browser at the Debian virtual machine, which is its own discrete computer.

The rule of thumb with localhost is that it always refers to the same computer. However, for networking purposes, your Debian VM is its own separate computer. If you're using a web browser in your Windows 7 host, localhost will always refer to your Windows 7 and nothing else.

You have two options here:

  • Set the VirtualBox VM adapter to Bridge mode (as you mentioned in your question). This will put your Debian directly on the same outside/home/work network as your Windows 7 host. You can then determine your Debian VM's IP by running ifconfig. Look for the IP of your eth0 or eth<something> adapter. Plug this into your browser's address bar.

  • Use a NAT adapter and VirtualBox port forwarding. IMO this is more complicated.

Debian firewall

It's been a while since I've worked on Debian so I don't know what the current defaults are, but if the above two things don't work then this might be worth looking into. If Debian is running a firewall, you'll need to open any ports you wish to access from another machine.

Hope this helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.