How to use VirtualBox in Terminal / Command line


http://www.howopensource.com/2011/06/how-to-use-virtualbox-in-terminal-commandline/


Updated by Administrator | Apr 23, 2013 | Linux | 5 Comments


How to manage VirtualBox in command line or terminal. VBoxManage is the command which is used to manage VirtualBox in commandline. Using VBoxManage, one can create and control the virtual OS and there are many features than GUI VirtualBox.

Here is a simple tutorial on how to create a virtual OS (Ubuntu10.10) using VBoxManage and access it remotely from the host machine.

INTRUCTIONS

To create a virtualmachine(Ubuntu10.10), use the below command or copy and paste it in terminal. If you want to create a virtual machine for fedora or some other OS, change the name Ubuntu10.10 to fedora or slax or kubuntu etc.

VBoxManage createvm --name Ubuntu10.10

In the above command, “createvm” is used to create a virtual machine and “–name“defines the name of the virtual machine. After executing this command it will create virtual machine called “Ubuntu10.10.vbox” in home folder under “VirtualBox VMs/Ubuntu10.10/Ubuntu10.10.vbox”

*Note: If the name has space, then it should be given within quotes.

Say for example, “Ubuntu 10.10″.

Now, create the hard disk image for the virtual machine using the below command

VBoxManage createhd --filename Ubuntu10.10 --size 5120

In the above command, “createhd” is used to create hard disk image and “–filename” is used to specify the virtual machine’s name, for which the hard disk image is created. Here, “–size” denotes the size of the hard disk image. The size is always given in MB. Here we have specified 5Gb that is 5120MB.

After creating a virtual machine, the VirtualBox has to be registered. “registervm” command is used to register the virtual machine. The full path of the virtual machine’s location has to be mentioned.

VBoxManage registervm '/home/user/VirtualBox VMs/Ubuntu10.10/Ubuntu10.10.vbox'

or

The virtualmachine can be registered while creating virtual machine using “–register“. Below is the command

VBoxManage createvm --name Ubuntu10.10 --register

Now set the OS type. For example, if the Linux OS has to be installed, then specify the OS type as Linux or Ubuntu Or Fedora etc.

VBoxManage modifyvm Ubuntu10.10 --ostype Ubuntu

One of the most important command in VBoxManage is “modifyvm“. Using “modifyvm”, one can modify many features in virtual machine like changing the memory size, name of the Virtual Machine, OS type and many more. The name of the virtual machine has to be specified inorder to modify it. In the above command, Ubuntu10.10 has been explained. The command “–ostype” is used to set the OS type like Linux, Windows, Ubuntu, Fedora, etc,.

Now, set the memory size for the virtual OS, i.e. the ram size for the virtual OS from the host Machine.

VBoxManage modifyvm Ubuntu10.10 --memory 512

The command “–memory <size>” is used to set the RAM size for the virtual machine from the host machine. The size should be defined in MB.

Now create a storage controller for the virtual machine.

VBoxManage storagectl Ubuntu10.10 --name IDE --add ide --controller PIIX4 --bootable on

storagectl <name>” is used to create a storage controller for virtual machine. Later the virtual media can be attached to the controller using “storageattach” command. The above command creates the storage controller called IDE. <name> defines the name of the virtual machine.

Here,

–name <name>” specifies the name of the storage controller that needs to be created or modified or removed from the virtual machine.

–add <options>” defines the type of system bus to which the storage controller must be connected. Available options are ide/sata/scsi/floppy.

–controller <options>” allows to choose the type of chipset that is to be emulated for the given storage controller. Available options are LsiLogic / LSILogicSAS / BusLogic / IntelAhci / PIIX3 / PIIX4 / ICH6 / I82078.

–bootable <on/off>” defines whether this controller is bootable or not.

VBoxManage storagectl Ubuntu10.10 --name SATA --add sata --controller IntelAhci --bootable on

Using the above command, a storage controller called SATA has been created. The hard disk image can be attached to this later.

Now, attach the storage controller to the virtual machine using “storageattach” .

VBoxManage storageattach Ubuntu10.10 --storagectl SATA --port 0 --device 0 --type hdd --medium "filename"

The above command will attach the storage controller SATA to virtual machine Ubuntu10.10 with the medium i.e., to the virtual disk image which is created.

storageattach <name>” is the command used to attach the storage controller to the virtual machine.

<name> defines the name of the virtual machine.

