قالب وردپرس درنا توس
Home / Tips and Tricks / How to Create VirtualBox VMs Using the Linux Terminal – CloudSavvy IT

How to Create VirtualBox VMs Using the Linux Terminal – CloudSavvy IT



  VirtualBox logo

Most cloud providers rent virtual servers rather than actual hardware. If you want to do the same on your own servers, either to run different operating systems or to manage multiple environments, you can do so entirely from your terminal.

If you only want to use with You might be interested in a hypervisor for virtual machines. A hypervisor replaces your host operating system and was specially developed to manage multiple virtual machines. You cannot run applications on the host operating system, but you can simply virtualize Ubuntu Server or the like and run the applications on the hypervisor.

Install VirtualBox and the VBox extension package

VirtualBox is free GUI-based virtualization software that runs as an app on your host operating system (a type 2 hypervisor). You may have previously used it on a PC to set up a VM, but VirtualBox actually has a full command line interface and can only be fully used from one terminal, making it perfect for this use case. Another solution would be QEMU, but VirtualBox has proven itself and is unbeatable because it is free and open source.

However, the VirtualBox-CLI is a bit unwieldy, so if you only play around with and have only one home server. If you want to save some RAM, you can instead install a light desktop environment on your server and set up VirtualBox as usual. You can still run VMs in headless mode to continue running after you log out of the DE.

If you plan to follow the CLI, the installation is fairly easy since it is directly available from apt . :

  sudo apt-get install virtualbox 

You should make a note of the installed version ( VBoxManage -v ) as you need to install some more things. Since we are setting up in headless mode, we need a way to connect to the computer before SSH / RDP can be configured.

Go to the VirtualBox download page and find the link for the expansion pack for the version you are running on. Download it with wget :

  wget https: //download.virtualbox. org / virtualbox / 5.2.42 / Oracle_VM_VirtualBox_Extension_Pack-5.2.42.vbox-extpack [1
