Ruby-on-rails 告诉 Vagrant 主机的 ip
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19917148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tell Vagrant the ip of the host machine
提问by jstim
I am using a vagrant box as a development machine for a project with an odd dependency I can only seem to install on ubuntu.
我正在使用 vagrant box 作为具有奇怪依赖项的项目的开发机器,我似乎只能在 ubuntu 上安装。
I created my database and have my app code on my host computer, and I share them with the vagrant instance through the NFS file share and postgres config.
我创建了我的数据库并将我的应用程序代码放在我的主机上,我通过 NFS 文件共享和 postgres 配置与 vagrant 实例共享它们。
My issue is that, as I move my computer from work to home, the ip for my computer changes and my database.ymlbecomes invalid. To get my server and console working, I need to update the yaml file with the host's new ip address each time I join a new network.
我的问题是,当我将计算机从工作场所移到家中时,计算机的 ip 发生变化并且我的计算机database.yml变得无效。为了让我的服务器和控制台正常工作,我需要在每次加入新网络时使用主机的新 IP 地址更新 yaml 文件。
Since the rails app is running on vagrant (even though it's files are located on my host machine), any attempt for me to grep the ip out of ifconfig will fail because it's looking at the VM and not the host box. So something like this doesn't work:
由于 rails 应用程序在 vagrant 上运行(即使它的文件位于我的主机上),任何让我从 ifconfig 中 grep ip 的尝试都将失败,因为它正在查看 VM 而不是主机框。所以这样的事情不起作用:
# database.yml
development:
host: <%= `ifconfig en0 | grep "inet " | cut -d" " -f2` %>
Is there a config in the Vagrant file to pass this info through, or a way to create an ENV variable of the host ip that the ubuntu instance can read?
Vagrant 文件中是否有配置可以传递此信息,或者是否有创建 ubuntu 实例可以读取的主机 ip 的 ENV 变量的方法?
回答by Matt Cooper
According to this, you can reach the host through the VM's default gateway.
根据这个,可以达到通过虚拟机的默认网关的主机。
Running netstat -rnon the guest should give you the relevant info, on my machine changing your code above to the following looks like it would get things going for you:
netstat -rn在来宾上运行应该会为您提供相关信息,在我的机器上将您上面的代码更改为以下看起来可以为您提供帮助:
# database.yml
development:
host: <%= `netstat -rn | grep "^0.0.0.0 " | cut -d " " -f10` %>
回答by Daniel Garmoshka
Not sure, if it will work for everybody, though all my virtual box machines see host machine by this "magic" ip:
不确定,它是否适用于每个人,尽管我所有的虚拟机都通过这个“魔法”IP 看到主机:
10.0.2.2
Don't know if it's always static, though for me works and it's very convenient - I can use laptop at home, from office - having assigned different IPs to me by routers, but my VMs know the "trusty name" of their master
不知道它是否总是静态的,尽管对我来说是有效的,而且非常方便 - 我可以在家中或办公室使用笔记本电脑 - 通过路由器为我分配了不同的 IP,但我的虚拟机知道它们的主人的“可信名称”
回答by Filipe Pina
As an alternative to Matt's solution, you can also use private networksyour Vagrantfile.
作为替代马特的解决方案,你也可以使用专用网络的Vagrantfile。
If you add a line such as:
如果添加一行,例如:
config.vm.network "private_network", ip: "192.168.42.10"
Vagrant will add a second adapter to the virtual machine and hook it up to host-only adapter on that subnet.
Vagrant 将向虚拟机添加第二个适配器,并将其连接到该子网上的仅主机适配器。
This way you can then let database.ymlsimply point to 192.168.42.1all the time (in this example).
这样你就可以让你一直database.yml简单地指向192.168.42.1(在这个例子中)。

