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.
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.
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.
And that's it. So let's move on to configure the network, so that we can actually reach the server from the outside.
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.
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.
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.
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.
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.
Save and close that file. Then ping the server at the new name.
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"
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.
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
And now you can ssh into the server just by calling the "Host" name.
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
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!