简体   繁体   English

vagrant / etc / hosts:machine IP vs localhost

[英]vagrant /etc/hosts: machine IP vs localhost

I have a vagrant machine setup with this IP address: 我有一个使用此IP地址设置的流浪汉机器:

Vagrant.configure("2") do |config|
    config.vm.network :private_network, ip: 192.168.33.11
    config.vm.network "forwarded_port", guest: 80, host: 8080
    config.vm.hostname = "my-devenv"
...
end

Everything works just fine. 一切正常。 But I am confused on the /etc/hosts file on the VM: What is the difference between using localhost and VM's IP (127.0.0.1 some-dev-site.dev vs 192.168.33.11 some-dev-site.dev)? 但我对VM上的/ etc / hosts文件感到困惑:使用localhost和VM的IP有什么区别(127.0.0.1 some-dev-site.dev vs 192.168.33.11 some-dev-site.dev)?

127.0.0.1 localhost
127.0.0.1 some-dev-site.dev
192.168.33.11 some-dev-site.dev

General 一般

The localhost is normally always the same on different machines: 127.0.0.1 (local loopback) and the VM IP is the external IP on the 'network'. localhost在不同的机器上通常始终相同:127.0.0.1(本地环回),VM IP是“网络”上的外部IP。 You can for example connect from your machine to your VM by accessing the VM IP, but if you connect to localhost from your machine to the VM, you end up on your own machine. 例如,您可以通过访问VM IP从计算机连接到VM,但如果从计算机连接到localhost到VM,则最终会使用自己的计算机。

If you bind a service to 127.0.0.1 you will not be able to reach it from 'outside' of the 'machine'. 如果将服务绑定到127.0.0.1,则无法从“计算机”的“外部”访问它。

This offers probably a better explanation if you want to read more: https://www.lifewire.com/network-computer-special-ip-address-818385 如果您想了解更多内容,这可能会提供更好的解释: https//www.lifewire.com/network-computer-special-ip-address-818385

More specific to your situation 更具体的情况

Not sure if I understood your question correctly, but I guess your question is about: what are the hostnames in the /etc/hosts of your virtual machine? 不确定我是否正确理解了您的问题,但我想您的问题是:您的虚拟机的/ etc / hosts中的主机名是什么? That is because they don't exist in the DNS and if you are connecting to these hosts it needs to end up at the right place and that, in this case is the VM itself. 这是因为它们不存在于DNS中,如果您要连接到这些主机,它需要最终到达正确的位置,在这种情况下是VM本身。

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

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