From 9216f43463a7517f17a00a1f64895cd6ed98c771 Mon Sep 17 00:00:00 2001 From: Moss Date: Thu, 19 Dec 2019 17:03:31 +0100 Subject: [PATCH] Configure NAT interface before forwarded port mapping --- builder/virtualbox/common/step_forward_ssh.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/builder/virtualbox/common/step_forward_ssh.go b/builder/virtualbox/common/step_forward_ssh.go index ecb0947e0..95bb93db8 100644 --- a/builder/virtualbox/common/step_forward_ssh.go +++ b/builder/virtualbox/common/step_forward_ssh.go @@ -63,9 +63,22 @@ func (s *StepForwardSSH) Run(ctx context.Context, state multistep.StateBag) mult s.l.Listener.Close() // free port, but don't unlock lock file sshHostPort = s.l.Port + // Make sure to configure the network interface to NAT + command := []string{ + "modifyvm", vmName, + "--nic1", + "nat", + } + if err := driver.VBoxManage(command...); err != nil { + err := fmt.Errorf("Failed to configure NAT interface: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + // Create a forwarded port mapping to the VM ui.Say(fmt.Sprintf("Creating forwarded port mapping for communicator (SSH, WinRM, etc) (host port %d)", sshHostPort)) - command := []string{ + command = []string{ "modifyvm", vmName, "--natpf1", fmt.Sprintf("packercomm,tcp,127.0.0.1,%d,,%d", sshHostPort, guestPort),