51

I have a VirtualBox machine running Ubuntu 12.04 in a Mac OS X host machine. In my host machine (Mac OS X), I have PostgreSQL installed. I would like to connect to this PostgreSQL server from my Ubuntu virtual machine (I know normally it's the opposite way).

I guess I should configure some networking parameters in VirtualBox itself. All I get from Vagrant docs is I need to assign my vm a static IP to use a private network. But once created, how do I reach my host from my guest?

I didn't find anything like that in vagrant docs. So, there's probably a good reason for that. Does it make sense? I don't want to duplicate PostgreSQL installation. Just use my current existent one.

4

2 回答 2

98

您可以使用 VM 上的默认网关从来宾访问您的主机。

请参阅此答案以获取解释。

通过运行netstat -rn,您可以获得默认网关,然后在应用程序的配置文件中使用该 IP 地址。

运行netstat -rn | grep "^0.0.0.0 " | cut -d " " -f10应该会给你正确的IP地址。(仅在我的机器上测试过)

于 2013-11-12T21:57:10.900 回答
34

简单的方法 - 只需从 vagrant 内部使用这个“神奇”IP,无需任何额外配置:

10.0.2.2

不知道它是否总是静态的,但对我来说可以工作而且非常方便 - 我可以在家中或办公室使用笔记本电脑 - 通过路由器为我分配了不同的 IP,但我的虚拟机知道他们主人的“可信赖的名字”

于 2016-12-07T18:11:07.187 回答