VirtualBox Introduction
VirtualBox Documentation
Here are a few items that are helpful
- To switch in and out of full screen, the default key sequence is CNT-F
- Once operational, we will refer to the different screens as follows:
- the main VirtualBox window that allows you to start/stop and configure the VMs, will be referred to as VirtualBox Manager window, sometimes referred to as Mgr Window.
- The window that represents the VM (the specific VM name is in the top portion of the window). We refer to this by a specific VM name, such as VM1Window or sometimes as VM1.
- System wide configuration is done in the VirtualBox Manager window by selecting File and then Preferences. You can also reach this configuration window from a VM Window. We refer to this configuration window as System Config Window. For example, the Network Preference allows specific types of virtual networks to be created ... and in some cases further VM configuration is required.
- VM specific configuration, which we refer to as VM configuration, is Sfound in several ways:
- From the Mgr Window, highlight the VM listed in the set of VMs that have been configured, then select Settings (or Machine).
- From a VM Window, select Machine and then Settings.
Installing a VM on VirtualBox
Step 1. Installing a VM.
- Download to your laptop the latest version of VirtualBox
(use latest version).
- Download and install the recommended extension pack.
- Download and install the Linux distribution we will use: Ubuntu 16.04.1 Long Term Support (LTS)
- Please use the 64-bit version.
- The file you download should be an iso file- you can keep this file
in the Downloads directory (or where ever you downloaded
it).
You do NOT need to burn an install CD.
- Read over the VirtualBox Users Manual, and then install VirtualBox along with one VM (specify ubuntu 64 bit as the type), 2 GBytes of memory,
a max disk space of 10GBytes that is allocated
dynamically, and file type VDI.
Step 2. Once you install the VM, you should install the VMs set of
helpful drivers referred to as "Guest Addtions"
- Start your VM and select the 'Devices' option and then 'Insert Guest Options CD' - it should ask you if you want the VM to run the install program - say yes.
- An alternative, in a Ubuntu guest, issue 'sudo apt-get install virtualbox-guest-utils'
and
- Note cd to the mnt of the CD image, Issue 'sudo runasroot.sh' or 'sudo ./VboxLinuxAdditions.run' or 'sudo VboxLinuxAdditions-amd64.exe'
- See this page for help: https://www.virtualbox.org/manual/ch04.html
Step 3. Tweak your VM settings to enable these useful features::
- Make sure VM Manager is configured for CPU acceleration. Make sure your VM is not running, from VirtualBox Mgt, select Settings, and then System, and then Acceleration and Enable.
- If this is not selectable, then you should reboot your computer, and go to BIOS setup to enable VM Acceleration.
- To allow the VM to operate on >1 physical CPUs
- VM Virtual Box Manager, select Settings and then System and set number of processors
- To make it easy to transfer files between VMs and Host OS, I create a Shared Folder
- On your VM, select Devices and Shared Folder.
- Make sure to create a folder on your Host OS first, and then specify this as the shared folder. Indicate it is permanent.
- On your VMs, issue a 'df -k' and you should see the folder mounted. Note that you might need to use sudo to access the folder.
- To allow cut and paste across your Host and Guests, enable Bidirectional Drag and Drop in each VMs Settings, Select General, and then Advanced - set shared Clipboard and DragNDrop to bidirectional. .
- This is an example .bashrc for Ubuntu 16.04 that provides some helpful alias's and functions : ./bashrc
Packages to install after a installing a Ubuntu VM
After a fresh install of Ubuntu, you should do a 'sudo apt-get update'. Then, ,issue 'sudo apt-get install xxx' the following.
To see all installed, 'apt-cache pkgnames', To see if a specific pkg is installed, like iperf, 'apt-cache showpkg iperf', And if you know the program you want to get but it is different from the package name, you can try 'apt-cache showpkg iperf'. It appears that are 16.04 release comes with at least some of these packages. So only install them if needed.
- build-essential
- vim
- inetutils-traceroute
- dhcping
- xutils-dev
- iperf3
- sysstat (to get mpstat)
- dos2unix
- source-highlight (to get check-regexp)
- snmp
- openssh-server - need to install and start to ssh into your VM
- restart: sudo /etc/init.d/ssh restart
- Octave is an open source system similar to matlab.
- Full install:
- sudo apt-get install octave
- If it was not in the main repos, you could setup the ppa.
- sudo apt-add-repository ppa:octave/stable
- sudo apt-get update
- sudo apt-get install octave
Additional setup on the VM (optional)
Network Setup
In this discussion, we add further details on the default virtual environment setup with VirtualBox with ubuntu 16.04 running as a Guest. From a TCP/IP perspective, we refer to the Guest as VM1 and the TCP/IP host that represents VirtualBox's Host and VMM as the Host. Any number of VMs can be created, with 1 or more virtual networks.
In this section, we overivew networking in a VirtualBox VM environment. Then we describe the setup required to support two different scenarios, one involving one VM; one involving two VMs. We refer to each scenario as Scenario 1and Scenario 2.
VirtualBox Network Information:
Types of networks:
- NAT (Section 6.3 of the Manual): This is the default as it is the simplest. Each VM by default will use the NAT network. The VM interface is allocated 10.0.2.15 (as all other VMs).
- NAT Network: This is a second NAT implementation. See section 6.4. It relies more on the VMs for NAT support than the original NAT.
- Host only network: This is a private network that connects Hosts. This network does not have connectivity to the outside unless one of the VMs has a second interface connected to the Host.
- Bridge: This supports a configuration where a VM effectively sits on the same external network as the Host OS. The VMs IP address for the interface can be assigned by the same DHCP server that assigned the IP of the Host OS.
Another concept that can be used with IP networks (and that is supported by VirtualBox) is port forwarding. Several variants of the idea, but basically the Host can be told that for any packets that arrive with a destination IP address matching the Host's external facing IP address and with a specific destination port, this packet should not be forwarded to the Host, instead, it should be forward to a different socket endpoint. When setting up port forwarding, you specify the matching destination IP address and port and the new destination address and port. The simple case is when the new destination IP address is the Host's IP address, but packets with specific ports are 'forwarded' to a new destination IP and port. NAT generally does not allow 'unsolicited' outside connections (in other words, NAT works only on IP flows that originate from the private side. NAT, therefore, supports inside-to-outside connectivity. Port forwarding is a widely used technique that allows unsolicited outside-to-inside access.
These various choices of networks allows VirtualBox to support VMs that are happy being 1)disconnected from any external network; 2)secure outside-to-inside access with NAT; 3)unsolicited outside-to-inside access with Bridge networking; 4)more restrictive unsolicited outside-to-inside access with NAT and port forwarding.
Also, review outside material to make sure the following makes sense:
- The virtual network environment supported by VirtualBox (and VMWare) appears as a real network to the Guest OS. When we install the extensions however, it replaces the real ethernet driver with one that can interact directly with the VMM running on the Host OS. So from our Linux Host, all TCP/IP protocols, including the use of arp hold.
- We assume IPv4 networking, which means a 32 bit IP address is to be assigned to each interface on a Guest VM.
- Guest IP addresses can be assigned dynamically or statically.
- Dynamic- this requires a Dynamic Host Configuration Protocol (DHCP) server- which by default is provided by the Host and which by default is required by the Guest.
- Static- To set a specific IP address, use the Guest OS configuration.
- In TCP/IP (version 4 only - version 6 uses much larger size IP addresses), there are 2EXP32 available numbers in a 32 bit address space. Not all of these numbers are valid IP addresses as portions of the address range (from 0 to 2EXP32) are considered special (all 1's, all 0's, private IP addresses).
- Rather than identify an IP address as a unsigned number, the number is interpreted an octet at a time, such as 130.127.22 55. This is referred to as dotted decimal notation.
- IP v4 addresses are considered as 2 (sometimes 3) level hierarchical address. This means there is multiple pieces of interformation encoded in a 32 bit IP address. The original classful address mechanism defines a 32 bit address as <network id , host id> , further, the first several bits (most significant bits of the 32 bit value) encode the class (or type) of address: Class A, B, C, or D. Class A,B,C correspond to valid IP addresses but each class specifies a different demarcation of network and host bits. Class A defines the network id as the most significant octet (leaving 3 octets to define specific hosts that attach to that network. Class B and Class C addresses define netork id of 16 and 24 bits. The idea behind classful addressing is that all networks are likely not the same size.
- Blocks of addresses within Class A,B,and C networks are referred to as private addresses. For example, in Class A networks, addresses in the range 10.0.0.0-10.255.255.255 are private. Class B and Class C private addressing blocks are 172.16.0.0-172.31.255.255 and 192.168.0.0-192.168.255.255. By definition, a private IP address only has meaning (i.e., is routable) by Hosts within a specific organization's TCP/IP network (in Internet terminology: within a single autonomous network).
- Clemson has several blocks of valid IP addresses, one of which is the Class B network 130.127/16 (using network prefix notation to identify a specific network (the number after the slash specifies how many bits are used to specify the network id).
- No other network in the world can be assinged 130.127/16 (there are standards organizations to deal with the use and management of global Internet resources such as addresses).
- The global routing used in the network has enough information to know that any packet that contains a destination IP address that falls within the block of 64K IP addresses beginning with 130.127 are to be routed to our clemson border routers.
- It is up to Clemson if they want to route any of the private IP networks (they so route some 172.16/16 nets). There are many private nets on campus, some that use the same network id. The admins of each of these private networks is responsible for correctly interconnecting as needed with outside networks.
- Network address translation is the most common technique for handling private IP networks to communicate with external networks.
- A border gateway (in our case, VirtualBox Host) supports NAT. There are several types of NAT but the most basic is called NAPT (for Network Address Port Translation). By default, a form of NAPT is used to interconnect our VM to the outside world.
Scenario 1 - one VM
There are two options: the default NAT and bridge mode.
NAT:
L1 is the link from Host to the outside (campus wifi ) and L2 is the virtual network where the details can vary depending on how we configure the VM. Our starting point is to have L2 be a simple ethernet link as seen by VM1.The simplest approach is to use the default setup of NAT. By default, VirtualBox sets the the default network setup for VM1 will be 'NAT' (Note.....not NATNetwork). See Section 6.2 of the Manual.
Network--L1----Host--Link2-L2--VM1
By default, the Host OS VM facing NIC will be assigned an IP address of 10.0.2.2, although you won't see this IP if you issue ifconfig at the HostOS. The VM (in all VMs ) are assigned 10.0.2.15.
To allow outside to inside access, port forwarding is used.
It is very easy to use port forwarding with NAT.Issue the following from your Host OS. The first allows ssh access to your VM from a machine on the campus network.
- VBoxManage.exe modifyvm "VM2-Ubuntu-16.04" --natpf1 "sshClient,tcp,,5555,,22"
- And then run the ssh client with a param '-p 5555' which tells it to use port 5555.
- VBoxManage.exe modifyvm "VM2-Ubuntu-16.04" --natpf1 "UDPEcho,udp,,5000,,5000"
- Forwards udp packets with dst port of 5000 to the VM (the VM name must match)
Bridge Mode: Modify the VM machine configuration to use Bridge Mode for the type of network on the interface that will be used to provide connectivity to the outside world. Modify the VM (actually this would be the default settings in the VM OS) to used DHCP to obtain its IP information. When the VM boots it interacts with a DHCP server that should be in operation in the outside network. For a campus environment, the VM is allocated an IP address in the same way as the Host OS's interface to the campus network. The address will be different than that allocated to the Host OS. The campus wifi network (eduroam) is a bit problematic- you might need to connect your laptop to a campus ethernet. The easiest to get working is usually in your home environment where you have control over the WiFi AP.
Scenario 2 - Two VMs
Network ----Host -----VM1----VM2
We idenfity the following assumptions for this scenario:
- VM1 will be our current ubuntu 16.04 VM,
- VM2 will be kali 64 bit version 2016.2
- VM1 must be accessible from an external network. We will be setting up various servers (Apache, and possibly others) that you will need to access from the campus network (or from your home network)
We describe the setup in the following four steps. Step
Step 1: VM1 to Internet with NAT network
This is the 'one VM' setup described above.
Internet ----host os ------'hard wired link'---- VM1
------------ ....10.0.2.2 .................................10.0.2.15
VirtualBox setups up a 'hard wired link' allowing a VM (any number) to communicate through the Host using NAT. Don't be alarmed when you see all VMs are assigned an IP address of the same.
The VM will show a NAT network (Settings --- Networks ). Click Advanced and it will show the MAC address. VirtualBox uses a somewhat strange network scenario-it assigns all VMs to use NAT with an IP of 10.0.2.15.
To complete step 1, validate the network is as expected by:
- On VM1 issue ifconfig and make sure the IP is 10.0.2.15
- On VM1 issue traceroute google.com and make sure the first hop router IP is 10.0.2.2
Explanation: Host OS is performing NAT on behalf of flows intiated by VM1. Let's say our Host OS has an IP of 172.23.0.1. When VM1 issues a ping to a machine like netlab.cs.clemson.edu, the netlab host will think its interacting with a host with a source IP of the Host OS's IP address to the outside world.....when really the source is VM1!!
Step 2. Setup Host only Network or an internal network between VMs
Next, let's add VM2 and connect VM1-VM2 using a private network
Internet ----host os ------'hard wire'---- VM1 ----- private 192.168.1/24 ---- VM2
Turn off all VMs. In VirtualBox Manager, File --- Preferences---Network create a Host only network. Double click...and we see the default address of 192.168.56.1. I suggest turning DHCP OFF.
Or....instead of Host only, create an internal network.
Go to VM1 settings --- Network --- And should see Adapter 1 and Adapter 2 (NAT and Host Only). Double check in your VM, select Devices --- Network --- Settings : you should see Adapter 1 and 2.
Next, we need to configure Linux to use the second adapter. Got to Linux System Settings and Network. You should see 2 wired adapters: The interface for connecting to the outside world - it likely shows 10.0.2.15 for the IP, subnet of 255.255.255.0 and GW of 10.0.2.2. . Make sure to use DHCP for the Method (addressing). This setting will cause the IP host to include just one default route in the route table (check with 'netstat -rn' or 'ip route show' ).
The other interface, select Manual, Add a new entry to specify an IP of 192.168.2.1, subnet of 255.255.255.0 but Don't specify a GW! Go to routes, and select the button that says 'Use this connection only for resources on its network.' This is necessary to ensure that only 1 default route is set up.
To double check, on a terminal on VM1, issue ifconfig. You should now see both adapters with the IP addresses (10.0.2.15 and 192.168.2.1).
Note that the 'internal network' is even easier- no need to create a Host only network in Preferences....in fact you might need to remove any Host only networks defined that you are not using.
Step 3. Complete VM2 setup.
------------VM1-----------VM2
....................192.168.2.1 ----192.168.2.100
(Note: you can use any network addressing scheme you like....does not HAVE to be 192...Any private addresses will work. )
While powered off, go to settings and Network and disable (deselect) the NAT network adapter (in my case it is Adapter 1). But Adapter 2 should still indicate 'enable' and Host Only.
Start VM2. Issue an ifconfig. Repeat the steps above when we configured VM1's Linux Network Manager to use the Host Only adapter, assign a static IP 192.168.2.100. Make the default router be VM1's 192.168. address.
From VM2, you should be able to ping 192.168. but nothing beyond VM1. So a ping google.com will fail.
Step 4. Configure VM1 to serve as a NAT router
This is the magic step...setting up VM1 to allow VM2 to connect with the outside world. If successful, the IP addresses are as shown below. NAT is in affect at two locations:
- Host OS to outside
- VM1 to HostOS
Outside Network ---HostOS-------------VM1-----------VM2
........................................10.0.2.2 .....10.0.2.15 192.168.2.1 ------ 192.168.2.100
- Configure VM1 to
- To forward IP packets.
- 'sudo sysctl net.ipv4.conf.all.forwarding=1'
- To run NAT on outbound over 10.0.2.15
- 'sudo iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to 10.0.2.15'
Our Host OS continues to operate NAT for all traffic initiated by our VMs. Additionally, VM1 operates NAT on behalf of traffic initiated by VM2.
Problems/Troubleshooting:
If you have problems:
- On Windows, cd to "Program Files\oracle\VirtualBox" and do a dir
- On Linux and Mac Hosts, google to find the right location
- VBoxManage is a command line interface to VirtualBx
Additional VirtualBox Items
The following operations might come in handy
- The VBoxManage program on a VirtualBox Host allows much more customization and debugging help.
- On Windows, it usually is in 'Program Files\oracle\VirtualBox'
- Snapshots are highly recommended- it is a way to fall back on a recent and working instance of your VM. While it might not happen frequently, you are likely to encounter some sort of failure that might prevent your VM from booting ever again (or .... it might require significant effort to restore the corrupted system).
- Porting a VM snapshot to another machine allows you to create a customized VM, and then publishing it making it available to others. Later in the semester, we'll see that 'containers' are an alternative way to package and release applications (or sets of applications that provide more complex sets of applications that require a particular set of system software.
3/14/2017