1. Access Windows-host shared folders from Ubuntu-guest
This is the scenario where you run Windows as your host OS and Ubuntu inside the VirtualBox, and that you want to access a specific Windows folder from Ubuntu.
First you have to make sure that have install Guest Additions. From the VirtualBox’s menu go to Devices → Install Guest Additions… This will mount a virtual CD on your /media/cdrom. Normally this folder’s window will show up. As root run the program VBoxLinuxAdditions.run. When the program completes reboot your VirtualBox.
With Guest Additions installed you may now go ahead and define the shared folder(s). From the VirtualBox’s menu go to Devices → Shared Folders. A dialog will show up. In this dialog you can specify which folder from your Windows system you want to share with your Ubuntu. Press the button with the + symbol to add a new shared folder in the list. You will have to specify a Folder Name for each folder you add. Make sure you memorize that name because you will need it very soon.
When done with you shared folder(s) specification, you may now go ahead and actually mount these folders from Ubuntu. First you have to create a mounpoint, that is, a directory in your Ubuntu which will reflect the shared folder from Windows:
# sudo mkdir /media/windows-share
Of course you may choose an alternative path for your mountpoint. With your mountpoint created you can now mount the shared folder, like this:
# sudo mount -t vboxsf folder-name /media/windows-share
Where folder-name will be the name you assigned for this folder when you were adding it in the shared folders list.
You could use the /etc/init.d/rc.local script to execute these commands on startup to have the shared folders automatically mounted every time you start your Ubuntu VirtualBox.
2. SSH into a VirtualBox guest OS from LAN/external network/host OS or use guest OS as a VPS
First thing first (you should be able to set the host port as 22 as well)…
The best way to login to a guest Linux VirtualBox VM is port forwarding. By default, you should have one interface already which is using NAT. Then go to the Network settings and click the Port Forwarding button. Add a new Rule:
Host port 3022, guest port 22, name ssh, other left blank.
or from command line
VBoxManage modifyvm myserver --natpf1 "ssh,tcp,,3022,,22"
where ‘myserver’ is the name of the created VM. Check the added rules:
VBoxManage showvminfo myserver | grep 'Rule'
That’s all! Please be sure you don’t forget to install an SSH server:
sudo apt-get install openssh-server
To SSH into the guest VM, write:
ssh -p 3022 user@127.0.0.1
Where user
is your username within the VM. This will allow you to SSH into your guest OS from the host OS (sounds silly, isn’t it…)
From another computer on the LAN network, you will use the host OS ip address to SSH into the guest OS.
3. VirtualBox: How to use a VirtualBox guest OS as a VPS
Oracle’s VM VirtualBox is a virtualization program that allows you to run another operating system from within your native operating system. Though it is most commonly used to run fully functional operating systems such as Linux or OS X from within Windows 7 (or vice versa), it can also be used to host a Virtual Private Server (VPS).
This post does nothing to compare benchmarks between more efficient (and recommended) VPS environments such as VMware or Linux-VServer, and I would not recommend using VirtualBox as a VPS in a production environment. However, it is useful in many situations, and I’ll let you be the judge of when this should or should not be done. It is certainly acceptable for personal and developmental purposes. And hosting a VPS through something like VirtualBox that is extremely simply to setup and use allows you to easily experiment with configurations and operating systems, or even jump between multiple VPSs on the same computer.
This tutorial assumes you have a rudimentary knowledge of server software and operating systems. I’m going to be explaining virtualization to you, not the details of the server installation and configuration.
Setting Up VirtualBox
First, some definitions. When I refer to the host operating system, that is the primary operating system that your computer boots into. When I refer to the guest operating system, that is the virtualized system that is run from within VirtualBox. There will also be references to IP address and ports on the host and guest. They follow the same theme. Now that we’ve got that of the way …
You can pick up VirtualBox for free. Download and run the installer for your host operating system. Congratulations. VirtualBox is now ready to run. Unfortunately, it doesn’t have a guest operating system installed or configured yet, so it doesn’t do much for you. But before we actually install one of those, let’s create a virtual environment for it and configure some VirtualBox settings.
In VirtualBox, click New to create an environment where we install a guest operating system. I’m assuming you’re a civilized human being and installing a Linux server operating system, so select Linux, then select the version of operating system you’re using. If the exact version isn’t in the VirtualBox list, select the parent Linux distribution (for instance, for CentOS you’d select Fedora).
Ideally, you should grant at least half of your host system’s memory to the guest operating system. You should dedicate at least 8GB to the guests hard drive space. Luckily, since this is a virtual environment, you can select to dynamically allocate this space, so the virtual hard drive will only consume space on your host’s hard drive as it is needed. Finish up the wizard, and the guest environment will be created.
Now, to make that guest environment accessible to our host computer. Right-click on the newly created environment and select “Settings”. Click on “Network” in the list on the left, and click on “Adapter 2”. Enable this adapter and, from “Attached to:” select “Bridged Adapter”. This will cause the guest environment to resolve DHCP IP information directly from the host operating system, which means we can now forward some host ports directly to the guest operating system.
Go back to the “Adapter 1” tab, make sure this adapter is “Attached to: NAT”, and click “Advanced”. Click on “Port Forwarding” and add a new TCP forward. Let’s call it “SSH”. Specify 22 for the host and guest ports. This will forward the host machines port 22 to the guest machines port 22—they don’t have to be the same, they just have to match other configurations on the host and guest side of things. It’s also worth adding an “HTTP” forward for port 80 as well as any other the forwards for ports controlling any other services you’d like accessible from the guest environment.
Server Operating System
If you haven’t already, now’s the time to choose what operating system you’re going to use for your guest environment. I recommend Ubuntu Server if you’re used to Ubuntu or Debian environments, and CentOS is another wildly popular one, though it’s not my cup of tea. Whatever operating system you choose, download the ISO for it’s installation and open up VirtualBox again.
Right-click on your guest environment and select “Settings”. From the list on the left select “Storage”, and point your virtual disc drive to the ISO you just downloaded. Once this is done, you can simply start the guest environment and it will boot with that disc “in the drive”, so you can install that operating system in the guest environment.
If you’re installing Ubuntu Server, selecting OpenSSH during the install process as well as LAMP and any other services you’d like available will make things much easier for you. However, as I said above, this tutorial assumes you have a rudimentary knowledge of server operating systems, so I’m not going to go into the details of installing those services. But to prove that our port forwards worked, you should at least install OpenSSH (during installation or as soon as you boot into the environment), and if you are able to SSH to your host computer on port 22 and access the guest environment, then everything worked the way it should have.
Launching Server When Computer is Booted
It may be useful to launch this virtual server when the computer boots. To do this, create a BAT file with the following command:
VBoxManage startvm “VM Name” –type headless
Place a shortcut to this BAT file in the Startup folder of a (or all) user accounts and you’re good to go. The server will launch and run in the background, allowing you to SSH into the server to control it from a terminal.
For maintenance purposes, you may also want to create a second BAT file for stopping the server (since it’s running in the background with no visible window). To do so, create a BAT file with the following command:
VBoxManage controlvm “VM Name” poweroff
Access from External IP
Login to your router and go the Port Forwarding section. Add a new port 22 forward, and forward that port to the IP address of the host. Do the same for port 80 and any other ports you added during the configuration above. Now, by typing in the external IP address of your network, you can SSH into the guest operating system through port 22, and you can utilize other services available to other ports.
There’s a lot more than can be done from here (using DNS to propagate to your external IP address, mail servers, etc.), but this tutorial has gotten you to the point where you can use tutorials for non-virtualized environments tutorials to accomplish those goals now.
Comparison of VirtualBox network modes