VM Tunneling
How to host remote services without having to mess with router configurations
Project Details
I discovered VM Tunneling when learning different ways to make my Minecraft Server publicly accessible. To put it simply, VM tunneling is just routing remote traffic through a Virtual Machine in the cloud. To accomplish this I will be using SSH, more specifically SSH tunneling. With a few simple commands you will be able to deploy any web server to the wide web without having touch any router configurations. Plus, this is so stunningly simple to do.
Step 1 ) Acquire your Cloud VM: There are many different options when looking for a cloud hosted VM since there are so many different companies that offer it (some are free). While it is only required that this vm has a publicly available ip address and you are able to use its shell, you want to ensure you are selecting a VM with a good location relative to your physical address. This is because we are routing our traffic through an encrypted tunnel which will cause lag if the distance is too far. (For reference, I use a VM hosted in Atlanta Georgia while I live in Southern Virginia.
Step 2 ) Create SSH Keys: First, it is important that you are securely handling these keys as they allow passwordless logon to the server. So be sure to store them in a secure location and remember where you store them. In the folder that you would like your keys stored, run the command # > ssh-keygen -t rsa -b 4096. This command will prompt you for some information needed to create a public and private key pair. Now, from that same folder use the command ssh-copy-id -i [name you typed in] [VM username]@[VM ip address] . This will send your key over to your VM, now test it out with ssh -i [path/to/key] [VM username]@[VM ip address]
Step 3 ) Download AutoSSH and setup a service: AutoSSH is a lightweight *nix program that is good for keeping SSH connections alive. So, to install it search if your package manager has it (most do) and install it just like you would install any other program through the Mac/Linux terminal. Once autossh is installed, start your service that you would like to expose and take note of the port it operates on. Now, at the bottom of this page I included a button that takes you to a Template for an autossh systemd service. Either download that file and replace the values, or create your own file. Once the values are correctly changed, move this file into your /etc/systemd/system folder. sudo mv autossh.service /etc/systemd/system . Once you've done this, ensure your service is running and run sudo systemctl start autossh . Now, make sure your local machine and VM allow traffic on their respective ports and verify functionality.
Good to Know: To view if the service is active and running use sudo systemctl status autossh, To view the logs use sudo journalctl -u autossh, Restart with sudo systemctl restart autossh
Tech Used: Any VPS, SSH, AutoSSH, Linux Systemd