–storagectl <name>” is used to define the name of the storage controller which needs to be attached to the virtual machine.

<name> defines the name of the storage controller.

–port <number>” is used to define the number of storage controller’s port which is to be modified.

–device <number>” is used to define the number of the port’s device which is to be modified.

–type <options>” is used to specify the type of the drive in which the medium should be attached. Available options are dvddrive / hdd / fdd.

–medium <options>” defines the hard disk image or ISO image file or virtual DVD. Available options are none / emptydrive / <uuid> / <filename>host:<drive>iscsi

*Note: If you decide to specify the filename, then specify the full path where it is located.

Example: “/home/user/Ubuntu10.10.vdi”

VBoxManage storageattach Ubuntu10.10 --storagectl IDE --port 0 --device 0 --type dvddrive --medium "filename"

Here, the above command will attach the storage controller IDE with the medium of ISO image as DVD drive. This medium can be closed after installing the virtual OS(Ubuntu10.10).

filename“– Example: “/home/user/Downloads/ubuntu-10.10-desktop-i386.iso”

Next, add some features like audio, 3d acceleration, network, etc,.

VBoxManage modifyvm Ubuntu10.10 --vram 128 --accelerate3d on --audio alsa --audiocontroller ac97

–vram <size>” This sets the size of RAM that the virtual graphics card should have. The size should given in MB.

–accelerate3d <on/off>” if the guest additions are installed, this sets the hardware 3D acceleration for the virtual machine.

–audio <options>” is used to set the audio for the virtual machine with available host driver. Available options are none /null / oss / alsa / pulse.

–audiocontroller <options>” is used to set the controller for the audio in the virtual machine. Available options are ac97 / hda / sb16.

VBoxManage modifyvm Ubuntu10.10 --nic1 nat --nictype1 82540EM --cableconnected1 on

–nic<1-N> <options>” with this the type of networking can be set for each of the VM’s virtual network cards. Available options are none / null / nat / bridged / intnet / hostonly / vde.

–nictype<1-N> <options>” is used to specify which networking hardware is to be presented to the guest VirtualBox. Available options are Am79C970A / Am79C973 / 82540EM / 82543GC / 82545EM / virtio.

–cableconnected<1-N> <on/off>” This allows to temporarily disconnect a virtual network interface from virtual machine. This might be useful for re-setting certain software components in the VM.

Now to start a virtual machine, use the below command

VBoxManage startvm Ubuntu10.10

startvm <name>” is the command used to start the virtual machine. By default it starts in the GUI mode.

<name> defines the name of the virtual machine.

Remote Desktop in VirtualBox

To activate the remote desktop, set the port number and address.

VBoxManage modifyvm Ubuntu10.10 --vrde on --vrdeport 5012 --vrdeaddress 192.168.1.6

–vrde <on/off>” is used to set the remote desktop ON or OFF.

–vrdeport <number>” is used to set the port number in the port in which the virtual machine should be available. “default or 0″ will set the port in 3389.

–vrdeaddress <address>” is used to set the “IP” address in which it should be accessable.

Now, start the virtual machine using the below command

VBoxManage startvm Ubuntu10.10 --type headless

–type <options>” is used to specify the mode in which it should start the virtual machine. Available options are gui / sdl / headless.

headless” produces no visible output on the host at all, but only delivers VRDP data. This front-end has no dependencies on the X Window system on Linux and Solaris hosts.

Alternative method to start virtual machine for remote access is VBoxHeadless. Use the below command.

VBoxHeadless --startvm Ubuntu10.10

–startvm <name>” is used to start the virtual machine. <name> defines the name of the virtual machine.

To access the remote desktop, use rdesktop command. By default the linux system should have rdesktop. If not, install it. Use the below command from the client machine to access the virtual machine remotely.

rdesktop -a 16 -N 192.168.1.6:5012

To stop the virtual machine, check the below command

VBoxManage controlvm Ubuntu10.10 poweroff

“controlvm <name> <options>” controlvm command is used to control the state of the virtual machine. <name> defines the name of the virtual machine. Some of the available options are pause / resume / reset / poweroff / savestate / acpipowerbutton / acpisleepbutton. There are many options in controlvm to see all the options available in it. Either type or copy and paste the below command in terminal.

VBoxManage controlvm

Hope this will be helpful for you!!!


posted @ 2016-06-17 17:25  张同光  阅读(248)  评论(0编辑  收藏  举报