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.

asked on Stack Overflow Feb 5, 2014 by LetMeSOThat4U • edited Feb 5, 2014 by LetMeSOThat4U

5 Answers


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); }'
answered on Stack Overflow Jan 7, 2016 by Andrew Wolfe

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.

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: :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"
answered on Stack Overflow Mar 28, 2014 by mhogerheijde

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 =, host port = 2022, guest ip = , guest port = 22
answered on Stack Overflow Dec 17, 2014 by Amyro

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.

answered on Stack Overflow Jul 2, 2015 by doron grinstein • edited Jul 2, 2015 by rink.attendant.6

Expanding on Andrew's answer, this lists the rules for all your VMs

for vm in `vboxmanage list vms | awk -F'"' '$0=$2'`
    echo "Rules for VM $vm"
    VBoxManage showvminfo $vm --machinereadable | awk -F '[",]' '/^Forwarding/ { printf ("Rule %s host port %-5d forwards to guest port %-5d\n", $2, $5, $7); }'
    printf '\n'
answered on Stack Overflow Aug 7, 2019 by Dominic Phillips • edited Aug 7, 2019 by Dominic Phillips

User contributions licensed under CC BY-SA 3.0