20

I am currently running Rails 3.2.3 server on a laptop running Ubuntu 12.04 and would like to access it via VirtualBox (ver 4.1.16) from a WinXP (SP3) VM, which is also running on the laptop. Tried using bridged networking instead of NAT but end up with a refused connection in Firefox (ver 13). Firefox displays an error page which includes the following:

Unable to connect.

Firefox can't establish a connection to the server at localhost:3000.

Is there a straightforward way to access the Rails server from VirtualBox remotely? Not sure if Vagrant will resolve the issue since it is designed to work the other way around (from the host OS to the VM). Would be good to access the Rails server remotely as it means testing can be easily done with Internet Explorer (using ver 8) on a single PC on demand.

user1467809
  • 201
  • 1
  • 2
  • 3

7 Answers7

40

Using NAT:

  1. Go to VirtualBox... Settings -> Network -> Port Fowarding
    • Enter 3000 for Host Port
    • Enter 3000 for Guest Port
  2. Go to your VirtualBox guest machine.
  3. In the terminal run ifconfig, use the inet addr for eth0. In my case it is 10.0.2.15
  4. When you run the rails server, in the VirtualBox guest machine, use the following command:
    $ rails server -b 10.0.2.15
    You want to bind to that address.
  5. On your Host System, go to the browser and visit http://127.0.0.1:3000
erik998
  • 401
  • 4
  • 3
  • 2
    OP is asking how to access a server running on the *host* from a client running on the *guest*. This answer is the other way round. – PJSCopeland Nov 02 '15 at 00:35
  • Nevertheless, the important part of the answer is the step 4. – mentatkgs May 11 '16 at 14:00
  • Um, no. Step 4 maybe important to _this answer_, but this answer is still not an answer to the question. – radixhound Jun 21 '16 at 17:00
  • Really saving my days ... step 4 is really crucial. I have been looking information around for NAT port forwarding. – bLaXjack Jun 27 '16 at 14:38
  • This worked for me, connecting to the host machine rails server from my virtualbox guest machine, but I used the actual IP address in the url, not 127... – Joe Sak Feb 14 '17 at 15:57
17

The answer is yes there is a simple way to do it. I am using VirtualBox on OS X Mountain Lion, but I suspect it will work the same for Ubuntu.

  1. You don't need to use bridged mode, NAT works fine.
  2. Just use the IP address of your host machine. e.g. 192.168.xxx.xxx:3000
  3. As an alternative, you can use ipconfig /all on your virtual machine in the command prompt (use windows search / find (windows+f) and search for 'cmd') Look for Default Gateway and use that IP. In my case it is 10.0.2.2 so 10.0.2.2:3000 works
radixhound
  • 2,190
  • 2
  • 18
  • 23
  • I don't know why this is flagged down. None of the other answers actually gave the straight up answer. – radixhound Oct 22 '13 at 22:50
  • 1
    As per some of the comments on [this question](http://stackoverflow.com/questions/1261975/addressing-localhost-from-a-virtualbox-virtual-machine?rq=1), IE8 insists on having the protocol (`http://`) present in the URL, or it simply won't succeed in connecting. – PJSCopeland Nov 02 '15 at 00:44
  • I used the gateway method here and just edited my hosts file on my windows 10 virtual guest machine to point my domain/subdomain (multi-tenant app). So on windows guest I can now brows to something like: http://mysubdomain.myappname:3000/ and it works fine. – lacostenycoder Feb 18 '16 at 18:08
6

Instead of localhost you should have typed the IP of the host machine. With bridged mode the vm and your host machine have a different IP in your local network.

bnvdarklord
  • 309
  • 2
  • 6
  • Using Bridged mode enabled communication from the VM to the Rails Server on the host OS. Had to create an allow rule to allow all incoming connections to the Rails server to go through in order to use UFW (Uncomplicated Firewall), which is the built in Firewall for Ubuntu. – user1467809 Jun 21 '12 at 04:58
1

If you're running rails in development mode, it comes with webrick by default which has access restricted just to localhost. You could use something like thin in dev, which allows access from another machines.

Alter Lagos
  • 12,090
  • 1
  • 70
  • 92
0

You need the local IP of Ubuntu. To get this type ifconfig in the terminal.

I've had success using NAT with virtualbox, after you get the machine's ip, type it with the 3000 port from the VM and you should be able to access it.

bbonamin
  • 30,042
  • 7
  • 40
  • 49
0

Start Rails server remotely and then just connect to it:

http://virtualboxhost:3000

If it doesn't work:

  1. Try ping virtualboxhost
  2. see netstat -l on virtualboxhost for opened 3000 port
odiszapc
  • 4,089
  • 2
  • 27
  • 42
0

If you're using Pow...

...and have configured your app as myapp, you can't access it as myapp.dev from the guest machine, but you can use the "magic" xip.io domain. Just access http://myapp.10.0.2.2.xip.io and it should work!

szeryf
  • 3,197
  • 3
  • 27
  • 28