简体   繁体   English

告诉Vagrant主机的ip

[英]Tell Vagrant the ip of the host machine

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. 我正在使用一个流浪盒作为一个开发机器,用于一个奇怪的依赖项目,我似乎只能安装在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.yml becomes invalid. 我的问题是,当我将计算机从工作转移到家时,我的计算机的ip发生了变化,我的database.yml变得无效。 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地址更新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. 由于rails应用程序在vagrant上运行(即使它的文件位于我的主机上),任何尝试从ifconfig中取出ip的尝试都将失败,因为它正在查看VM而不是主机框。 So something like this doesn't work: 所以像这样的东西不起作用:

# 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变量?

According to this , you can reach the host through the VM's default gateway. 根据 ,您可以通过VM的默认网关访问主机。

Running netstat -rn on 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: 在guest上运行netstat -rn应该会在我的机器上为您提供相关信息,将上面的代码更改为以下内容,看起来它会让您感觉:

# database.yml
development:
  host: <%= `netstat -rn | grep "^0.0.0.0 " | cut -d " " -f10` %>

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,但我的VM知道他们的主人的“可信赖的名字”🐶

As an alternative to Matt's solution, you can also use private networks your Vagrantfile . 作为Matt解决方案的替代方案,您还可以使用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.yml simply point to 192.168.42.1 all the time (in this example). 这样你就可以让database.yml指向192.168.42.1 (在这个例子中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM