127

I was wondering if it's possible to run an Ubuntu virtual machine using VirtualBox in background, I mean, without any window open.

The idea is to connect via SSH to the Linux host, that would be running in background.

Has anyone ever done something like this? Is it possible?

5

11 Answers 11

109

VBoxManage startvm $VM --type headless will start the specified virtual machine in the background.

To shut it down, request the shut down from the guest.

4
  • You will only be able to SSH into the machine if you've setup networking properly. I think you need to use bridged so that the VM and your machine are on the same network. Also, VirtualBox will still run the gui of the target OS and you can connect to it via remote desktop, "rdesktop" (get from apt since you're in ubuntu)
    – basszero
    Apr 28, 2010 at 11:36
  • 1
    True. I assume that the VM is run usually through the VirtualBox GUI first to get it installed and configured. Only use VBoxManage once everything (including networking) works. Apr 28, 2010 at 11:48
  • With other VM software, I can close the window so it isn't always open and have it continue to run in the background, but open the window when I want to interface with it. Is this what this does?
    – jfa
    Jul 1, 2015 at 13:13
  • Make sure to ssh to the machine without -X or -Y since those seem to make the above command attach the process to the shell, so if you exit, the VM aborts.
    – moritz
    Aug 28, 2015 at 9:06
73

This is built into VirtualBox 4.2.

Simply hold down Shift when launching the VM from the Manager.

https://blogs.oracle.com/fatbloke/entry/what_s_new_in_oracle

1
  • 2
    Update : in VirtualBox 5.1 you even have a drop-down start menu just right of the start button in the manager. This menu is available only if the VM is stopped.
    – Titou
    Mar 27, 2017 at 8:49
25

Absolutely! You are looking for a headless installation (Virtualbox). The way to start the machine from the command line is something like:

VBoxHeadless --startvm Debian --vrdp=off

However you will need some way to connect. What I do is mapping ports between guest and host. With this configuration, your host 2222 port will be mapped to port 22 of your guest machine.

VBoxManage setextradata "Debian" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/Protocol" TCP  
VBoxManage setextradata "Debian" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/GuestPort" 22  
VBoxManage setextradata "Debian" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/HostPort" 2222  

After that you can log in with:

ssh localhost -p2222
1
  • vm must be listening to ssh connections, right? It may be worthy making this explicit. Mar 25, 2018 at 14:47
3

an alternative to VBoxManage startvm "{VMName}" --type headless is
VBoxHeadless -startvm "{VMName}"

Amusingly enough, I just finished figuring out how to run a VM headless, over RDP.

Note - At least on windows, it will block your command window. If you need to continue using your console window, open another to start the VM in.

To shut the VM down, you request the Guest OS to shutdown. VBoxHeadless releases the console when the Guest is fully shut down (You can Ctrl+C, but I think it may be analogus to a hard reset on a real machine).

2
  • The fact that vboxheadless blocks the console very much defeats the purpose of a headless mode. On Mac OS X (and presumably on Linux and Solaris) the headless mode works when you send vboxheadless into the background using &. Apr 28, 2010 at 11:48
  • I'd imagine there is a way to do that on windows, I just haven't found it. In any event, a headless vm certainly uses less resources on the server computer than one running a full GUI.
    – Fake Name
    Apr 28, 2010 at 12:20
3

To start in headless mode using GUI:

Hold the Shift key when starting the VM.

After that you will also be able to connect and disconnect from the GUI (Using Show and Machine -> Detach GUI options).

2

I'm using VBoxTool from http://vboxtool.sourceforge.net/ and it works perfectly for me. It can autostart VMs at boot and stop/save VMs at shutdown and provides a simple clean command line interface.

2

I don't think seamless mode is quite what he's looking for here, I think the proper thing would be VBoxHeadless which details of you can find in the manual. It runs using a command-lin interface and would be used, for example, if a virtual machine was running on a server but the display was not wanted from the server. You will be able to remote desktop onto the machine (providing you know how to set the option via command line).

I would reccomend asking this in the virtualbox forums. Actually, do a search first as I expect this has already been asked.


VBoxManage was the right thing, sorry. Link the info in the manual http://www.virtualbox.org/manual/ch08.html

1

I found that the headless mode seems to work fine when backgrounding in Linux (CentOS 5.6), but you lose access to the RDP functionality as long as the process is backgrounded. Do an 'fg' to bring the process back to the foreground, and RDP functionality is restored. Looks like starting the vm headless is best accomplished through an init script, where interactivity with the console is not required.

1

You can use VBoxHeadless or (and I wonder why nobody mentioned this before me) you can simply select one of start-up options, which will run the VM without any GUI.

Either hold shift and start the machine, or right click the machine and search for something like "Run without output" under item "Run"

0

I've added these lines into my .bashrc:

VM='anakim'
alias vm='VBoxManage startvm $VM --type headless'
alias sshvm='ssh -p2222 localhost'

For ssh access you don't need to change the configuration just keep the NAT and you can set up port forwarding table as following:

Name      | Protocol | Host Port | Guest Port
guestssh  | TCP      | 2222      | 22
localhost | TCP      | 8080      | 80

When you insert localhost:8080 into your machine browser the page from VM (on port 80) will be opened.

0

If the VM is running and the screen is showing, you may hide it by doing the following steps.

enter image description here

enter image description here

You must log in to answer this question.