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

时间:2012-06-08 17:01:57

标签: 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

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

谢谢, 凯文

13 个答案:

答案 0 :(得分:60)

您可以通过运行

查看计算机上正在运行的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

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

ps -ef |grep VBox

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

答案 1 :(得分:19)

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

当你得到这个:

~/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$ 

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

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

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

$ 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)

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

  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"

人:

~/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

并注意包含其他Vagrantfiles,即:

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

现在它起作用了:

$ 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
...

希望这有帮助。

答案 2 :(得分:13)

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

config.vm.forward_port 80, 1234

然后我会把它改成

config.vm.forward_port 80, 5656

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

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

答案 3 :(得分:9)

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

lsof -i | grep LISTEN 

输出

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)

答案 4 :(得分:5)

另请注意(至少在Vagrant 1.6.4中)有文件夹~/.vagrant.d/data/fp-leases,文件名称为80808081等。删除此文件夹内容帮助我现在

答案 5 :(得分:1)

如果您使用Proxifier(或类似的应用程序),请先尝试关闭它。由于OSX 10.9上的Proxifier,我遇到了这个问题。

答案 6 :(得分:0)

我遇到此问题是因为我有一个试图运行Postgres的VM,并且我的Postgres在本地计算机上的端口5432上运行。

vagrant resume之后,我得到了错误:

  

Vagrant无法转发此VM上的指定端口,因为它们   会与其他正在监听的应用程序发生冲突   在这些端口上。转发到5432的端口已在使用中   在主机上。

查看端口5432上正在运行什么:

o-ets-webdeveloper:portal me$ lsof -i :5432
COMMAND   PID     USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
postgres 1389     me    5u  IPv6 0x681a62dc601cf1e3      0t0  TCP localhost:postgresql (LISTEN)
postgres 1389     me    6u  IPv4 0x681a62dc6499362b      0t0  TCP localhost:postgresql (LISTEN)

原来是本地的Postgres,杀死了这些进程使我能够成功运行vagrant resume

答案 7 :(得分:0)

我以这种方式解决了这个问题

  1. vagrant suspend
  2. 在RubyMine IDE上关闭项目
  3. vagrant resume
  4. 在RubyMine IDE上最近打开

答案 8 :(得分:0)

我的观察: 我没有在端口8000上运行任何进程,因此端口转发基本上不起作用。 固定: 菲尔的答案提供了解决方案

~/.vagrant.d/boxes/ 

上面的路径具有列出端口8000的其他版本的vagrant文​​件。使用以下命令将它们全部修剪后,便能够成功运行vagrant。

vagrant box remove [name] --all

答案 9 :(得分:0)

出路:

  1. $无业游民暂停
  2. $无业游民的简历

答案 10 :(得分:0)

Vagrant.configure(“ 2”)做| config |

config.vm.network“转发端口”,访客:80,主机:8080,

auto_correct: true

结束

最后的:auto_correct参数设置为true,告诉Vagrant自动更正任何碰撞。在无业游民补给或无业游民重新装填期间,Vagrant将输出有关任何碰撞检测和自动更正的信息,因此您可以注意到并采取相应行动。

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

答案 11 :(得分:0)

您必须使用以下命令在当前目录中修改Vagrant文​​件:

config.vm.network "forwarded_port", guest: 4567, host: <a port not used by your host machine>

请记住,还有一个隐藏文件夹(.vagrant.d /),其中包含您的流浪者环境的设置以及框的配置文件。通常,此文件夹位于您的主目录中。

例如

~/.vagrant.d/boxes/<your_box_name>/0/virtualbox/Vagrantfile

通常,该文件包括位于~/.vagrant.d/boxes/<your_box_name>/0/virtualbox/include/_Vagrantfile

中的另一个Vagrant文​​件

您还必须使用port-forwarding命令修改此文件

答案 12 :(得分:-1)

在这里参考我的答案:https://superuser.com/a/1610804/1252585

再次写内容:

要列出所有的侦听端口:

$ netstat -a

使用以下命令查找在所需端口上运行的进程的进程ID:

$ netstat -ano | findstr :8080

结果将显示为:

$ netstat -ano | findstr :5000
  TCP    0.0.0.0:5000           0.0.0.0:0              LISTENING       18024

此处,18024是PID或进程ID。

然后使用以下命令杀死8080上的进程:

$ taskkill /PID 18024 /F

$ taskkill //PID 18024 //F

结果将显示为:

$ taskkill //PID 18024 //F
SUCCESS: The process with PID 18024 has been terminated.
相关问题