Search code examples
virtual-machinevirtualboxubuntu-16.04

Create headless Ubuntu VM on a Ubuntu server without GUI


I need to create a Ubuntu_64 virtual machine (VM1) on our lab server which is also a Ubuntu machine (U1), no GUI, to host my web server tool and the goal is to set it public for anyone to use without affecting our lab server. I successfully created VM1 with VirtualBox, but now I have no idea how to SSH from U1 to VM1, or from any other computer to VM1. If fact, I'm stuck at the step of "VM1 has been successfully started".

I've looked into several instructions, but most of them have the GUI version VirtualBox to configure VM.

Here are my main questions:

1) About VM1, how to get the IP address?

2) How to set username and login for VM1, say ssh [email protected], what is the "user"?

Below is my code for create VM1:

 VBoxManage createvm --name VM1 --ostype Ubuntu_64 --register --basefolder /pwd/
 VBoxManage modifyvm VM1 --ioapic on
 VBoxManage modifyvm VM1 --memory 1024 --vram 128
 VBoxManage modifyvm VM1 --bridgeadapter1 vboxnet0
 VBoxManage modifyvm VM1 --nic1 bridged
 VBoxManage createhd --filename `pwd`/VM1/VM1_DISK.vdi --size 80000 --format VDI
 VBoxManage storagectl VM1 --name "SATA Controller" --add sata --controller IntelAhci
 VBoxManage storageattach VM1 --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium  `pwd`/VM1/VM1_DISK.vdi
 VBoxManage storagectl VM1 --name "IDE Controller" --add ide --controller PIIX4
 VBoxManage storageattach VM1 --storagectl "IDE Controller" --port 1 --device 0 --type dvddrive --medium `pwd`/ubuntu-16.04.6-server-amd64.iso
 VBoxManage modifyvm VM1 --boot1 dvd --boot2 disk --boot3 none --boot4 none
 VBoxManage modifyvm VM1 --vrde on
 VBoxManage modifyvm VM1 --vrdemulticon on --vrdeport 10001
 VBoxManage startvm VM1 --type headless

Any suggestions would be appreciated!!


Solution

  • It's great that you're breaking this down so much and learning about how VirtualBox works. However, you should know automation frameworks exist to solve these problems. The commands you're running and the things you wish to configure like IP Addresses and Usernames can be easily configured in a file that is passed in a single command to a binary. For example Vagrant by HashiCorp.

    Vagrant allows you to provision a VM using Ruby syntax in a declarative Vagrantfile. I searched Google for an example of a Vagrant Box that leverages Ubuntu and provides a Desktop Environment and found this.

    So install Vagrant. Then run these commands:

    vagrant init dmhughes/ubuntu-18.04-desktop-gui \
      --box-version 0.0.1
    vagrant up
    

    Using Vagrant will allow you to do a lot of cool things. Pass around variables, declare multiple VMs, share secrets between them, create bridge networks between their virtual network interfaces, etc.