如何调试"Vagrant无法转发此VM上的指定端口"消息

Kev*_*rke 41 vagrant

我正在尝试启动Vagrant实例并收到以下消息:

Vagrant cannot forward the specified ports on this VM, since they
would collide with another VirtualBox virtual machine's forwarded
ports! The forwarded port to 4567 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.forward_port 80, 1234
Run Code Online (Sandbox Code Playgroud)

我打开了VirtualBox,但此刻我没有任何运行的盒子,所以我很难过.如何确定哪个进程正在侦听4567?有没有办法列出我机器上运行的所有Vagrant盒子?

谢谢,凯文

小智 61

您可以通过运行查看计算机上正在运行的vagrant实例

$ vagrant global-status
id       name    provider   state   directory
----------------------------------------------------------------------
a20a0aa  default virtualbox saved   /Users/dude/Downloads/inst-MacOSX
64bc939  default virtualbox saved   /Users/dude/svn/dev-vms/ubuntu14
a94fb0a  default virtualbox running /Users/dude/svn/dev-vms/centos5
Run Code Online (Sandbox Code Playgroud)

如果您没有看到任何正在运行的虚拟机,那么您的冲突就不是流浪汉(流浪者知道).接下来要做的是启动VirtualBox UI,并检查它是否有任何实例在运行.如果您不想运行UI,您可以:

ps -ef |grep VBox
Run Code Online (Sandbox Code Playgroud)

如果您正在运行VirtualBox实例,则它们应包含在该输出中.您应该能够杀死其输出中包含VirtualBox的进程.一个问题是这些过程中的一个似乎存在以进行保持活动.刚刚杀掉最高的VirtualBox进程.如果您正在运行VirtualBox映像,但vagrant不知道它,则可能已手动删除了一些Vagrant目录,这意味着Vagrant将丢失对该实例的跟踪.

  • 如果你想用一行代码杀掉`VBox`进程:`ps -ef | grep VBox | awk'{print $ 2}'| xargs杀死` (8认同)
  • 在运行`vagrant global-status`后,运行`vagrant destroy -f <id>`来杀死它们 (2认同)

Phi*_* L. 19

注意,你的Vagrantfile 不是唯一一个在启动Vagrant盒子/实例时使用的.

当你得到这个:

~/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$ 
Run Code Online (Sandbox Code Playgroud)

您实际上不仅使用〜/ dev/vagrant中的Vagrantfile,还使用通常位于此处的"box"分发.box文件中的Vagrant文​​件:

~/.vagrant.d/boxes/trusty/0/virtualbox/Vagrantfile
Run Code Online (Sandbox Code Playgroud)

如果你看一下它,你会发现它有很多默认的端口映射:

$ 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)
Run Code Online (Sandbox Code Playgroud)

因此,请继续编辑此文件以删除有问题的冲突转发端口:

  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"
Run Code Online (Sandbox Code Playgroud)

通过:

~/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
Run Code Online (Sandbox Code Playgroud)

并注意其他Vagrantfiles包含,即:

include_vagrantfile = File.expand_path("../include/_Vagrantfile", __FILE__)
Run Code Online (Sandbox Code Playgroud)

现在它的工作原理:

$ 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
...
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.


Vis*_*ani 13

如消息所示,端口与主机盒发生冲突.我只是将端口更改为主机上的其他值.所以,如果我收到错误

config.vm.forward_port 80, 1234
Run Code Online (Sandbox Code Playgroud)

然后我会把它改成

config.vm.forward_port 80, 5656
Run Code Online (Sandbox Code Playgroud)

因为1234可能在我的主机上使用.

为了实际检查任何机器上的端口,我将该tcpview实用程序用于该操作系统,并了解在哪里使用哪个端口.


Ric*_*ber 9

我遇到了这个问题,事实证明RubyMine仍然坚持一个端口.我通过运行此命令找出了哪个应用程序正在保留端口(在我的情况下为31337):

lsof -i | grep LISTEN 
Run Code Online (Sandbox Code Playgroud)

产量

node       1396 richard.nienaber    7u  IPv4 0xffffff802808b320      0t0  TCP *:20559 (LISTEN)
Dropbox    1404 richard.nienaber   19u  IPv4 0xffffff8029736c20      0t0  TCP *:17500 (LISTEN)
Dropbox    1404 richard.nienaber   25u  IPv4 0xffffff8027870160      0t0  TCP localhost:26165 (LISTEN)
rubymine  11668 richard.nienaber   39u  IPv6 0xffffff8024d8e700      0t0  TCP *:26162 (LISTEN)
rubymine  11668 richard.nienaber   65u  IPv6 0xffffff8020c6e440      0t0  TCP *:31337 (LISTEN)
rubymine  11668 richard.nienaber  109u  IPv6 0xffffff8024d8df80      0t0  TCP localhost:6942 (LISTEN)
rubymine  11668 richard.nienaber  216u  IPv6 0xffffff8020c6ef80      0t0  TCP localhost:63342 (LISTEN)
Run Code Online (Sandbox Code Playgroud)

  • 当没有人在那个端口上听时该怎么办,但是流浪汉还在抱怨端口正在使用?怎么可能? (8认同)

hij*_*ian 6

还要注意的是(在流浪1.6.4至少)有文件夹~/.vagrant.d/data/fp-leases,具有类似名称的文件8080,8081等这个删除文件夹的内容帮我刚才.

  • 为了更深入地了解,这是虚拟机当前使用的端口,这可能会有所帮助的情况是,如果 vagrant 文件所在的目录在运行时被删除,vagrant 仍然会认为端口正在使用中,因为它没有执行虚拟机的拆卸,删除此文件将“释放”这些端口以供其他流浪机使用。 (3认同)