解决Vagrant cannot forward the specified ports on this VM
问题
今天笔记本重启之后,vagrant up
就一直提示下面的内容,而事实上3300
端口并没有被占用,而且无论是修改转发端口,还是在Vagrantfile
中设置auto_correct: true
,是端口冲突时自动纠正,都会出现同样的问题。
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 3300 is already in use
on the host machine.
To fix this, modify your current project's Vagrantfile to use another
port. Example, where '1234' would be replaced by a unique host port:
config.vm.network :forwarded_port, guest: 8000, 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. You could
try 'vagrant reload' (equivalent of running a halt followed by an up)
so vagrant can attempt to auto-correct this upon booting. Be warned
that any unsaved work might be lost.
Vagrantfile
配置文件是这样的
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.hostname = "nexus3"
# config.vm.synced_folder "../data", "/vagrant_data"
config.vm.network "forwarded_port", guest: 8000, host: 3300, protocol: "tcp"
config.vm.network "private_network", type: "dhcp"
config.vm.synced_folder "./", "/vagrant_data"
# VirtualBox 的配置
config.vm.provider "virtualbox" do |vb|
# vagrant up启动时,是否自动打开virtual box的窗口,缺省为false
# vb.gui = true
#指定vm-name,也就是virtualbox管理控制台中的虚机名称
vb.name = "nexus3"
#指定vm内存,单位为MB
vb.memory = "4000"
#设置CPU个数
vb.cpus = 2
end
end
解决
修改Vagrantfile
配置文件,在转发端口的地方添加host_ip: "127.0.0.1"
就可以了,修改配置文件的第5
行如下:
config.vm.network "forwarded_port", guest: 8000, host: 3300, protocol: "tcp", host_ip: "127.0.0.1"
就可以正常使用了。
原理
host_ip (string)
:要绑定到端口转发的宿主机的 IP
。如果没有指定,会绑定到所有的 IP
。默认是空。
附:端口转发选项手册
这部分内容来自https://blog.csdn.net/kikajack/article/details/80030614
- auto_correct (boolean):如果为 true,当宿主机的端口因为被占用而发生冲突时会自动更换端口。默认是 false。
- guest (int):要暴露给宿主机的虚拟机的端口。可以是任何端口。
- guest_ip (string):要绑定到端口转发的虚拟机的 IP。如果没有设置这个选项,端口会使用所有的 IP 接口。默认是空。
- host (int):用于访问虚拟机的宿主机的端口。必须大于 1024,除非 vagrant 以 root 身份运行(不建议)。
- host_ip (string):要绑定到端口转发的宿主机的 IP。如果没有指定,会绑定到所有的 IP。默认是空。
- protocol (string):“udp”或“tcp”之一。指定端口转发允许的协议。默认是“tcp”。
- id (string):规则名称,VirtualBox 中可见。默认是“protocol”“guest”(例如:“tcp123”)。
参考
https://blog.csdn.net/kikajack/article/details/80030614
还没有评论,来说两句吧...