VirtualBox's little secret: the command line. Managing VirtualBox using the Virtualbox console running from the console

Sometimes there is a need to run a virtual machine on a host without X. I will tell you how to do this, having access to the host system only via ssh + rdp (Remote Desktop Protocol). I will describe the process for OC Ubuntu 9.10 as a host.

Let's start by installing VirtualBox.

You must first install the dkms (Dynamic Kernel Module Support Framework) package:

Sudo apt-get install dkms

The site offers 2 options: register the package source (deb download.virtualbox.org/virtualbox/debian karmic non-free) in /etc/apt/sources.list or download and install the deb package. When I registered the source and did sudo apt-get install virtualbox-3.1, I got a bunch of dependency packages (including some for the GUI interface). Therefore, it is better to download the deb package. Download and install:

Sudo dpkg -i virtualbox-3.1_3.1.0-55467_Ubuntu_karmic_i386.deb

Perhaps dependencies will also be required here (some libraries for parsing xml, in which configs are stored, but there are significantly fewer of them than in the first case). If the installation did not complete due to dependencies, you can simply do

Sudo apt-get -f install

This will install dependencies and VirtualBox

OK. Installed VirtualBox. Let's start creating guest machines.

Let's create the car itself:

VBoxManage createvm --name ubuntu --ostype Ubuntu --register
(name - machine name, ostype - system type. A complete list of all types can be found with the VBoxManage list ostypes command)

Setting it up

VBoxManage modifyvm ubuntu --memory 512 --floppy disabled --audio none --nic1 bridged --bridgeadapter1 eth0 --vram 4 --accelerate3d off --boot1 disk --acpi on --cableconnected1 on --usb off --vrdp on --vrdpport 3390

Everything is clear here. You can also specify NAT as the network type (--nic1 nat). also enable rdp

We create hdd disk for a virtual machine:

VBoxManage createhd --filename /home/user/vbox/ubuntu.vdi --size 20000 --register

Adding an IDE controller to our machine

VBoxManage storagectl ubuntu --name "IDE Controller" --add ide

We attach the previously created hdd to IDE0

VBoxManage storageattach ubuntu --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium /home/user/vbox/ubuntu.vdi

On IDE1 we attach the installation image

VBoxManage storageattach ubuntu --storagectl "IDE Controller" --port 1 --device 0 --type dvddrive --medium /home/user/vbox/iso/ubuntu-9.10-alternate-i386.iso

Telling the machine to boot from disk

VBoxManage modifyvm ubuntu --boot1 dvd

Let's start the car

In order to put basic system Let's use an rdp client (I have KDE, KRDC is included in the standard package). connect to the host machine on the port specified in the settings (--vrdpport 3390), install the system, do sudo apt-get install openssh-server . Now you can access the virtual machine via ssh

Stopping the virtual machine

VBoxManage controlvm ubuntu acpipowerbutton
via acpi

Or more harshly

VBoxManage controlvm ubuntu poweroff

We say it boots from the hdd

VBoxManage modifyvm ubuntu --boot1 disk

You can also unclip the installation disc

VBoxManage storageattach ubuntu --storagectl "IDE Controller" --port 1 --device 0 --medium none

And we start again

Nohup VBoxHeadless --startvm ubuntu &

More useful commands:

VBoxManage list runningvms
view all running machines

VBoxManage showvminfo ubuntu
viewing information about a virtual machine

Thus, on one machine with minimal installed system you can raise several virtual ones for various purposes and experiments


Posted by Ken Hess
Date of publication: January 18th, 2010
Translation: N. Romodanov
Translation date: February 2010

Regular VirtualBox users may not be aware of the enormous power that lies within the command line interface.

People usually ask: "Why use the command line when you have a perfectly good GUI?" The answer is the enormous power of the command line. Since 1995, there has been a whole generation of computer users who believe that a computer is a collection of windows and graphics, but in reality this is very far from the truth. Why, 1995, you ask? With the advent of Windows 95 in 1995, a new computer era was opened - the era of graphical interfaces. Shortly after that fateful August, FVWM95 was released, which was Linux's answer to the Windows 95 Explorer interface. It seems to me that in those good old days command line disappeared forever. Nobody wanted to use DOS anymore and they stopped being afraid of Linux. But the command line is still alive and well in the Linux environment. It's still quite alive in Windows. And now that Macs are Unix-based, they also have a lot of things that don't require a GUI.

It may come as a surprise to you that underneath VirtuaBox's pretty GUI, with its bright graphics, clear Settings page and large number of drop-down lists, lurks the occult world of the command line. The true power of this dark world will only be revealed to those who dare to enter these musty catacombs. Once you've gotten to grips with the VirtualBox command line, you might decide that the GUI is little more than just a pretty drapery designed for those who would otherwise just ignore the boring black rectangle of an actual running application.

The Power of the Command Line

The black rectangle I was talking about is the terminal window. The command line allows you to take full advantage of the power of automatic command execution. The only automation method that I know of in *nix systems is the use of scripts written in Perl, shell scripts, or scripts in some other language. And the system scheduler daemon (cron) will help you right time run the script. This is the power of the command line, which, thanks to the ability to run automatically, which also requires system administrators to be able to write scripts, is their skill set. In every vacancy that requires System Administrator, it will be indicated that the applicant must be able to write scripts in at least one of the interpreted languages ​​listed in the vacancy.

As for virtualization, the command line will allow you to quickly create virtual disks, add a new network interface, launch virtual machines and create new virtual machines, all without using a graphical interface. Automatic execution using scripts will also allow you to schedule the start and stop of virtual machines without your direct intervention. And, if you're a really great scripter, you can write a script that will guide you, with minimal intervention on your part, to create a virtual machine, run it, or install and change it. If you don't like doing everything manually, you'll make a list of the virtual machines you use all the time, then set up scripts (templates) for each type of virtual machine you use and, if necessary, create a virtual machine within seconds.

Let's look at simple and elegant functionality command line in VirtualBox.

Installation and main components

Use the VirtualBox Linux Downloads link to download the binary distribution designed for your system. If you want, you can download source. For the option I chose (for RHEL5/CentOS5 in rpm format), the following directories were created:

  • For binary modules - /usr/lib/virtualbox with links from /usr/bin
  • For libraries - /usr/lib/virtualbox
  • For source codes - /usr/share/virtualbox
  • For guest components (ISO) - /usr/share/virtualbox

If your distribution does not have latest version VirtualBox (version 3.1.2 at the time of this writing), then use one of the packages that can be downloaded, or download the source code.

Creating and configuring a virtual machine

The hardest thing about using the command line is knowing where to start. IN graphical interface you need to use the New button. The command line reveals its secrets a little more complexly. In order to create and configure a new virtual machine, you must open a terminal window, or use an ssh session to access the Linux system on which VirtualBox is installed, and run the following command:

$ /usr/bin/VBoxManage createvm --name Debian5 –register

This command creates a new virtual machine named Debian5, which registers with VirtualBox. The created virtual machine is XML file, which is located in the home directory ~/.VirtualBox/Machines/Debian5/Debian5.xml.

We create virtual disk for your virtual machine:

$ /usr/bin/VBoxManage createhd --filename Debian5.vdi --size 4000 --variant Fixed

You have created a virtual disk named Debian5.vdi with a size of 4 GB (4000 MB); the size is fixed, i.e. it does not increase dynamically.

The virtual disk is created as a file ~/.VirtualBox/HardDisks/Debian.vdi

Create a disk device controller to which you will attach the virtual disk and virtual CD/DVD device

$ /usr/bin/VBoxManage storagectl Debian5 --name "IDE Controller" --add ide --controller PIIX4

This command creates an IDE controller named “IDE Controller”, the controller type is PIIX4.

Now let's connect the virtual disk to your virtual machine:

$ /usr/bin/VBoxManage storageattach Debian5 --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium Debian5.vdi

Connecting ISO image that operating system you want to install:

$ /usr/bin/VBoxManage storageattach Debian5 --storagectl "IDE Controller" --port 0 --device 1 --type dvddrive --medium /ISO/debian-40r3-i386-netinst.iso

Setting up the network:

$ /usr/bin/VBoxManage modifyvm Debian5 --nic1 bridged --cableconnected1 on --bridgeadapter1 eth0

Using this command, a bridge NIC network interface is created: the cable is connected at startup Linux systems, adapter - eth0.

Let's start the virtual machine:

$ /usr/bin/VBoxManage startvm Debian5

If everything goes as planned, you will see the following message, and then you will see the virtual machine window ready for installation:

Waiting for the remote session to open... Remote session has been successfully opened.

You see, creating and configuring a virtual machine from the command line frees you from endless mouse clicks and opens up a whole new world of virtual machine automation possibilities. We haven't gone too deep here, but I hope this will pique your curiosity and encourage you to explore the enormous power that the command line has.

If you liked the article, share it with your friends:

Sometimes there is a need to run a virtual machine on a host without X. I will tell you how to do this, having access to the host system only via ssh + rdp (Remote Desktop Protocol). I will describe the process for OC Ubuntu 9.10 as a host.

Let's start by installing VirtualBox.

You must first install the dkms (Dynamic Kernel Module Support Framework) package:

Sudo apt-get install dkms

The site offers 2 options: register the package source (deb download.virtualbox.org/virtualbox/debian karmic non-free) in /etc/apt/sources.list or download and install the deb package. When I registered the source and did sudo apt-get install virtualbox-3.1, I got a bunch of dependency packages (including some for the GUI interface). Therefore, it is better to download the deb package. Download and install:

Sudo dpkg -i virtualbox-3.1_3.1.0-55467_Ubuntu_karmic_i386.deb

Perhaps dependencies will also be required here (some libraries for parsing xml, in which configs are stored, but there are significantly fewer of them than in the first case). If the installation did not complete due to dependencies, you can simply do

Sudo apt-get -f install

This will install dependencies and VirtualBox

OK. Installed VirtualBox. Let's start creating guest machines.

Let's create the car itself:

VBoxManage createvm --name ubuntu --ostype Ubuntu --register
(name - machine name, ostype - system type. A complete list of all types can be found with the VBoxManage list ostypes command)

Setting it up

VBoxManage modifyvm ubuntu --memory 512 --floppy disabled --audio none --nic1 bridged --bridgeadapter1 eth0 --vram 4 --accelerate3d off --boot1 disk --acpi on --cableconnected1 on --usb off --vrdp on --vrdpport 3390

Everything is clear here. You can also specify NAT as the network type (--nic1 nat). also enable rdp

Create a hdd disk for a virtual machine:

VBoxManage createhd --filename /home/user/vbox/ubuntu.vdi --size 20000 --register

Adding an IDE controller to our machine

VBoxManage storagectl ubuntu --name "IDE Controller" --add ide

We attach the previously created hdd to IDE0

VBoxManage storageattach ubuntu --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium /home/user/vbox/ubuntu.vdi

On IDE1 we attach the installation image

VBoxManage storageattach ubuntu --storagectl "IDE Controller" --port 1 --device 0 --type dvddrive --medium /home/user/vbox/iso/ubuntu-9.10-alternate-i386.iso

Telling the machine to boot from disk

VBoxManage modifyvm ubuntu --boot1 dvd

Let's start the car

In order to install the base system, we will use an rdp client (I have KDE, KRDC is included in the standard package). connect to the host machine on the port specified in the settings (--vrdpport 3390), install the system, do sudo apt-get install openssh-server . Now you can access the virtual machine via ssh

Stopping the virtual machine

VBoxManage controlvm ubuntu acpipowerbutton
via acpi

Or more harshly

VBoxManage controlvm ubuntu poweroff

We say it boots from the hdd

VBoxManage modifyvm ubuntu --boot1 disk

You can also unclip the installation disc

VBoxManage storageattach ubuntu --storagectl "IDE Controller" --port 1 --device 0 --medium none

And we start again

Nohup VBoxHeadless --startvm ubuntu &

More useful commands:

VBoxManage list runningvms
view all running machines

VBoxManage showvminfo ubuntu
viewing information about a virtual machine

Thus, on one machine with a minimally installed system, you can raise several virtual ones for various purposes and experiments

Virtualization...virtualization...
Nowadays everyone is trying to squeeze as much as possible out of their hardware resources. Having several separate computers with different operating systems is a little expensive and not all organizations will do it. But there is a way out, you can use virtual machines. And it is possible that many people use the wonderful product from Sun “VirtualBox” for these purposes. I'm sure that most people use the GUI interface for configuration, because... it is very clear and convenient. But how many people have thought about the possibility of managing VirtualBox via the console?
The task was set: to install two operating systems (Ubuntu and Windows) on one server. At the same time, there was a running server running Ubuntu 9.04 Server Edition. Therefore, it was decided to install Windows XP as a guest system.
But a problem arose on Ubuntu: no graphical environment was installed, i.e. there is only a bare console.

So what we have:

  • Host system: Ubuntu 9.04 Server Edition
  • VirtualBox 2.2.2
  • Guest system: Windows XP

Installing VirtualBox

Download the package for Ubuntu 9.04 from the official VirtualBox website
After downloading, run the command:
dpkg -i virtualbox-2.2_2.2.2-46594_Ubuntu_jaunty_i386.deb
VirtualBox should install. If you have problems installing the package, check the error messages. You may not have the necessary packages installed. In my case I didn't have the package installed libxslt1.1, which was immediately fixed by the command sudo apt-get install libxslt1.1.
Add yourself to the group vboxusers
sudo usermod -a -G vboxusers username

Creating a virtual machine

To begin with, it is worth noting that to control virtual machines One VBoxManage command is used through the console (although it has a lot of parameters).
Let's begin.
  1. First, let's create a car and immediately register it
    VBoxManage createvm -name virtual_machine_name -register
  2. Next you need to create a disk for the machine
    VBoxManage createhd --filename disk_name --size disk_size in_megabytes
    It is possible to select the disk type VDI (VirtualBox), VMDK (VMWare), VHD (Microsoft Virtual PC). By default, of course, VDI :).
  3. Let's do additional settings our virtual machine. Let us indicate:
    • Guest OS type. To get a list of supported operating systems, run the VBoxManage list ostypes command
    • Memory allocation size
    • Main disk name
    • Ability to use VRDP (VirtualBox Remote Desktop Protocol)
    VBoxManage modifyvm virtual_machine_name --ostype OS_type --memory memory_size in megabytes --hda disk_name --vrdp on
  4. Let's connect the disk image from which we will install Windows XP
    • Let's register a DVD with the system distribution image
      VBoxManage openmedium path_to_image
    • Let's insert our disk into the virtual drive
      VBoxManage modifyvm WindowsXP --dvd path_to_image
  5. Let's look at the settings of our machine. We make sure that everything suits us and...
    VBoxManage showvminfo machine_name

...Launch... Let's go

To start a virtual machine you need to run
VBoxManage startvm machine_name --type vrdp
Parameter --type vrdp needed to connect to the machine using VRDP
That's it, now all that remains is to connect to our machine using a remote desktop. This can be done using standard utilities: for windows it is mstsc, for x-based systems it is rdesktop. Since we only have a console, we need to use a computer that has a graphical desktop.
To connect, you need to specify the IP host of the machine and the port for this virtual machine. In my case, I didn't change anything and used the default port (3389). To change the port, run VBoxManage --vrdpport port. If you have several virtual machines installed, then in order to connect to each of them, you need to specify different ports in the settings, for example, for the first machine 3389, for the second 3390, etc.

Instead of an epilogue

Now we have a virtual machine running and we can easily connect to it using the VRDP protocol. I quietly installed Window XP while at my work computer.

P.S. Windows XP was needed for only one reason. The project needed to use MS SQL Server.