LIST natpf rules in Virtualbox/Vagrant

2019-03-12 15:53发布

I often get errors like this when running Vagrant:

VBoxManage: error: A NAT rule of this name already exists
VBoxManage: error: Details: code NS_ERROR_INVALID_ARG (0x80070057), component     NATEngine, interface INATEngine, callee nsISupports
VBoxManage: error: Context: "AddRedirect(Bstr(strName).raw(), proto,    Bstr(strHostIp).raw(), RTStrToUInt16(strHostPort), Bstr(strGuestIp).raw(),       RTStrToUInt16(strGuestPort))" at line 1524 of file VBoxManageModifyVM.cpp

I'd like to remove all port forwarding rules before doing vagrant up, but I have trouble LISTING natpf rules. Is there any way to do it using vboxmanage or via some facilities in Vagrant?

Update: Vagrant version 1.3.4. I can replicate the problem as follows: start vm installation normal way (vagrant up) and force power off the vm during installation (this simulates e.g. failed install). Then the natpf1 port forwarding rule is left in the system. The only way to clean it up is like vboxmanage modifyvm #{vmid} --natpf1 delete rule_name, but you have to know the rule name beforehand... Furthermore, the rule stays there after vagrant destroy and it seems that my Ruby natpf1 clearing function present in Vagrant.configure is not ran, which means that the stale rule still clashes with "fresh" one that Vagrant attempts to create.

4条回答
2楼-- · 2019-03-12 16:04

This got the job done for me:

VBoxManage showvminfo $VM_NAME --machinereadable | awk -F '[",]' '/^Forwarding/ { printf ("Rule %s host port %d forwards to guest port %d\n", $2, $5, $7); }'
查看更多
神经病院院长
3楼-- · 2019-03-12 16:05

I came here with the same problem; with your hint about deleting the rule I found that you can use the VirtualBox GUI to find the rules and delete them.

Of course, this only works when you are working on a machine with a GUI desktop.

  • Open the VirtualBox manager
  • Open the settings for the box in question (rmb -> settings, or the gear icon)
  • Select Network from the list on the left and open the Port Forwarding dialogue

From here you'll be able to directly remove the rules.

http://i.stack.imgur.com/6fQQc.png


Looking at the rules, it seems they just get a name that is equal to the port being set. So you can also look at the Vagrantfile, and search for a line like this:

db.vm.network :forwarded_port, guest: 5432, host: 5432

And guess that the name of the rule will be 5432. The name of the rule for forwarding the ssh port 22, is called ssh

$ vboxmanage modifyvm "vbox-id" --natpf1 delete "5432"
查看更多
姐就是有狂的资本
4楼-- · 2019-03-12 16:06

You can delete a rule from the command line by issuing:

VBoxManage controlvm "boot2docker-vm" natpf1 delete "tcp-port80"

the last parameter in quotes is the rule name you wish to delete.

查看更多
Melony?
5楼-- · 2019-03-12 16:16

You can list the nat rules by the following command:

VBoxManage showvminfo #{vmid}

You then get a lot of information about your VM including the forwarding rules, for example:

NIC 1 Rule(1):   name = ssh, protocol = tcp, host ip = 127.0.0.1, host port = 2022, guest ip = , guest port = 22
查看更多
登录 后发表回答