简体   繁体   English

是否可以通过虚拟机使localhost工作?

[英]Is it possible to make localhost work through a Virtual Machine?

I am using a Macbook running 10.6. 我正在使用运行10.6的Macbook。 I am using VMware Fusion to run an Ubuntu Server minimal virtual machine. 我正在使用VMware Fusion运行Ubuntu Server最小虚拟机。 Ubuntu Server is running your basic LAMP stack. Ubuntu Server正在运行您的基本LAMP堆栈。

I do my development in Mac OS. 我在Mac OS中进行开发。 I have VMware share a directory from Mac OS to the Ubuntu Server. 我让VMware从Mac OS到Ubuntu服务器共享一个目录。 Ubuntu Server uses that directory for apache. Ubuntu Server将该目录用于apache。

I access my server is Mac OS in firefox using the ip address of my virtual machine. 我使用我的虚拟机的IP地址在firefox中访问我的服务器是Mac OS。 This is a pain because I have to find out what the ip address is of my virtual machine each time I boot it up. 这很痛苦,因为每次启动时我都必须查明虚拟机的IP地址。 I could set a static ip address but this causes problems if I move my Macbook from network to network. 我可以设置一个静态IP地址,但是如果我将Macbook从一个网络移动到另一个网络,则会导致问题。

Is there any configuration (NAT or Bridged or something) that would let me access my virtual machine from the Mac OS using localhost or something similar? 是否有任何配置(NAT或桥接的或其他某种配置)可以让我使用本地主机或类似工具从Mac OS访问我的虚拟机?

Thanks 谢谢

NAT should be OK. NAT应该可以。 Your VM is on a different subnet that way, you can give it the static IP you like, and it won't interfere with the (dynamic) IP on your real network. 这样,您的VM便位于不同的子网中,您可以为其提供所需的静态IP,而不会干扰您实际网络上的(动态)IP。

What you are looking for is the host-only networking adapter as opposed to the NAT or bridged adapters. 您正在寻找的是仅主机的网络适配器 ,而不是NAT或桥接适配器。 This creates a network interface on the virtual machine that only connects the actual host. 这将在虚拟机上创建仅连接实际主机的网络接口。 It is perfectly safe to set an IP address for this interface that does not change, and there will be no tricky NAT getting in the way. 为该接口设置一个不变的IP地址是绝对安全的,并且不会遇到麻烦的NAT。 It's a little network that only exists for communication between the real host and the virtual host. 这是一个很小的网络,仅用于真实主机和虚拟主机之间的通信。 It's exact purpose is so you can do development like this. 确切的目的是使您可以像这样进行开发。 I use the same feature on VirtualBox all the time, but VMWare has it as well. 我一直在VirtualBox上使用相同的功能,但VMWare也具有此功能。

Now, with a host-only adapter you might be worried that your VM now has no access to the Internet. 现在,使用仅主机适配器,您可能会担心您的VM现在无法访问Internet。 The answer is simple. 答案很简单。 Just make two adapters. 只需制作两个适配器。 eth0, eth1. eth0,eth1。 Make one of them a bridged or NAT adapter for Internet access. 使其中之一成为用于Internet访问的桥接适配器或NAT适配器。 Make the other one the host-only adapter for your development. 使另一个成为开发主机专用适配器。 Most modern Linux distros will automatically route accordingly. 大多数现代Linux发行版都会自动进行相应的路由。 I know for a fact that Ubuntu does, because I do it all the time. 我知道Ubuntu就是这样,因为我一直都这样做。 Again, this is with VirtualBox. 同样,这是VirtualBox。 Your mileage may vary with VMWare, but I can't imagine it's that different. 您的工作量可能因VMWare而异,但我无法想象这是不同的。

我正在使用Virtual Box并输入计算机本地地址(例如192.168.1.100)而不是localhost来解决问题。

Maybe I'm misunderstanding your question but why not just add an entry in your hosts file for the virtual machine? 也许我误会了您的问题,但是为什么不只在您的主机文件中为虚拟机添加一个条目呢? That way you can access it with some arbitrarily assigned name (like testmachine) instead of the IP. 这样,您可以使用一些任意分配的名称(例如testmachine)而不是IP来访问它。

This is the first tutorial I found through google: http://decoding.wordpress.com/2009/04/06/how-to-edit-the-hosts-file-in-mac-os-x-leopard/ 这是我通过Google找到的第一个教程: http : //decoding.wordpress.com/2009/04/06/how-to-edit-the-hosts-file-in-mac-os-x-leopard/

This would work best if your VM has a static IP, BTW (either no DHCP or configure the DHCP server to give that MAC the same IP every time). 如果您的虚拟机具有静态IP,即BTW(没有DHCP或将DHCP服务器配置为每次都为该MAC提供相同的IP),这将是最好的选择。 That way you don't have to worry about changing the hosts entry every time the DHCP server gives the VM a different IP. 这样,您不必担心每次DHCP服务器为VM提供不同IP时更改主机条目的情况。

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

相关问题 如何通过虚拟机IE浏览器连接到PHP localhost Web应用程序? - How to connect to PHP localhost web app through virtual machine IE browser? 在虚拟机上查看“ localhost”时,PHP包含正在中断 - PHP Includes are chopping when viewing “localhost” on Virtual Machine App Store无法通过Vmware虚拟机在Mac上连接吗? - App store not connecting on Mac through Vmware virtual machine? 是否可以在MacBook Pro的虚拟机中运行Alea GPU - Is it possible to run Alea GPU on MacBook Pro in Virtual Machine 虚拟主机打开本地主机 - virtual host opening localhost 检测在虚拟机上运行 - Detect running on virtual machine 通过Windows机器在Mac OS X上进行远程工作 - Remote work on Mac OS X through Windows machine 如何在虚拟机上开发? - How to develop on a virtual machine? 是否可以在不打开 VMWare 应用程序(和其他窗口)的情况下启动 VMWare Fusion 虚拟机? - Is it possible to power-on a VMWare Fusion virtual machine without opening the VMWare application (and other windows)? 在OSX中使matplotlib在虚拟环境中工作的最简单方法是什么? - What is the simplest way to make matplotlib in OSX work in a virtual environment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM