I'm creating a VirtualBox image using Packer.
Afterwards I start the VM and I'd like to use ssh to connect to it. I know how to enable port forwarding using the GUI but I prefer to automate things, so I'm looking for a way to let Packer do that for me.
I'm using NAT as the way of connecting my VM to the network.
How do I tell Packer to forward some ports to the VM?
After having a look at how to enable port forwarding using VirtualBox' commandline tool VBoxManage, I came up with this configuration in my packerConfig.json
:
"type": "virtualbox-iso",
"vboxmanage": [
[ "modifyvm", "{{.Name}}", "--memory", "1024" ],
[ "modifyvm", "{{.Name}}", "--cpus", "1" ],
[ "modifyvm", "{{.Name}}", "--natpf1", "guest_ssh,tcp,,3022,,22" ]
]
...
The last part makes VirtualBox forward traffic from the host's 3022 to the guest's 22.
This means I can do ssh -p 3022 [email protected]
to connect to the VM.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With