Setting Up A Local VPS

This article details the steps of setting up a local VPS or in other words a VPS on your local computer for offline use. The virtualization software used is Virtualbox. The server is a Ubuntu LTS server installed on the Virtualbox guest machine and made accessible over a Virtualbox managed network. The end result is a server that behaves just like a cloud VPS but is local. It can be used for testing scripts and configurations or for example as a staging replacement server for offline use. It could be a cheaper alternative to a cloud VPS. Another nice side effect of having the server locally is that it's faster and that restoring the machine to its start configuration is as easy as keeping a copy of the virtual disk and copying it as needed.

Creating the virtual machine

I will use Ubuntu 14.04 LTS server as the operating system to install on the Virtualbox guest. This will also determine where the configuration files can be found and what some commands look like. But the basic idea could be used with other Linux distros as well. Just the location of files and command lines may differ in some places. To install a Virtualbox Ubuntu guest, the following steps are necessary.

  1. Download the Ubuntu server ISO from here. This link should always point to the latest LTS version.
  2. Create a virtual machine as described here. (Virtualbox must obviously be installed on your host)
    • Open the Virtualbox manager window and click on the "New" icon. This will open the dialog to create a new virtual machine.
    • Select Linux as  "Operating System Type". And for version choose the version of the ISO you downloaded.
    • Choose default values for RAM. You can change it later.
    • Choose "Create a virtual hard drive" and then choose VDI as type.
    • Choose dynamically allocated file. It will save space.
    • Finish.
  3. Go to the Virtualbox Manager window, select the virtual machine you just created, then click on Settings.
  4. Go to Storage, click on the CD/DVD symbol in the middle box, then check "Live CD" on the right hand side, and click on the CD symbol on the right hand side. This will let you select the Ubuntu server ISO you downloaded in step 1. Select that ISO, then close the settings dialog.
  5. You are now ready to install the server onto the virtual machine.

Installing the server

In this step you select the virtual machine in the Virtualbox manager window, then click Start. The steps in the previous section have prepared the machine to boot from the server ISO as if it were a Live CD in the DVD drive of the virtual machine. After the machine is started, it will start the installation process, and the server will be installed onto the virtual hard disk. Follow the installation instructions and keep the following things in mind.

  1. Don't encrypt the hard disk or home directory. It will make life easier.
  2. Write down the following information as you create them.
    • username
    • user password
    • server name
  3. Select "OpenSSH server" for installation if given the option. It will be needed to connect to the server later.
  4. After the installation is finished, close down the machine and open the machine's settings again. Go to Storage, select the CD, and uncheck "Live CD", so that next time the machine is started it will boot from the virtual disk instead of the installation ISO.

And that's it. So let's move on to configure the network, so that we can actually reach the server from the outside.

Configuring the network

First you have to make sure Virtualbox has setup a network and what its range of IP addresses is. To check the Virtualbox network follow the steps below.

  • Open the Virtualbox manager.
  • Go to File > Preferences > Network and then select Host-only Networks.
  • By default there should be a vboxnet0 network. If you click on edit for that network it will show the IP address and network mask. For example IP 192.68.56.1 and network mask 255.255.255.0 means that you can choose IP addresses in the 192.68.56.xxx range. That is the default for vboxnet0.
  • Write down the IP range of the network. By default that range is 192.68.56.xxx. If there is no network, create one first by clicking on the create icon on the right hand side.

The next step is to "create" a network adapter for that network for the virtual machine. Select and highlight the virtual machine in the Virtualbox manager again, and click Settings, then go to Network. On the right hand side you should see one adapter attached to NAT. That is the internet connection for the virtual server. We need to enable another adapter for the vboxnet0 network. Click on Adapter 2, click the checkbox for "Enable Network Adapter" and choose "Host-only adapter" next to "Attached to" and choose vboxnet0 as the network name.

Start the virtual machine again. If you unchecked Live CD in the storage settings, the virtual machine will boot into the server operating system now. Login using the username and password you created during the installation. You have to edit the network interfaces now. In Ubuntu you can use the nano editor. On the command line type

sudo nano /etc/network/interfaces

