Copying text to terminal based VMs in VirtualBox

You are configuring a terminal only virtual machine using VirtualBox. At some point, you need to download a file using wget. The URL is 150 characters long, with the usual mix of underscores, dashes, slashes, upper and lower case letters, numbers… Or, even worse, you need to enter your extremely long and complex password without seeing what you type. And remember, you CANNOT use the clipboard, because there’s no such thing in bare terminal VMs.

Thanks to this comment in a VirtualBox forum thread, I found that there is a solution(-ish) for that problem, added to VirtualBox on version 5.2.0. See below:

# Type a string in the VM
$ VBoxManage controlvm <uuid|vmname> keyboardputstring <string1> [<string2> ...]

# Type the contents of a file
$ VBoxManage controlvm <uuid|vmname> keyboardputfile <filename>

# Type specific scan codes
$ VBoxManage controlvm <uuid|vmname> keyboardputscancode <hex> [<hex> ...]

Replace <uuid|vmname> with the respective information from your VM. The rest of the commands are self explanatory.

Things worth noting:

  • Only works with ASCII characters
  • For *nix based OSes, some escaping is required (\\ to actually type \)
  • \n won’t work for pressing enter or line breaks
  • Some specific keypresses need to be processed in a different way, such as pressing ENTER. It has to be explicitly pressed and released, using scan codes. See the example below:
$ VBoxManage controlvm VMNAME keyboardputstring ls -la
$ VBoxManage controlvm VMNAME keyboardputscancode 1c 9c

This types ls -la, presses ENTER with 1c and releases ENTER with 9c. For a nice list of available scan codes, look here.