解决Vagrant cannot forward the specified ports on this VM

水深无声 2023-02-14 01:48 56阅读 0赞

问题

今天笔记本重启之后,vagrant up就一直提示下面的内容,而事实上3300端口并没有被占用,而且无论是修改转发端口,还是在Vagrantfile中设置auto_correct: true,是端口冲突时自动纠正,都会出现同样的问题。

  1. Vagrant cannot forward the specified ports on this VM, since they
  2. would collide with some other application that is already listening
  3. on these ports. The forwarded port to 3300 is already in use
  4. on the host machine.
  5. To fix this, modify your current project's Vagrantfile to use another
  6. port. Example, where '1234' would be replaced by a unique host port:
  7. config.vm.network :forwarded_port, guest: 8000, host: 1234
  8. Sometimes, Vagrant will attempt to auto-correct this for you. In this
  9. case, Vagrant was unable to. This is usually because the guest machine
  10. is in a state which doesn't allow modifying port forwarding. You could
  11. try 'vagrant reload' (equivalent of running a halt followed by an up)
  12. so vagrant can attempt to auto-correct this upon booting. Be warned
  13. that any unsaved work might be lost.

Vagrantfile配置文件是这样的

  1. Vagrant.configure("2") do |config|
  2. config.vm.box = "centos/7"
  3. config.vm.hostname = "nexus3"
  4. # config.vm.synced_folder "../data", "/vagrant_data"
  5. config.vm.network "forwarded_port", guest: 8000, host: 3300, protocol: "tcp"
  6. config.vm.network "private_network", type: "dhcp"
  7. config.vm.synced_folder "./", "/vagrant_data"
  8. # VirtualBox 的配置
  9. config.vm.provider "virtualbox" do |vb|
  10. # vagrant up启动时,是否自动打开virtual box的窗口,缺省为false
  11. # vb.gui = true
  12. #指定vm-name,也就是virtualbox管理控制台中的虚机名称
  13. vb.name = "nexus3"
  14. #指定vm内存,单位为MB
  15. vb.memory = "4000"
  16. #设置CPU个数
  17. vb.cpus = 2
  18. end
  19. end

解决

修改Vagrantfile配置文件,在转发端口的地方添加host_ip: "127.0.0.1"就可以了,修改配置文件的第5行如下:

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

发表评论

表情:
评论列表 (有 0 条评论,56人围观)

还没有评论,来说两句吧...

相关阅读