VirtualBox on Server

I wanted to create a headless VirtualBox on a remote server. I did this before but didn’t remember the correct sequence, so I thought I’d better save the work for the next time and write my steps down:

VMNAME="newvm"
mkdir -p "/opt/VirtualBox VMs/${VMNAME}"
cd "/opt/VirtualBox VMs/${VMNAME}"
# You may have to change the next line:
wget -nc http://releases.ubuntu.com/18.04/ubuntu-18.04-desktop-amd64.iso
# Create a disk size that would fit your operation system. Here I chose 32768 MB = 32 GB.
VBoxManage createhd --filename "${VMNAME}".vdi --size 32768
# Create the VM itself. The available ostypes can be listed by issuing 'VBoxManage list ostypes'
VBoxManage createvm --name "${VMNAME}" --ostype "Ubuntu_64" --register
# Add a SATA controller with the dynamic disk attached.
VBoxManage storagectl "${VMNAME}" --name "SATA Controller ${VMNAME}" --add sata --controller IntelAHCI
VBoxManage storageattach "${VMNAME}" --storagectl "SATA Controller ${VMNAME}" --port 0 --device 0 --type hdd --medium ${VMNAME}.vdi
# Add an IDE controller with a DVD drive attached, and the install ISO inserted into the drive:
VBoxManage storagectl "${VMNAME}" --name "IDE Controller ${VMNAME}" --add ide
VBoxManage storageattach "${VMNAME}" --storagectl "IDE Controller ${VMNAME}" --port 0 --device 0 --type dvddrive --medium ubuntu-18.04-live-server-amd64.iso
# Memory and graphics settings
VBoxManage modifyvm "${VMNAME}" --memory 4096 --vram 128
# If you want the network to be nat'ed:
VBoxManage modifyvm "${VMNAME}" --nic1 nat
 
# Disable audio
VBoxManage modifyvm "${VMNAME}" --audio none
 
# Enable ssh-access
VBoxManage modifyvm "${VMNAME}" --natpf1 "guestssh,tcp,127.0.0.1,60022,,22"
# If you ever want to remove this:
# VBoxManage modifyvm "${VMNAME}" --natpf1 delete "guestssh"
 
# Enable VRDE / You need this especially for the installation process
VBoxManage modifyvm "${VMNAME}" --vrde on
VBoxManage modifyvm "${VMNAME}" --vrdeaddress 127.0.0.1
 
# Extensionpack to access via RDP etc.
wget -nc https://download.virtualbox.org/virtualbox/5.2.12/Oracle_VM_VirtualBox_Extension_Pack-5.2.12.vbox-extpack
VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-5.2.12.vbox-extpack
 
 
# Bootup:
VBoxHeadless -s "${VMNAME}"
# It should be available via RDP on local port 3389.
 
# Shutdown (three methods, best to worst):
# 1.) Inside of the virtualbox
# 2.) virtual powerbutton
VBoxManage controlvm "${VMNAME}" acpipowerbutton
# 3.) virtual power off
VBoxManage controlvm "${VMNAME}" poweroff
 
# If the installation is complete, shutdown the VirtualBox and eject the iso:
VBoxManage storageattach "${VMNAME}" --storagectl "IDE Controller ${VMNAME}" --port 0 --device 0 --type dvddrive --medium none

If you want to secure your virtual machine even more (after logging into another openvpn, for example), you can use ufw, but remember that the connection comes from the VirtualBox Interface:

sudo ufw allow from 10.0.2.0/24 to any

Inspired by these posts on the internet:

  • https://www.perkin.org.uk/posts/create-virtualbox-vm-from-the-command-line.html
  • https://forums.virtualbox.org/viewtopic.php?f=6&t=65975
  • https://askubuntu.com/questions/42482/how-to-safely-shutdown-guest-os-in-virtualbox-using-command-line
  • http://arunnsblog.com/2010/07/20/nat-with-port-forwarding-for-virtual-box/
  • https://www.reddit.com/r/virtualbox/comments/5lze8p/remove_sound_card_with_vboxmanage/

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.