How to debug “Vagrant cannot forward the specified ports on this VM” message

后端 未结 13 2474
时光取名叫无心
时光取名叫无心 2020-12-25 10:33

I\'m trying to start a Vagrant instance and getting the following message:

Vagrant cannot forward the specified ports on this VM, since they
would collide wi         


        
相关标签:
13条回答
  • 2020-12-25 10:39

    If you use Proxifier (or a similar app) try closing it first. This was a problem I experienced due to Proxifier on OSX 10.9.

    0 讨论(0)
  • 2020-12-25 10:40

    The way out:

    1. $ vagrant suspend
    2. $ vagrant resume
    0 讨论(0)
  • 2020-12-25 10:41

    Vagrant.configure("2") do |config|

    config.vm.network "forwarded_port", guest: 80, host: 8080,

    auto_correct: true
    

    end

    The final :auto_correct parameter set to true tells Vagrant to auto correct any collisions. During a vagrant up or vagrant reload, Vagrant will output information about any collisions detections and auto corrections made, so you can take notice and act accordingly.

    https://www.vagrantup.com/docs/networking/forwarded_ports.html

    0 讨论(0)
  • 2020-12-25 10:43

    As message says, the port collides with the host box. I would simply change the port to some other value on the host machine. So if I am getting error for

    config.vm.forward_port 80, 1234
    

    then I would change it to

    config.vm.forward_port 80, 5656
    

    As 1234 might be used on my host machine.

    For actually inspecting ports on any machine, I use the tcpview utility for that OS and get to know which port is used where.

    0 讨论(0)
  • 2020-12-25 10:43

    Watch out, your Vagrantfile is not the only one being used when bringing up a Vagrant box/instance.

    When you get this:

    ~/dev/vagrant user$ vagrant reload
    Vagrant cannot forward the specified ports on this VM, since they
    would collide with some other application that is already listening
    on these ports. The forwarded port to 8001 is already in use
    on the host machine.
    
    To fix this, modify your current projects Vagrantfile to use another
    port. Example, where '1234' would be replaced by a unique host port:
    
      config.vm.network :forwarded_port, guest: 8001, host: 1234
    
    Sometimes, Vagrant will attempt to auto-correct this for you. In this
    case, Vagrant was unable to. This is usually because the guest machine
    is in a state which doesn't allow modifying port forwarding.
    ~/dev/vagrant user$ 
    

    You are actually not only using the Vagrantfile from ~/dev/vagrant but also the one from your "box" distribution .box file which is typically located here:

    ~/.vagrant.d/boxes/trusty/0/virtualbox/Vagrantfile
    

    And if you have a look at it you'll see it has plenty of default port mappings:

    $ cat ~/.vagrant.d/boxes//trusty/0/virtualbox/Vagrantfile
    $script = <<SCRIPT
    bzr branch lp:jujuredirector/quickstart /tmp/jujuredir
    bash /tmp/jujuredir/setup-juju.sh
    SCRIPT
    
    Vagrant.configure("2") do |config|
      # This Vagrantfile is auto-generated by 'vagrant package' to contain
      # the MAC address of the box. Custom configuration should be placed in
      # the actual 'Vagrantfile' in this box.
    
      config.vm.base_mac = "080027DFD2C4"
      config.vm.network :forwarded_port, guest: 22, host: 2122, host_ip: "127.0.0.1"
      config.vm.network :forwarded_port, guest: 80, host: 6080, host_ip: "127.0.0.1"
      config.vm.network :forwarded_port, guest: 8001, host: 8001, host_ip: "127.0.0.1"
      config.vm.network "private_network", ip: "172.16.250.15"
      config.vm.provision "shell", inline: $script
    
    end
    
    # Load include vagrant file if it exists after the auto-generated
    # so it can override any of the settings
    include_vagrantfile = File.expand_path("../include/_Vagrantfile", __FILE__)
    load include_vagrantfile if File.exist?(include_vagrantfile)
    

    So, go ahead and edit this file to remove the offending colliding forwarding port(s):

      config.vm.network :forwarded_port, guest: 22, host: 2122, host_ip: "127.0.0.1"
      config.vm.network :forwarded_port, guest: 80, host: 6080, host_ip: "127.0.0.1"
      # config.vm.network :forwarded_port, guest: 8001, host: 8001, host_ip: "127.0.0.1"
    

    By:

    ~/dev/vagrant user$ cp ~/.vagrant.d/boxes//trusty/0/virtualbox/Vagrantfile ~/.vagrant.d/boxes//trusty/0/virtualbox/Vagrantfile.old
    ~/dev/vagrant user$ vi ~/.vagrant.d/boxes//trusty/0/virtualbox/Vagrantfile
    

    and watch out for other Vagrantfiles inclusion i.e.:

    include_vagrantfile = File.expand_path("../include/_Vagrantfile", __FILE__)
    

    And now it works:

    $ vagrant up
    Bringing machine 'default' up with 'virtualbox' provider...
    ==> default: Importing base box 'trusty'...
    ==> default: Matching MAC address for NAT networking...
    ==> default: Setting the name of the VM: vagrant_default_1401234565101_12345
    ==> default: Clearing any previously set forwarded ports...
    ==> default: Clearing any previously set network interfaces...
    ==> default: Preparing network interfaces based on configuration...
        default: Adapter 1: nat
        default: Adapter 2: hostonly
    ==> default: Forwarding ports...
        default: 22 => 2122 (adapter 1)
        default: 80 => 6080 (adapter 1)
        default: 22 => 2222 (adapter 1)
    ==> default: Running 'pre-boot' VM customizations...
    ==> default: Booting VM...
    ==> default: Waiting for machine to boot. This may take a few minutes...
        default: SSH address: 127.0.0.1:2222
        default: SSH username: vagrant
        default: SSH auth method: private key
        default: Warning: Connection timeout. Retrying...
    ==> default: Machine booted and ready!
    ==> default: Checking for guest additions in VM...
    ==> default: Configuring and enabling network interfaces...
    ==> default: Mounting shared folders...
        default: /vagrant => /Home/user/dev/vagrant/vagrant-docker
    ==> default: Running provisioner: shell...
        default: Running: inline script
    ...
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-25 10:45

    I fixed it this way:

    1. vagrant suspend
    2. Close Project on RubyMine IDE
    3. vagrant resume
    4. Open Recent on RubyMine IDE
    0 讨论(0)
提交回复
热议问题