告诉 Vagrant 主机的 ip [英] Tell Vagrant the ip of the host machine

查看:41
本文介绍了告诉 Vagrant 主机的 ip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个 vagrant box 作为一个项目的开发机器,它有一个奇怪的依赖项,我似乎只能在 ubuntu 上安装.

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.

我创建了我的数据库并在我的主机上安装了我的应用程序代码,我通过 NFS 文件共享和 postgres 配置与 vagrant 实例共享它们.

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.

我的问题是,当我将计算机从工作场所移到家中时,计算机的 ip 发生变化并且我的 database.yml 变得无效.为了让我的服务器和控制台正常工作,我需要在每次加入新网络时使用主机的新 IP 地址更新 yaml 文件.

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. 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.

由于 rails 应用程序在 vagrant 上运行(即使它的文件位于我的主机上),我尝试从 ifconfig 中 grep ip 将失败,因为它正在查看 VM 而不是主机框.所以这样的事情是行不通的:

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:

# database.yml
development:
  host: <%= `ifconfig en0 | grep "inet " | cut -d" " -f2` %>

Vagrant 文件中是否有一个配置来传递这些信息,或者有一种方法可以创建 ubuntu 实例可以读取的主机 ip 的 ENV 变量?

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?

推荐答案

根据这个,可以联系到主机通过虚拟机的默认网关.

According to this, you can reach the host through the VM's default gateway.

在来宾上运行 netstat -rn 应该会为您提供相关信息,在我的机器上将上面的代码更改为以下看起来可以让您顺利进行:

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:

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

这篇关于告诉 Vagrant 主机的 ip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