Hi,
VirtualBox offers a very powerful command inferface: vboxmanage. With vboxmanage you can create new virtual machine, add and modify harddisk and much more. All the things you could do in the GUI, and a lot more are also possible at command line. In this post I will create, modify, control and delete a complete virtual machine.
At the bottom of the post the whole script can be downloaded.
I used a debian linux system for my tests. To install the latest VirtualBox version use the following commands:
echo "deb http://download.virtualbox.org/virtualbox/debian wheezy contrib non-free" >> /etc/apt/sources.list # Add Oracle Keyring wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | apt-key add - # Update repositories apt-get update # Install latest virtualbox version apt-get -y install virtualbox-4.3 # Installing the Extentionpack for USB2 and RDP support wget http://download.virtualbox.org/virtualbox/4.3.6/Oracle_VM_VirtualBox_Extension_Pack-4.3.6-91406.vbox-extpack VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-4.3.6-91406.vbox-extpack
I define some variables so you can adapt the following VBoxManage commands easy for your needs.
export VMFOLDER=/vm if [ ! -d "$VMFOLDER" ]; then mkdir VMFOLDER; fi export VIRTUALDSDA_OS=$VMFOLDER/hda.vdi export VIRTUALDSDB=$VMFOLDER/hdc.vdi export VIRTUALMACHINE_CONFIG_DIR=/etc/vbox export VIRTUALMACHINE_CERT_DIR=$VIRTUALMACHINE_CONFIG_DIR/tls if [ ! -d "$VIRTUALMACHINE_CERT_DIR" ]; then mkdir $VIRTUALMACHINE_CERT_DIR; fi export VIRTUALMACHINE_NAME="TestMachine" export VIRTUALMACHINE_MAC="0050563a2d1c" export VIRTUALMACHINE_RAM=1024 export VIRTUALMACHINE_MASSSTORAGE_CONTROLLER_NAME="SATA Controller" export VIRTUALMACHINE_MASSSTORAGE_CONTROLLER_DVD_NAME="IDE Controller" export VIRTUALMACHINE_SDA_OS_SIZE=512 export VIRTUALMACHINE_SDB_SIZE=1024 export VIRTUALMACHINE_RDP_PORT=3389 export VIRTUALMACHINE_RDP_USER=Admin export VIRTUALMACHINE_RDP_PASSWORD=AdminsPassword export VIRTUALMACHINE_RDP_ENCRYPT_CA_KEY=$VIRTUALMACHINE_CERT_DIR/cakey.pem export VIRTUALMACHINE_RDP_ENCRYPT_CA_CERT=$VIRTUALMACHINE_CERT_DIR/ca.cert export VIRTUALMACHINE_RDP_ENCRYPT_CA_PASSWORD=YourCAPassword export VIRTUALMACHINE_RDP_SERVER_KEY=$VIRTUALMACHINE_CERT_DIR/srvkey.pem export VIRTUALMACHINE_RDP_SERVER_CERT=$VIRTUALMACHINE_CERT_DIR/srv.cert export VIRTUALMACHINE_RDP_SERVER_SIGN_REQUEST=$VIRTUALMACHINE_CERT_DIR/srvreq.pem export VIRTUAL_FLOPPY_FILE_FREEDOS=/tmp/fdos1440.img
Create a new virtual machine for OS type linux
VBoxManage createvm --name "$VIRTUALMACHINE_NAME" --ostype Linux --register
Some modifications 🙂
- BIOS RTC uses UTC
- enable ACPI
- Memory 1024MB
- NIC 1 -> Network Bride mode on host interface eth0
- 2 Serial Interfaces, ttyS0 connected to host ttyS0, ttyS1 disconnected
VBoxManage modifyvm "$VIRTUALMACHINE_NAME" --memory $VIRTUALMACHINE_RAM --rtcuseutc on --acpi on --nic1 bridged --bridgeadapter1 eth0 --macaddress1 $VIRTUALMACHINE_MAC --uart1 0x3f8 4 --uart2 0x2f8 3 --uartmode2 disconnected --uartmode1 /dev/ttyS0
Or if you want to use NAT. Changing network parameters. MAC Address and Network to which the virtual machine should connect
VBoxManage modifyvm "$VIRTUALMACHINE_NAME" --macaddress1 $VIRTUALMACHINE_MAC --nic1 nat
Some DMI Information
Board Serial
VBoxManage setextradata "$VIRTUALMACHINE_NAME" "VBoxInternal/Devices/pcbios/0/Config/DmiSystemSerial" "string:YE123456"
The “virtual” BIOS Vendor
VBoxManage setextradata "$VIRTUALMACHINE_NAME" "VBoxInternal/Devices/pcbios/0/Config/DmiBIOSVendor" "BIOS Vendor"
BIOS Version String
VBoxManage setextradata "$VIRTUALMACHINE_NAME" "VBoxInternal/Devices/pcbios/0/Config/DmiBIOSVersion" "5.0.1.2"
BIOS Date
VBoxManage setextradata "$VIRTUALMACHINE_NAME" "VBoxInternal/Devices/pcbios/0/Config/DmiBIOSReleaseDate" "02/07/2014"
Bios Version
VBoxManage setextradata "$VIRTUALMACHINE_NAME" "VBoxInternal/Devices/pcbios/0/Config/DmiBIOSReleaseMajor" 5 VBoxManage setextradata "$VIRTUALMACHINE_NAME" "VBoxInternal/Devices/pcbios/0/Config/DmiBIOSReleaseMinor" 0 VBoxManage setextradata "$VIRTUALMACHINE_NAME" "VBoxInternal/Devices/pcbios/0/Config/DmiBIOSFirmwareMajor" 1 VBoxManage setextradata "$VIRTUALMACHINE_NAME" "VBoxInternal/Devices/pcbios/0/Config/DmiBIOSFirmwareMinor" 2
For more DMI settings see VirtualBox Manual, Chapter 9.12
If your virtual maschine should run in background and start if the host system starts
VBoxManage setextradata "$VIRTUALMACHINE_NAME" autostart headless
Create a new harddisk
VBoxManage createhd --filename "$VIRTUALDSDA_OS" --size $VIRTUALMACHINE_SDA_OS_SIZE --format VDI --variant fixed
and a second one
VBoxManage createhd --filename "$VIRTUALDSDB" --size $VIRTUALMACHINE_SDB_SIZE --format VDI --variant fixed
Add a SATA Massstorge controller
VBoxManage storagectl "$VIRTUALMACHINE_NAME" --add sata --controller IntelAHCI --name "$VIRTUALMACHINE_MASSSTORAGE_CONTROLLER_NAME"
Attach a harddisk to a controller
VBoxManage storageattach "$VIRTUALMACHINE_NAME" --storagectl "$VIRTUALMACHINE_MASSSTORAGE_CONTROLLER_NAME" --port 0 --device 0 --type hdd --medium $VIRTUALDSDA_OS
Attach the other disk
VBoxManage storageattach "$VIRTUALMACHINE_NAME" --storagectl "$VIRTUALMACHINE_MASSSTORAGE_CONTROLLER_NAME" --port 1 --device 0 --type hdd --medium $VIRTUALDSDB
Add a IDE Controller for DVDs
VBoxManage storagectl "$VIRTUALMACHINE_NAME" --add ide --controller PIIX3 --name "$VIRTUALMACHINE_MASSSTORAGE_CONTROLLER_DVD_NAME"
Add a DVD Drive, no medium
VBoxManage storageattach "$VIRTUALMACHINE_NAME" --storagectl "$VIRTUALMACHINE_MASSSTORAGE_CONTROLLER_DVD_NAME" --port 0 --device 0 --type dvddrive --medium emptydrive
Add a floppy drive
VBoxManage storagectl "$VIRTUALMACHINE_NAME" --add floppy --name "Floppy Controller"
Attaching a floppy image file
VBoxManage storageattach "$VIRTUALMACHINE_NAME" --storagectl "Floppy Controller" --device 0 --port 0 --type fdd --medium "$VIRTUAL_FLOPPY_FILE_FREEDOS"
The Virtual Machine is ready to install operating system. Mount a iso image
VBoxManage storageattach "$VIRTUALMACHINE_NAME" --storagectl "$VIRTUALMACHINE_MASSSTORAGE_CONTROLLER_DVD_NAME" --port 0 --device 0 --type dvddrive --medium /tmp/kali-linux.iso
And remove the “virtual” DVD
VBoxManage storageattach "$VIRTUALMACHINE_NAME" --storagectl "$VIRTUALMACHINE_MASSSTORAGE_CONTROLLER_DVD_NAME" --port 0 --device 0 --type dvddrive --medium emptydrive
Time to start the VM. If you have a X11 display active start a VM in foreground
VBoxManage startvm "$VIRTUALMACHINE_NAME"
Or if no display available start virtual machine in background
VBoxManage startvm "$VIRTUALMACHINE_NAME" --type headless
Enable RDP with simple authentication to view the console screen over network.
Note: RDP does only work when the extension pack is installed!
VBoxManage setproperty vrdeauthlibrary "VBoxAuthSimple" VBoxManage modifyvm $VIRTUALMACHINE_NAME --vrdeauthtype external --vrdeauthlibrary VBoxAuthSimple export PASSHASH=`VBoxManage internalcommands passwordhash $VIRTUALMACHINE_RDP_PASSWORD|sed -e 's/Password hash: //g'` VBoxManage setextradata $VIRTUALMACHINE_NAME "VBoxAuthSimple/users/"$VIRTUALMACHINE_RDP_USER $PASSHASH VBoxManage modifyvm $VIRTUALMACHINE_NAME --vrde on --vrdemulticon on --vrdeextpack default --vrdeport $VIRTUALMACHINE_RDP_PORT --vrdeauthtype external --vrdeauthlibrary VBoxAuthSimple
Now you can connect with the Remote Desktop Client to the console of the virtual machine.
Note: There is a strange behaviour when connecting with the Windows Remote Desktop Client. The checkbox “Allow to save my credentials” must be enabled otherwise no connection could established. Any ideas?
In a secure environment encrypt the RDP Session
Create a simple CA only for the reason to create TLS Certificate, its recommended to set a password the CA and the RDP Key
openssl req -new -x509 -days 18250 -extensions v3_ca -keyout $VIRTUALMACHINE_RDP_ENCRYPT_CA_KEY -out $VIRTUALMACHINE_RDP_ENCRYPT_CA_CERT
Create RDP Key
openssl genrsa -out $VIRTUALMACHINE_RDP_SERVER_KEY
Create signing request
openssl req -new -key $VIRTUALMACHINE_RDP_SERVER_KEY -out $VIRTUALMACHINE_RDP_SERVER_SIGN_REQUEST
Sign certifcate with the CA
openssl x509 -req -days 18250 -in $VIRTUALMACHINE_RDP_SERVER_SIGN_REQUEST -CA $VIRTUALMACHINE_RDP_ENCRYPT_CA_CERT -CAkey $VIRTUALMACHINE_RDP_ENCRYPT_CA_KEY -set_serial 01 -out $VIRTUALMACHINE_RDP_SERVER_CERT
# Add Certificate to the RDP Config and enable it
vboxmanage modifyvm $VIRTUALMACHINE_NAME --vrdeproperty "Security/CACertificate=$VIRTUALMACHINE_RDP_ENCRYPT_CA_CERT" vboxmanage modifyvm $VIRTUALMACHINE_NAME --vrdeproperty "Security/ServerCertificate=$VIRTUALMACHINE_RDP_SERVER_CERT" vboxmanage modifyvm $VIRTUALMACHINE_NAME --vrdeproperty "Security/ServerPrivateKey=$VIRTUALMACHINE_RDP_SERVER_KEY" vboxmanage modifyvm $VIRTUALMACHINE_NAME --vrdeproperty "Security/Method=negotiate"
Some other commands. Poweroff a VM
VBoxManage controlvm "$VIRTUALMACHINE_NAME" poweroff
Deleting a VM
VBoxManage unregistervm "$VIRTUALMACHINE_NAME" --delete
How to change the UUID of a virtual disk is descripted here.
Enjoy!
Michael