Inside this file you can configure a static IP address for the network interface. This will be the fixed IP address you can reach the machine from later. Using a static IP address allows you to define an easy to remember and descriptive hostname for the machine later. Add the following lines to the interfaces file.

auto eth1
    iface eth1 inet static
    address 192.168.56.12
    netmask 255.255.255.0

A few things to keep in mind.

  • If you want to make sure that eth1 is the correct network interface, check ifconfig. It should be easy to figure out which interface name is the correct one.
  • If 192.168.56.12 is already used by another machine, you should choose another IP address from the 192.168.56.xxx range or whatever range your network (vboxnet0) uses.

To make the changes take effect and assign the configured IP address to your server, restart the adapter as follows (Ubuntu).

# restart the interface eth1 after editing /etc/network/interfaces
sudo ifdown eth1
sudo ifup eth1

If your server is not Ubuntu based and you are not sure how to restart the adapter, you can also reboot the server to make sure the adapter is using the correct IP address.

Configuring SSH access

Now it's time to test access to the server from the host. I will use a Ubuntu host, but if you have a different operating system, the commands may differ slightly. First make sure the server is reachable under the configured IP address by pinging it.

ping 192.168.56.12

If you get a response, the server is configured correctly and reachable at the address 192.168.56.12. Let's set up a more descriptive hostname for that. In most Linux based distros hosts are configured in /etc/hosts. Open that file and add the following line.

192.168.56.12     testserver

Save and close that file. Then ping the server at the new name.

ping testserver

And you should get a response.

When installing the server OS we made sure that OpenSSH server was installed on the server. That means you should now be able to SSH into the server.

ssh testuser@testserver
# enter your password at the prompt
# you are now logged into the server remotely

If you successfully logged into the server you can type "exit" and return back to the host shell.

In order to ssh into the server without password prompt you have to create a key pair on the host and add the public key to the server's authorized_keys. The following script does the job.

#!/bin/bash
# Enable passwordless ssh to testserver by creating a keypair and adding
# the public key to testserver's authorized_keys

if [ ! -f ~/.ssh/id_dsa.pub ] # check if public key already exists
    # key pair doesn't exist yet
    ssh-keygen -t dsa # this will create key pair (id_dsa, id_dsa.pub) in ~/.ssh
fi

scp ~/.ssh/id_dsa.pub testuser@testserver:~ # copy the public key to the testserver
ssh testuser@testserver "echo ~/id_dsa.pub >> ~/.ssh/authorized_keys"

(Snippet)

You will be prompted for a password when scp and ssh run inside the script. "testuser" corresponds to the username you created when installing Ubuntu on the server. scp copies the public key to the testserver. The ssh command executes the string starting with "echo ~/.ssh " as a command on the testserver. This results in the public key from id_dsa.pub being added to the the authorized_keys file. You should now be able to ssh into the testserver again, this time without being prompted for a password.

ssh testuser@testserver

And you're almost set. The last step is to eliminate the need to specify the username "testuser" in the ssh command. For that you need to create a ssh configuration in ~/.ssh/config, that contains the username. This can be done as follows.

#!/bin/bash
# Add a ssh configuration to ~/.ssh/config

echo "
Host testserver
Hostname testserver
User testuser
" >> ~/.ssh/config

(Snippet)

And now you can ssh into the server just by calling the "Host" name.

ssh testserver

Setting up passwordless sudo

In order to run scripts on the server with sudo privileges you may want to set up passwordless sudo. Otherwise the script cannot run without manually entering the sudo password every time a command is run with sudo privileges. In order to set up passwordless sudo on the server, you can ssh into the server and run the following commands.

#!/bin/bash

echo "$USER ALL=(ALL) NOPASSWD:ALL" > sudoers-$USER

chmod 0440 sudoers-$USER
sudo cp sudoers-$USER /etc/sudoers.d 

(Snippet)

This works on Ubuntu. You have to enter the password one last time when you run the last command of the script above. After that you can disconnect and reconnect to the server and run any sudo command. You will not be prompted for a password.

And there you go. You have your very own local VPS, very similar to an AWS EC2 instance. You can use it as a remote in git repositories or you can install a webserver on it and serve webpages and so on. Have fun using it!



Comments



Post A Comment