15

I know how to configure NAT-based port forwarding using the VirtualBox graphical interface (as in the example below).

How can I do the same via the command line?

Port forwarding via VirtualBox GUI

3 Answers 3

25

If the machine is powered off, you may use the modifyvm command:

VBoxManage modifyvm "VM name" --natpf1 "guestssh,tcp,,2222,,22"

If it's powered on, controlvm.

1
  • 1
    Thanks for the natfp<nic number> on controlvm. That helps automating the setup of my VMs. Apr 18, 2018 at 11:28
7

On VirtualBox 6+, --natpf1 is not supported. @Lyndsy Simon's answer has just one change (--natpf1 is just changed to simply natpf1). Changed command looks like this:

VBoxManage controlvm "VM name" natpf1 "guestssh,tcp,,2222,,22" 

Obviously I am making a change in the running VM so I used controlvm. If the VM is power off mode, use modifyvm instead.

0

Now, with VirtualBox 7+, the command-line changed again. It is:

VBoxManage modifyvm "VM name" --nat-pf1="guestssh,tcp,,2222,,22"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .