LIST natpf rules in Virtualbox/Vagrant

后端 未结 5 1934
执念已碎
执念已碎 2021-02-04 06:13

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_AR         


        
相关标签:
5条回答
  • 2021-02-04 06:33

    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"
    
    0 讨论(0)
  • 2021-02-04 06:35

    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.

    0 讨论(0)
  • 2021-02-04 06:39

    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); }'
    
    0 讨论(0)
  • 2021-02-04 06:39

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

    for vm in `vboxmanage list vms | awk -F'"' '$0=$2'`
    do
        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'
    done
    
    0 讨论(0)
  • 2021-02-04 06:44

    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
    
    0 讨论(0)
提交回复
热议问题