Sunday, May 13, 2018

Ubuntu Headless in VirtualBox

This guide is how to setup a Ubuntu 18.04 virtual machine using Oracle's VirtualBox, using the default NAT network and forwarding the SSH port for the machine to the localhost. After creating the new VM and installing the OS, note the IP address. By default Ubuntu normally has OpenSSH server installed and running. To verify, once you log in with the account created run

sudo service ssh status

You should see the output say Active: active (running). If the service is not install run

sudo apt install openssh-server

After determining that SSH is running, find the IP address of the machine and pick a random port that your workstation is not using and is easy to remember such as 2222. Click ‘Port Forwarding’, and create a new rule. Change 10.0.2.15 to match the IP address of your virtual machine.

You should now be able to run programs such as PuTTY or MobaXterm to connect over port 2222 to the virtual machine. After setting up the port forwarding, you can now run the machine in headless mode, because you're most likely not going to need the console (unless something goes terribly wrong).
Launching the machine in headless mode navigate to the VirtaulBox install directory (default is C:\Program Files\Oracle\VirtualBox) and Command Prompt run the command to list the virtual machines on your workstation
VBoxManage list vms
"ubuntu" {b2c0f1da-9047-5e2d-4ae4-af01def00645}
Start the VM in headless mode with startvm {VM Name} and the --type flag headless
VBoxManage startvm ubuntu --type headless
If everything worked as expected you can SSH to the machine using the localhost address 127.0.0.1 and port 2222. When your done, either shut it down though SSH or with VBoxManage
VBoxManage controlvm ubuntu poweroff
It's as easy as that. You can add more ports for testing if you need, or continue to use the virtual machine for other purposes if you prefer a full Bash versus the Windows-Linux Subsystem. Not to mention, you can use a different OS if you prefer (such as OpenSuSE, Debian, CentOS, Fedora, etc.)