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:

mkdir -p "/opt/VirtualBox VMs/${VMNAME}"
cd "/opt/VirtualBox VMs/${VMNAME}"
# You may have to change the next line:
wget -nc
# 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,,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
# Extensionpack to access via RDP etc.
wget -nc
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 to any

Inspired by these posts on the internet:


Schreibe einen Kommentar