9659009‹Andinstall:

  sudo VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-5.2.42.extpack [builtinwithsomereasonVNCextensionpackagenotworkingRemovingthispackagefixedtheproblem:

  sudo VBoxManage extpack uninstalled VNC 

Setting up a VM

First we create a new VM with the name "Ubuntu". You can display a list of supported operating system types with VBoxManage list types .

  sudo VBoxManage createvm --name "Ubuntu" --ostype Ubuntu_64 --register 

Give the computer RAM and create a new hard drive. We'll be working with 4 GB of RAM and 32 GB of hard drive space, but both can be expanded later. Note that the hard disk is a virtual hard disk and is not pre-assigned.

  sudo VBoxManage modifyvm "Ubuntu" --memory 4096
sudo VBoxManage createhd --filename "Ubuntu.vdi" --size 32000 

Set the number of vCPUs. This is measured in threads, not in full CPU cores.

  sudo VBoxManage modifyvm "Ubuntu" --cpus 2 

Next follows another configuration for the hard disk. Usually this happens automatically when computers are created using the GUI. However, you must manually create a SAS controller and connect it to the VM for the hard drive to work properly.

  sudo VBoxManage storagectl "Ubuntu" - name "SAS Controller" --add sas --controller LSILogicSAS
sudo VBoxManage storageattach "Ubuntu" --storagectl "SAS Controller" --port 0 - device 0 - type hdd --medium "Ubuntu.vdi"
sudo VBoxManage storagectl "Ubuntu" --name "SAS Controller" --hostiocache on 

We do the same for the hard drive on which the ISO is located to install the operating system. This requires that your ISO is at /home/user/Ubuntu.iso . Therefore, change them accordingly.

  sudo VBoxManage storagectl "Ubuntu" - Name "IDE Controller" --add ide --controller PIIX4
sudo VBoxManage storageattach "Ubuntu" --storagectl "IDE Controller" --port 0 --device 0 --type dvddrive --medium "/home/user/Ubuntu.iso"[19659009weiblThen switch on some of the performance-related switches to prevent you from slowing down due to excessive overloading ' Ubuntu "--nestedpaging on
sudo VBoxManage modifyvm "Ubuntu" --largepages on 

Next we create the network that this VM will use. There are many options for this step:

  • NAT, the simplest with which the VM can easily communicate with the outside world via Network Address Translation (NAT). This does not allow VMs to communicate with each other and does not allow host-to-guest contact unless the port is forwarded.
  • NAT network is similar to NAT, except that the network is shared between the VMs and this is also possible to talk to each other. The NAT network acts as a virtual subnet.
  • Bridged, which bridges the physical connection directly to the VM. The VM has an actual IP address in your network. This can also work with just one Wi-Fi / Ethernet connection. However, if you have a network card with multiple Ethernet ports, you can directly assign an entire interface to a VM.
  • Virtual network that connects multiple instances in an isolated virtualized network. This is useful for modeling real networks, but is not suitable for most use cases.
  • Host Only Adapter is similar to the virtual network, but allows communication with the host (not the Internet).

In this example, we will choose NAT Network because it is easy to set up, the most versatile of the series, and offers a balance between connectivity and isolation.

Create a new network on the 10.0.0.0/24 . Subnet, start it:

  sudo VBoxManage natnetwork add --netname natnet1 --network "10.0.0.0/24" --enable
sudo VBoxManage natnetwork start --netname natnet1 

And attach it to the VM:

  sudo VBoxManage modifyvm "Ubuntu" --nic1 natnetwork --nat-network1 natnet1 

Next we have to configure how we do it Access to this instance. Since we are installing this on a headless server (no video output), we cannot complete the installation process and cannot install an RDP client on the guest operating system. To solve this problem, we use the VRDE server from VirtualBox. This is the main reason why we installed the VirtualBox Extension Pack earlier. This records the output of the virtual display and makes it available via RDP.

Activate this option and define a port for it. You want to make sure that this port is open on the host operating system.

  sudo VBoxManage modifyvm "Ubuntu" --vrde on
sudo VBoxManage modifyvm "Ubuntu" --vrdeport 5587 

Finally we can start the VM in headless mode and start installing the operating system.

  sudo VBoxManage startvm "Ubuntu" - type headless 

Note that headless mode means that it is running on a headless server and not that the guest operating system has no video output.

It takes a second or two to start, and you can start an RDP client and connect to the port that you specified. You may be asked for a username and password. By default VRDE is "admin" without a password.

Port Forwarding After Setup

After the operating system installation is complete, you need to perform port forwarding to maintain access after disabling VRDE, essentially like opening firewall rules. If you're setting up a headless environment like Linux, all you have to do is forward SSH. The following command adds a port forwarding rule to your NAT network to forward port 2200 on the host operating system to the standard SSH port on the guest. Note that this port must be open in your host's firewall for this to work.

  sudo VBoxManage natnetwork change --netname natnet1 --port-forward-4 "ssh: tcp: []: 2200: [10.0.0.2]: 22" 

If you set up RDP, you have to set the RDP server in Configure the guest operating system and ensure that the RDP port is open in the guest's firewall. Then you can forward in the same way:

  sudo VBoxManage change natnetwork --netname natnet1 --port-forward-4 "rdp: tcp: []: 5589: [10.11.0.2]: 3389" [19659009] If you have rules to delete the port forwarding, you have to delete it with this rather unwieldy command (the first string before the colon) by name: 

  sudo VBoxManage natnetwork modify --netname natnet1 --port-forward -4 delete rdp 

As soon as After you have verified that you can connect directly to the guest operating system via SSH / RDP, you can deactivate VRDE:

  sudo VBoxManage modifyvm "Ubuntu" --vrde off 

If you if you are planning to run this VM via a To use the RDP for a long period of time, you may want to install the VirtualBox Guest Additions Suite for your guest operating system.


If you want to manage VirtualBox after setting up this VM using the CLI using the GUI Note that this VM was created as root . You must therefore start the VirtualBox GUI as root e this VM in the list. Otherwise, it should work normally.

If you want the computer to run while your desktop environment is down, you must right-click to start the computer in headless mode.


Source link