Search code examples
virtualboxvagrant

LIST natpf rules in Virtualbox/Vagrant


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.


Solution

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

    https://i.sstatic.net/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"