Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VirtualBox port forwarding with Packer

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?

like image 259
Matthias Braun Avatar asked Jan 06 '15 11:01

Matthias Braun


1 Answers

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.

like image 176
Matthias Braun Avatar answered Sep 28 '22 00:09

Matthias Braun