diff --git a/.web-docs/components/builder/iso/README.md b/.web-docs/components/builder/iso/README.md index 17fedb43..aa0c140b 100644 --- a/.web-docs/components/builder/iso/README.md +++ b/.web-docs/components/builder/iso/README.md @@ -862,6 +862,8 @@ used to do things such as set RAM, CPUs, etc. does not setup forwarded port mapping for communicator (SSH or WinRM) requests and uses ssh_port or winrm_port on the host to communicate to the virtual machine. +- `ssh_listen_address` (string) - The address where the SSH port forwarding will be set to listen on. This value defaults to `127.0.0.1`. + diff --git a/.web-docs/components/builder/ovf/README.md b/.web-docs/components/builder/ovf/README.md index 48443a91..0bfc3d12 100644 --- a/.web-docs/components/builder/ovf/README.md +++ b/.web-docs/components/builder/ovf/README.md @@ -623,6 +623,8 @@ boot time. does not setup forwarded port mapping for communicator (SSH or WinRM) requests and uses ssh_port or winrm_port on the host to communicate to the virtual machine. +- `ssh_listen_address` (string) - The address where the SSH port forwarding will be set to listen on. This value defaults to `127.0.0.1`. + diff --git a/.web-docs/components/builder/vm/README.md b/.web-docs/components/builder/vm/README.md index 18c452f6..ff67e565 100644 --- a/.web-docs/components/builder/vm/README.md +++ b/.web-docs/components/builder/vm/README.md @@ -640,6 +640,8 @@ boot time. does not setup forwarded port mapping for communicator (SSH or WinRM) requests and uses ssh_port or winrm_port on the host to communicate to the virtual machine. +- `ssh_listen_address` (string) - The address where the SSH port forwarding will be set to listen on. This value defaults to `127.0.0.1`. + diff --git a/builder/virtualbox/common/comm_config.go b/builder/virtualbox/common/comm_config.go index ceddba45..a05eb6d3 100644 --- a/builder/virtualbox/common/comm_config.go +++ b/builder/virtualbox/common/comm_config.go @@ -26,6 +26,8 @@ type CommConfig struct { // does not setup forwarded port mapping for communicator (SSH or WinRM) requests and uses ssh_port or winrm_port // on the host to communicate to the virtual machine. SkipNatMapping bool `mapstructure:"skip_nat_mapping" required:"false"` + // The address where the SSH port forwarding will be set to listen on. This value defaults to `127.0.0.1`. + SSHListenAddress string `mapstructure:"ssh_listen_address" required:"false"` // These are deprecated, but we keep them around for backwards compatibility // TODO: remove later @@ -72,5 +74,9 @@ func (c *CommConfig) Prepare(ctx *interpolate.Context) []error { errors.New("host_port_min must be less than host_port_max")) } + if c.SSHListenAddress == "" { + c.SSHListenAddress = "127.0.0.1" + } + return errs } diff --git a/builder/virtualbox/common/step_port_forwarding.go b/builder/virtualbox/common/step_port_forwarding.go index 6f94431c..f55877a5 100644 --- a/builder/virtualbox/common/step_port_forwarding.go +++ b/builder/virtualbox/common/step_port_forwarding.go @@ -28,10 +28,11 @@ import ( // // Produces: type StepPortForwarding struct { - CommConfig *communicator.Config - HostPortMin int - HostPortMax int - SkipNatMapping bool + CommConfig *communicator.Config + HostPortMin int + HostPortMax int + SkipNatMapping bool + SSHListenAddress string l *net.Listener } @@ -133,10 +134,11 @@ func (s *StepPortForwarding) Run(ctx context.Context, state multistep.StateBag) // Create a forwarded port mapping to the VM ui.Say(fmt.Sprintf("Creating forwarded port mapping for communicator (SSH, WinRM, etc) (host port %d)", commHostPort)) + command = []string{ "modifyvm", vmName, "--natpf1", - fmt.Sprintf("packercomm,tcp,127.0.0.1,%d,,%d", commHostPort, guestPort), + fmt.Sprintf("packercomm,tcp,%s,%d,,%d", s.SSHListenAddress, commHostPort, guestPort), } retried := false retry: diff --git a/builder/virtualbox/iso/builder.go b/builder/virtualbox/iso/builder.go index edb29f67..14265671 100644 --- a/builder/virtualbox/iso/builder.go +++ b/builder/virtualbox/iso/builder.go @@ -431,10 +431,11 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) }, new(vboxcommon.StepAttachFloppy), &vboxcommon.StepPortForwarding{ - CommConfig: &b.config.CommConfig.Comm, - HostPortMin: b.config.HostPortMin, - HostPortMax: b.config.HostPortMax, - SkipNatMapping: b.config.SkipNatMapping, + CommConfig: &b.config.CommConfig.Comm, + HostPortMin: b.config.HostPortMin, + HostPortMax: b.config.HostPortMax, + SkipNatMapping: b.config.SkipNatMapping, + SSHListenAddress: b.config.SSHListenAddress, }, &vboxcommon.StepVBoxManage{ Commands: b.config.VBoxManage, diff --git a/builder/virtualbox/iso/builder.hcl2spec.go b/builder/virtualbox/iso/builder.hcl2spec.go index 471630cd..2acd2468 100644 --- a/builder/virtualbox/iso/builder.hcl2spec.go +++ b/builder/virtualbox/iso/builder.hcl2spec.go @@ -105,6 +105,7 @@ type FlatConfig struct { HostPortMin *int `mapstructure:"host_port_min" required:"false" cty:"host_port_min" hcl:"host_port_min"` HostPortMax *int `mapstructure:"host_port_max" required:"false" cty:"host_port_max" hcl:"host_port_max"` SkipNatMapping *bool `mapstructure:"skip_nat_mapping" required:"false" cty:"skip_nat_mapping" hcl:"skip_nat_mapping"` + SSHListenAddress *string `mapstructure:"ssh_listen_address" required:"false" cty:"ssh_listen_address" hcl:"ssh_listen_address"` SSHHostPortMin *int `mapstructure:"ssh_host_port_min" required:"false" cty:"ssh_host_port_min" hcl:"ssh_host_port_min"` SSHHostPortMax *int `mapstructure:"ssh_host_port_max" cty:"ssh_host_port_max" hcl:"ssh_host_port_max"` SSHSkipNatMapping *bool `mapstructure:"ssh_skip_nat_mapping" required:"false" cty:"ssh_skip_nat_mapping" hcl:"ssh_skip_nat_mapping"` @@ -252,6 +253,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "host_port_min": &hcldec.AttrSpec{Name: "host_port_min", Type: cty.Number, Required: false}, "host_port_max": &hcldec.AttrSpec{Name: "host_port_max", Type: cty.Number, Required: false}, "skip_nat_mapping": &hcldec.AttrSpec{Name: "skip_nat_mapping", Type: cty.Bool, Required: false}, + "ssh_listen_address": &hcldec.AttrSpec{Name: "ssh_listen_address", Type: cty.String, Required: false}, "ssh_host_port_min": &hcldec.AttrSpec{Name: "ssh_host_port_min", Type: cty.Number, Required: false}, "ssh_host_port_max": &hcldec.AttrSpec{Name: "ssh_host_port_max", Type: cty.Number, Required: false}, "ssh_skip_nat_mapping": &hcldec.AttrSpec{Name: "ssh_skip_nat_mapping", Type: cty.Bool, Required: false}, diff --git a/builder/virtualbox/ovf/builder.go b/builder/virtualbox/ovf/builder.go index 06787077..e4756aa2 100644 --- a/builder/virtualbox/ovf/builder.go +++ b/builder/virtualbox/ovf/builder.go @@ -107,10 +107,11 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) }, new(vboxcommon.StepAttachFloppy), &vboxcommon.StepPortForwarding{ - CommConfig: &b.config.CommConfig.Comm, - HostPortMin: b.config.HostPortMin, - HostPortMax: b.config.HostPortMax, - SkipNatMapping: b.config.SkipNatMapping, + CommConfig: &b.config.CommConfig.Comm, + HostPortMin: b.config.HostPortMin, + HostPortMax: b.config.HostPortMax, + SkipNatMapping: b.config.SkipNatMapping, + SSHListenAddress: b.config.SSHListenAddress, }, &vboxcommon.StepVBoxManage{ Commands: b.config.VBoxManage, diff --git a/builder/virtualbox/ovf/config.hcl2spec.go b/builder/virtualbox/ovf/config.hcl2spec.go index 60db9220..16a0f385 100644 --- a/builder/virtualbox/ovf/config.hcl2spec.go +++ b/builder/virtualbox/ovf/config.hcl2spec.go @@ -95,6 +95,7 @@ type FlatConfig struct { HostPortMin *int `mapstructure:"host_port_min" required:"false" cty:"host_port_min" hcl:"host_port_min"` HostPortMax *int `mapstructure:"host_port_max" required:"false" cty:"host_port_max" hcl:"host_port_max"` SkipNatMapping *bool `mapstructure:"skip_nat_mapping" required:"false" cty:"skip_nat_mapping" hcl:"skip_nat_mapping"` + SSHListenAddress *string `mapstructure:"ssh_listen_address" required:"false" cty:"ssh_listen_address" hcl:"ssh_listen_address"` SSHHostPortMin *int `mapstructure:"ssh_host_port_min" required:"false" cty:"ssh_host_port_min" hcl:"ssh_host_port_min"` SSHHostPortMax *int `mapstructure:"ssh_host_port_max" cty:"ssh_host_port_max" hcl:"ssh_host_port_max"` SSHSkipNatMapping *bool `mapstructure:"ssh_skip_nat_mapping" required:"false" cty:"ssh_skip_nat_mapping" hcl:"ssh_skip_nat_mapping"` @@ -218,6 +219,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "host_port_min": &hcldec.AttrSpec{Name: "host_port_min", Type: cty.Number, Required: false}, "host_port_max": &hcldec.AttrSpec{Name: "host_port_max", Type: cty.Number, Required: false}, "skip_nat_mapping": &hcldec.AttrSpec{Name: "skip_nat_mapping", Type: cty.Bool, Required: false}, + "ssh_listen_address": &hcldec.AttrSpec{Name: "ssh_listen_address", Type: cty.String, Required: false}, "ssh_host_port_min": &hcldec.AttrSpec{Name: "ssh_host_port_min", Type: cty.Number, Required: false}, "ssh_host_port_max": &hcldec.AttrSpec{Name: "ssh_host_port_max", Type: cty.Number, Required: false}, "ssh_skip_nat_mapping": &hcldec.AttrSpec{Name: "ssh_skip_nat_mapping", Type: cty.Bool, Required: false}, diff --git a/builder/virtualbox/vm/builder.go b/builder/virtualbox/vm/builder.go index ac5b7bd2..8e22db25 100644 --- a/builder/virtualbox/vm/builder.go +++ b/builder/virtualbox/vm/builder.go @@ -91,10 +91,11 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) }, new(vboxcommon.StepAttachFloppy), &vboxcommon.StepPortForwarding{ - CommConfig: &b.config.CommConfig.Comm, - HostPortMin: b.config.HostPortMin, - HostPortMax: b.config.HostPortMax, - SkipNatMapping: b.config.SkipNatMapping, + CommConfig: &b.config.CommConfig.Comm, + HostPortMin: b.config.HostPortMin, + HostPortMax: b.config.HostPortMax, + SkipNatMapping: b.config.SkipNatMapping, + SSHListenAddress: b.config.SSHListenAddress, }, &vboxcommon.StepVBoxManage{ Commands: b.config.VBoxManage, diff --git a/builder/virtualbox/vm/config.hcl2spec.go b/builder/virtualbox/vm/config.hcl2spec.go index 6a0d10cf..d4ff9186 100644 --- a/builder/virtualbox/vm/config.hcl2spec.go +++ b/builder/virtualbox/vm/config.hcl2spec.go @@ -95,6 +95,7 @@ type FlatConfig struct { HostPortMin *int `mapstructure:"host_port_min" required:"false" cty:"host_port_min" hcl:"host_port_min"` HostPortMax *int `mapstructure:"host_port_max" required:"false" cty:"host_port_max" hcl:"host_port_max"` SkipNatMapping *bool `mapstructure:"skip_nat_mapping" required:"false" cty:"skip_nat_mapping" hcl:"skip_nat_mapping"` + SSHListenAddress *string `mapstructure:"ssh_listen_address" required:"false" cty:"ssh_listen_address" hcl:"ssh_listen_address"` SSHHostPortMin *int `mapstructure:"ssh_host_port_min" required:"false" cty:"ssh_host_port_min" hcl:"ssh_host_port_min"` SSHHostPortMax *int `mapstructure:"ssh_host_port_max" cty:"ssh_host_port_max" hcl:"ssh_host_port_max"` SSHSkipNatMapping *bool `mapstructure:"ssh_skip_nat_mapping" required:"false" cty:"ssh_skip_nat_mapping" hcl:"ssh_skip_nat_mapping"` @@ -216,6 +217,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "host_port_min": &hcldec.AttrSpec{Name: "host_port_min", Type: cty.Number, Required: false}, "host_port_max": &hcldec.AttrSpec{Name: "host_port_max", Type: cty.Number, Required: false}, "skip_nat_mapping": &hcldec.AttrSpec{Name: "skip_nat_mapping", Type: cty.Bool, Required: false}, + "ssh_listen_address": &hcldec.AttrSpec{Name: "ssh_listen_address", Type: cty.String, Required: false}, "ssh_host_port_min": &hcldec.AttrSpec{Name: "ssh_host_port_min", Type: cty.Number, Required: false}, "ssh_host_port_max": &hcldec.AttrSpec{Name: "ssh_host_port_max", Type: cty.Number, Required: false}, "ssh_skip_nat_mapping": &hcldec.AttrSpec{Name: "ssh_skip_nat_mapping", Type: cty.Bool, Required: false}, diff --git a/docs-partials/builder/virtualbox/common/CommConfig-not-required.mdx b/docs-partials/builder/virtualbox/common/CommConfig-not-required.mdx index ae818dae..de05f5bc 100644 --- a/docs-partials/builder/virtualbox/common/CommConfig-not-required.mdx +++ b/docs-partials/builder/virtualbox/common/CommConfig-not-required.mdx @@ -12,4 +12,6 @@ does not setup forwarded port mapping for communicator (SSH or WinRM) requests and uses ssh_port or winrm_port on the host to communicate to the virtual machine. +- `ssh_listen_address` (string) - The address where the SSH port forwarding will be set to listen on. This value defaults to `127.0.0.1`. +