Access Your Remote Server as Localhost Using SSH Tunneling

AWS
IT
Security

By Daniel Laufenberg on 12/29/2024

By Daniel Laufenberg on 12/29/2024

ssh-tunneling

If you have deployed software in the past, you know that anything you host on a remote server is not directly accessible from your computer. You would usually need to RDP into the server or connect to the server via SSH, as we should. There are many obvious reasons why you would not want to directly connect your computer to the deployment server, most notably for security reasons.

But what if there were a way to connect to a remote server in such a way that it feels like you are connecting to a local system on your computer? We can create this behavior using SSH tunneling.

Basics of SSH Tunneling

SSH tunneling allows you to securely forward a port from a remote server to your local machine over an encrypted SSH connection. This means you can access services running on the remote server as if they were available on your own computer. Instead of exposing these services to the internet, which would introduce security risks, SSH tunneling keeps them protected behind a secure channel accessible only to you.

Let's look into how we can achieve this with a virtual machine hosted on AWS.

Spinning Up CouchDB

Let's spin up a CouchDB instance using AWS EC2 and connect to it via localhost.

Step 1: Log in to the AWS Portal

Access the AWS Management Console using your account credentials. This is where you will configure and manage your EC2 instances.

Step 2: Launch a CouchDB Instance Using the Bitnami Package

Navigate to the AWS Marketplace Bitnami CouchDB package and launch a pre-configured CouchDB instance. This saves you the time and effort of installing all dependencies manually.

AWS Marketplace Bitnami package for CouchDB

You can choose any configuration you want to try, but for this demonstration, the cheapest virtual machine will suffice.

WS Marketplace Bitnami package for CouchDB config

Key Pair Settings: Make sure to either generate a new key pair or use an existing one that is available and configured.

Step 3: Configure the Instance

Once your instance is running, take note of the public IP address or public IPv4 DNS name. You can find this information in the EC2 instance details in the AWS Console.

instance details EC2 us-east-1

Step 4: Allow SSH Access

  1. In your EC2 instance settings, scroll down to find the security group applied to the instance.
  2. Click on the security group link.
  3. Update the Inbound Rules to allow SSH (port 22) access.

For best security practices, restrict access to your current public IP address by specifying it with /32 CIDR notation.

Step 5: Set Up SSH Tunneling

Finally we are ready to set up SSH tunneling!

  1. In the AWS Console, select Actions -> Connect -> SSH Client.
  2. Follow the provided instructions to ensure that you can establish a working SSH connection.

Once verified, create an SSH tunnel using the following command:

ssh -i /path/to/private-key.pem -L 5984:localhost:5984 bitnami@your-ec2-public-ip

Replace /path/to/private-key.pem with the path to your private SSH key, and your-ec2-public-ip with the public IP address of your EC2 instance.

Step 6: Access CouchDB Locally

Before accessing CouchDB:

Using the existing SSH session, run the following command to retrieve your CouchDB credentials:

sudo cat /home/bitnami/bitnami_credentials

Take note of the username and password displayed.

Now visit the URL below:

http://localhost:5984/_utils

You will be prompted to provide a login, enter the username and password you retrieved earlier.

couchdb_via_localhost

And there you have it!

You are now accessing the remote CouchDB server's admin interface through your localhost.

This approach offers a much better experience compared to using RDP and interacting with a remote-controlled UI.

Thanks for reading!

Shameless promotion | Jargon Vault

Now we have a CouchDB instance running, but there is not much we can do with it yet.

What if there were an application that allows you to connect to any data source you choose, giving you full access and control over your data while still taking advantage of the application's full feature set? Jargon Vault does exactly that.

You can connect your CouchDB instance to Jargon Vault and utilize all of its features while maintaining complete ownership and control over your data.

jargon vault logo

Jargon Vault

Jargon Vault instantly highlights and expands jargon, acronyms, and abbreviations on any webpage and shows clear definitions with a single click.