106

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?

11 Answers 11

92

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.

  • 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 '10 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. – developmentalinsanity Apr 28 '10 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 '15 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 '15 at 9:06
63

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
    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 '17 at 8:49
23

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
  • vm must be listening to ssh connections, right? It may be worthy making this explicit. – Matheus Santana Mar 25 '18 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).

  • 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 &. – Andrew J. Brehm Apr 28 '10 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 '10 at 12:20
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

2

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).

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy