dcsimg

VirtualBox’s Little Secret: The Command Line

Casual VirtualBox users might not know about the awesome power that lurks just beneath the surface in the Command Line Interface (CLI).

The most obvious question is, “Why work at the command line when you have a perfectly good GUI?” The answer is power. Since 1995, there’s a whole generation of computer users who think that the computer is a collection of windows and graphics, but, in reality, nothing could fall farther from the truth. Why, 1995, you ask? 1995 ushered in a new age of graphical computing with the introduction of Windows 95. Soon following that fateful August was the release of FVWM95, which was the Linux answer to the Windows 95 Explorer interface. It seemed to me that the good old days of the command line were gone forever. No one wanted to use DOS anymore and they weren’t too thrilled with Linux either. The command line is still alive and well in Linux environments. It’s still very much alive in the Windows realm. And now that Macs are Unix-based, it’s kicking up some non-GUI dust as well.

It may surprise you to know that sneaking around under that cute VirtuaBox GUI, with its colorful graphics, explicit Settings page and numerous dropdown lists, is the occult world of the command line. The true power of this dark realm reveals itself to those who dare enter its musty catacombs. After you discover the VirtualBox CLI, you might decide that the GUI is little more than lame window dressing (pun intended) created for those passersby who would otherwise ignore the uninspiring black rectangle of the real application.

Command Line Power

The rectangular box to which I refer is the terminal window. The command line offers power automation. Using scripts in the form of Perl, shell scripts or some other language is the only automation method that I’m aware of on *nix systems. The system scheduler daemon (cron) assists you in firing your scripts at the right times. It’s the power of the command line through automation that requires system administrators have scripting capability in their cache of skills. Every system administrator job opening you see will have shell scripting and at least one other interpreted language listed as requirements.

For virtualization, the command line allows you to rapidly create virtual disks, add a new network interface, start VMs and create new VMs, all without the use of a GUI. Scripted automation also allows you to schedule a VM’s start and shutdown without intervention. And, if you’re a really clever scripter, you can create a script that will send you through a series of prompts to create a VM, start, install and update it with very little intervention. If you’re the hands-off type and you have a list of VMs that you use on a regular basis, you can setup a set of scripts (think templates) for each type of VM that you use and create your VM within seconds of needing one.

Let’s take a look at the simplicity and elegance of the command line functionality in VirtualBox.

Installation and Basics

Download a binary distribution from VirtualBox Linux Downloads for your system. For you purists, the source code is also available. My selection (RHEL5/CentOS5 rpm) installed everything in the following locations:

  • Binaries – /usr/lib/virtualbox linked from /usr/bin
  • Libraries – /usr/lib/virtualbox
  • Sources – /usr/share/virtualbox
  • Guest Additions (ISO) – /usr/share/virtualbox

If your distribution doesn’t have the latest VirtualBox available (3.1.2, as of this writing), use one of the downloadable packages or the source code. Once you’ve installed VirtualBox, you may proceed as a regular user.

VM Creation and Configuration

The most difficult part of using the command line is knowing where to start. The GUI makes this easy by having a New button available for you to use. The command line is a bit more reluctant to give up its secrets. To create and setup your new VM, open a terminal window or ssh to the Linux host where you installed VirtualBox and enter the following commands.

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

This creates the new VM named, Debian5 and registers it with VirtualBox. The VM created is an XML file located in your home directory: ~/.VirtualBox/Machines/Debian5/Debian5.xml.

Create a virtual disk for your VM:

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

You created a virtual disk named Debian5.vdi, 4GB (4,000MB) in size and with fixed size (non-expanding).

The virtual disk is created under ~/.VirtualBox/HardDisks/Debian.vdi

Create a Storage Controller to which you’ll attach the virtual disk and a virtual CD/DVD drive.

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

This command created an IDE controller named “IDE Controller” and controller type PIIX4.

Now, attach your virtual disk to your VM:

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

Attach an ISO image for the 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

Setup Networking:

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

This creates a bridged NIC with cable connected on startup and the Linux system sees the adapter as eth0.

Start the VM:

$ /usr/bin/VBoxManage startvm Debian5

If all goes as planned, you’ll see the following messages and then your VM window ready for installation.

Waiting for the remote session to open...

Remote session has been successfully opened.

As you can see, creating and setting up a VM at the command line frees you from your mouse click madness and opens up a whole new world of possibilities for VM automation. And, we’ve only scratched the surface here but hopefully this brief introduction will pique your curiosity and set you on a new quest to learn the command line’s awesome power.

Comments on "VirtualBox’s Little Secret: The Command Line"

viralnexxus

