【问题标题】:How to set up NAT with Vagrant如何使用 Vagrant 设置 NAT
【发布时间】:2014-01-28 19:37:36
【问题描述】:

我目前正在使用 VMWare Workstation Provider 插件运行最新版本的 Vagrant(尽管我认为第二部分不相关)。我正在为我的开发工作(即 NGINX 或 Rails 服务器)运行网络服务器,以便我可以通过主机(现在是 Windows 8)的 IP(或 DNS 欺骗域名)访问它们。在我当前的设置中,它分配了一个我无法从来宾计算机外部访问的192.168.xxx.xxx IP 地址。我当前的 Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # All Vagrant configuration is done here. The most common configuration
  # options are documented and commented below. For a complete reference,
  # please see the online documentation at vagrantup.com.

  # Every Vagrant virtual environment requires a box to build off of.
  config.vm.box = "vmwsbox"

  # The url from where the 'config.vm.box' box will be fetched if it
  # doesn't already exist on the user's system.
  # config.vm.box_url = "http://domain.com/path/to/above.box"

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # config.vm.network :forwarded_port, guest: 80, host: 8080

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  #config.vm.network "public_netwok"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  config.vm.network :public_network

  # If true, then any SSH connections made will enable agent forwarding.
  # Default value: false
  # config.ssh.forward_agent = true

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider :virtualbox do |vb|
  #   # Don't boot with headless mode
  #   vb.gui = true
  #
  #   # Use VBoxManage to customize the VM. For example to change memory:
  #   vb.customize ["modifyvm", :id, "--memory", "1024"]
  # end
  #
  # View the documentation for the provider you're using for more
  # information on available options.

  # Enable provisioning with Puppet stand alone.  Puppet manifests
  # are contained in a directory path relative to this Vagrantfile.
  # You will need to create the manifests directory and a manifest in
  # the file vmwsbox.pp in the manifests_path directory.
  #
  # An example Puppet manifest to provision the message of the day:
  #
  # # group { "puppet":
  # #   ensure => "present",
  # # }
  # #
  # # File { owner => 0, group => 0, mode => 0644 }
  # #
  # # file { '/etc/motd':
  # #   content => "Welcome to your Vagrant-built virtual machine!
  # #               Managed by Puppet.\n"
  # # }
  #
  # config.vm.provision :puppet do |puppet|
  #   puppet.manifests_path = "manifests"
  #   puppet.manifest_file  = "site.pp"
  # end

  # Enable provisioning with chef solo, specifying a cookbooks path, roles
  # path, and data_bags path (all relative to this Vagrantfile), and adding
  # some recipes and/or roles.
  #
  # config.vm.provision :chef_solo do |chef|
  #   chef.cookbooks_path = "../my-recipes/cookbooks"
  #   chef.roles_path = "../my-recipes/roles"
  #   chef.data_bags_path = "../my-recipes/data_bags"
  #   chef.add_recipe "mysql"
  #   chef.add_role "web"
  #
  #   # You may also specify custom JSON attributes:
  #   chef.json = { :mysql_password => "foo" }
  # end

  # Enable provisioning with chef server, specifying the chef server URL,
  # and the path to the validation key (relative to this Vagrantfile).
  #
  # The Opscode Platform uses HTTPS. Substitute your organization for
  # ORGNAME in the URL and validation key.
  #
  # If you have your own Chef Server, use the appropriate URL, which may be
  # HTTP instead of HTTPS depending on your configuration. Also change the
  # validation key to validation.pem.
  #
  # config.vm.provision :chef_client do |chef|
  #   chef.chef_server_url = "https://api.opscode.com/organizations/ORGNAME"
  #   chef.validation_key_path = "ORGNAME-validator.pem"
  # end
  #
  # If you're using the Opscode platform, your validator client is
  # ORGNAME-validator, replacing ORGNAME with your organization name.
  #
  # If you have your own Chef Server, the default validation client name is
  # chef-validator, unless you changed the configuration.
  #
  #   chef.validation_client_name = "ORGNAME-validator"
end

【问题讨论】:

    标签: vagrant


    【解决方案1】:

    假设您有适当的安全控制,您也可以在公共网络上为您的盒子分配一个静态 IP 地址:

       config.vm.network :private_network, ip: "192.168.33.10"
    

    【讨论】:

    • 感谢您的回答。我想出了如何做到这一点,我没有意识到,默认情况下,在主机的子网上创建了一个桥接适配器。
    【解决方案2】:

    我强烈建议您在公共网络上运行虚拟机。原因是 Vagrant 附带了一个默认的 SSH 密钥。这基本上意味着,如果您向外界开放您的虚拟机,任何人都可以通过 SSH 访问它并获得 root 访问权限。

    您可以在herehere 找到有关此安全问题的更多信息

    【讨论】:

    • 有道理,但是那我怎么能从虚拟机外部访问虚拟机呢?主要是:80 用于网络“东西”:3000 用于导轨。我想对我的主机文件进行编辑,所以基本上 rails.dev 将指向我的 VM 上的 rails 服务器。这是否更好地解释了@cocheese(顺便说一句,伟大的名字)?
    • 请赐教 :),但是您是否只想访问您的虚拟机主机,即您的开发机器?或者您的目标是远程开发工作并希望从家庭网络外部访问您的虚拟机?
    • 我只想从 VM 主机访问它们。我想通了,它实际上适用于默认网络设置,VMWare Workstation 只是发生了一些事情正在破坏事情。谢谢
    • 我想访问我的虚拟机进行远程开发。怎么做?谢谢你。 @cocheese
    • 好评论,但不是答案。
    猜你喜欢
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    相关资源
    最近更新 更多