Excellent tidbit! This is yet another reason I enjoy using VirtualBox over VMWare. Lately I\’ve been in this whole \”CLI\” only mood, so this is perfect for me. ;) GJ, and keep `em coming…
Cheers,
-VB

Reply
severian37

I started using this last year to run development VMs with VirtualBox. It\’s a great feature for a great piece of software.

Reply

    Ik vind alle ta#78o&to21t;s juist een verrijking, ook al is het een tatoeage die ik zelf super lelijk vind. Het zegt meestal wel iets over de persoon. En dat vind ik heel leuk en interessant

    Reply
tbuskey

Get a list of available VMs:
VBoxManage list vms

Start one of the VMs w/o starting a gui
VboxHeadless -s VMname &

Run your web server inside a headless VM. Or an SSH server, etc.

Reply
ddelv

Sorry, I don\’t get it. What \’power\’ have you demonstrated using the CLI over the GUI? So far as I can tell, everything you showed us works as well, and is much much easier to do, using the GUI. Am I missing something?
Thanks for pointing out VirtualBox, though!

Reply
meesterarend

Actually there are some power in the cli that\’s not in the gui, some settings are hidden away, or even, sometimes you even have a server to which your only access is via ssh and cli and no gui, over a 56k modem :), so yes while the author did not reach the wow effect, he did demonstrate that cli can be used to do everything the gui does and more, when putting everything in a script, you can actually start doing very impressive stuff.

Reply
khess

The power comes in from being able to use a single command to circumvent all those mouse clicks. The CLI also offers scripted automation. The other point is that GUIs are often \’broken\’ for many of the more elegant features.

Reply
tbuskey

I can run VBoxHeadless and then log out. The VM keeps running.
If I start a VM with the GUI and quit the GUI, the VM stops running.

With the CLI, I can have VMs start as soon as a reboot finishes. I could even have them gracefully suspend during a shutdown.

Potentially, this could give me most of what VMware ESX does without the costs.

Reply
buggin

I\’d rather click than type

Reply

    The beauty of VBox and similarly designed s/w is that you can use *both* the GUI *and* the CLI. Clicky-drool for some tasks (all if you’re a newbie) and CLI for other tasks.

    The CLI *rocks* for automating repetitive tasks. Want to re-encode only AVI files larger than 500MB? Sure you could click 8,000 times. With 200 clicks, though, you could write a “for” loop with embedded “if” statements to do it all while running overnight.

    Reply

    Thanks alot – your answer solved all my problems after several days stngiulgrg

    Reply

    I’m so happy for you Ashley! And I know what you mean. Everything changes, in one moment, and everything before was just getting you to that moment — becoming Su7#lvan&i821l;s mom. Much love coming your way.(Um, oh, and we put an offer in on a house in Connecticut last night. So you let me know when Sullivan’s up for a decorating trip to the northeast.) http://rdugbhle.com [url=http://lsfqvzkiws.com]lsfqvzkiws[/url] [link=http://qhqfzb.com]qhqfzb[/link]

    Reply
clawes

Using the GUI is OK if you\’re not a system administrator who needs to create several VMs as quickly as possible.

The bash script below will create and start a VirtualBox VM in less than 2 seconds. Try doing that with the GUI – impossible.

That\’s the beauty of CLI: automation and speed.

I can easily tweak this script to create 5 or 50 VMs in no time at all.

<script>
#!/bin/bash
vmname=\”vmUbunt810\”
VBoxManage createvm -name $vmname -register -ostype \”Ubuntu\”
VBoxManage modifyvm $vmname -memory \”512MB\” -acpi on -boot1 dvd -nic1 bridged
VBoxManage createvdi -filename $vmname.vdi -size 20000 -register
VBoxManage modifyvm $vmname -hda \”$vmname.vdi\”
VBoxManage modifyvm $vmname -bridgeadapter1 eth0
VBoxManage modifyvm \”$vmname\” -dvd /mnt/disk2/iso/ubunt810.iso
VBoxManage modifyvm \”$vmname\” -vrdp on
screen VBoxHeadless -startvm \”$vmname\”
</script>

One of the other neat features of VB is that you can use Windows RDP or Linux rdesktop to connect to a running vm on local or remote computer.
e.g.

rdesktop -a 16 -f localhost

Each vm can be configured to listen on a different RDP port
rdesktop -a 16 -g 800×600 vmserver:3390
rdesktop -a 16 -g 800×600 vmserver:3391

Reply
tbritton

As a long-time computer person – from the days of the cardpunch – I\’m strongly in favour of the commandline, especially for administrative tasks. The problem with commandlines though, is knowing what commands are available (and remembering them). This article is good but leaves out one critical detail – where do you go for information about what commandline commands are available?

Reply
asche

the commands are in section 8 of the manual… which if you have it on a local system (probably because you use GUI for your desktop or laptop) then you may have Virtualbox on the desktop, or you can download the current version here \” http://download.virtualbox.org/virtualbox/3.1.2/UserManual.pdf \” this was taken directly from the downloads section of the Virtualbox.org website… there are a LOT of options that are available, and as has been mentioned elsewhere, the beauty and speed of CLI for Windows OSX, Solaris BSD…. {list goes on} is not always in the ease of doing something, but in the fact that you can A. copy, and paste commands, B. script, C. reduce bandwidth needed for the access to do these things, D. SCHEDULING TASKS… using the AT or CHRON functions… so you can back up the VM for instance and start the task, then fire and forget…
make the connection to the host, enter the command for that platform to activate the script, fire and forget… walk away… the process will continue without further attention…

Reply
arvindgangal

VirtualBox guys have done a good job on GUI which provides a quick start in using product, as command line could alienate some one who wants to give it a try.
Having said that there are far more power under the hood, and it would have been a overkill to provide the functionality in GUI hence the command line.
For example \”Tuning TCP/IP buffers for NAT\” or setting up port forwarding which is used so one could SSH in the guest Linux OS or provide access the web server in the guest OS. See VBoxManage setextradata.

Reply
wamorita

I am very happy with the CLI.
I can drive it out of a standard shell script (bash).
I have an XP VM that I have a script that does the following:
* Backup the vdi using the \”clone\” feature.
* Compact the vdi (I typically run sdelete in the vdi before)
Typical yield is 2-3 GB reduction.

You can\’t do either of those things from the GUI.

Reply

    I would like to hear more about backing up or cloning via the command-line vm’s. Is there a way to make a backup (or clone) a running vm without shutting it down?

    Reply

      As would I. I’m currently in the process of trying to port a backup/restore script (found as a sticky at jasoncarman.wordpress.com) from it’s current Fedora 16/17 with KVM over to Mint using VirtualBox. I’m currently attending college for Network Administration & Networking, and find myself using virtual machines often.

      Reply
jwjwj

Hi,

Windows 95?? You might want to recheck your history lesson: let\’s try 1984 with the Macintosh for the first mainstream personal computer GUI and even earlier than that for a host of other pioneering efforts.

Cheers.

Reply
khess

@jwjwj
I used 1995 because that\’s when Linux began to hit the mainstream. It was a common reference point.

Reply
severian37

For myself as a sys admin, everything I do is from the CLI. The only time I see the GUI is when I install the server. I can setup a Vbox machine, have it automated from the CLI, and be done with it.

Reply
secrity

I currently admin 88 RHEL servers (most of the RHEL servers are virtual) and 74 HP-UX servers (all physical servers) — and I do everything on those servers using a cli. I do not have X installed on the Red Hat servers. I occasionally use a gui on the HP-UX servers for Glance (perf monitor) and I rarely use SAM for unusual things like configuring NICs.

I use a CLI to admin the VMware guest servers and storage, but I admin the VMware ESX hosts using a cli. There are an additional several hundred Citrix and Windows servers running on the VMware ESX hosts; I admin the host servers, but not the Citrix and Windows servers.

Reply
gcaldwell7

Nice. I can now login into my office and startup VM’s on the command line and do all of the config without the gui in the way, faster and more powerful. I Love VirtualBox it kicks butt !!!

Reply
mwclarke

System tuning is becoming a loss art I guess with the availability of faster processors, cheaper memory and cheaper storage.
Most non-windows servers usually do not have any GUI/X installed or loaded anyway. even if have installed for client support the server if not starting X for the console will still out perform a windows server. Use to get by with much smaller system requirement to run unix servers than a windows counterpart (and please I know the server/client relationship for X is backwards). Most applications running on servers do not need a GUI, nice for some admin functions but not needed and takes a crap load of memory to have that GUI sitting there most of the time doing nothing.
most enterprises going to do a lot of remote systems monitoring and administration anyway so no need for a GUI. I use WEBMIN on all my unix systems not much web overhead for that.
besides that if have the X windows components loaded I can start a remote X session from a client machine and run any of the unix GUI apps if needed and once quit is not continuing to run in the background taking up system resources.
CLI has power on unix systems with scripting. one can do so much and have so much control so easy with just a few modest scripts on unix is just a no brainer. Although I still love VMS best, just wished it had went opensource and on x86 :-)
Now, I do love a GUI when it comes to a desktop, but of course for the applications, for word processing, spreadsheets, web browsing and better, games, where it shines and is needed.

Reply
nonlinearly

And something else more more more important…
If you are in a serious production environment and the linux host is running without GUI and only with CLI then the Virtualbox CLI is the only choice…

Reply

monawath terenne na………………..

Reply

booyaka………. booyaka………….
Wrestle Zone

Reply

I have a question.

It’s awesome to have a CLI option instead of using a GUI, since all the servers I administer doesn’t have GUI’s (I prefer it that way -it’s easier)

My question is, RDP is disabled by default in Windows 7 so will this command: “rdesktop -a 16 -g 800×600 vmserver:3390″ allow me to logon to the VM’s monitor instead of directly connecting to the OS’s RDP protocol?

Does this question make sense?

Reply

I have been exploring for a bit for any high quality articles or weblog posts in this sort of space . Exploring in Yahoo I eventually stumbled upon this site. Reading this info So i’m satisfied to convey that I’ve a very good uncanny feeling I discovered exactly what I needed. I such a lot without a doubt will make certain to don?t omit this web site and provides it a glance regularly.

Reply

Thank you!! Really good info and easy to understand. I have been looking for this page for the last 3 hours.

Reply