DEV Community

ignazio gandolfo
ignazio gandolfo

Posted on • Updated on

how to establish an SSH tunnel using Cloudflare to expose your local web server to the internet

Do you want to host your website or web application on your local machine, but don't want to deal with the hassle of setting up a public IP address or a dynamic DNS service? Then this guide is for you! In this article, we will show you how to securely expose your local web server to the world using an SSH tunnel through Cloudflare. This setup provides a secure and convenient way to access your local web server from anywhere in the world, without having to worry about managing a public IP address or setting up dynamic DNS.

Prerequisites

Before getting started, you should have the following:

  • A local machine running a web server, such as Apache or Nginx.
  • An account with Cloudflare.
  • A domain name that you can associate with your Cloudflare account.

Additionally, this guide assumes that you have a default installation of OpenSSH. If you have a firewall or other security measures in place on your local machine, you may need to configure them to allow incoming traffic to your web server.

Step 1: Set up a Cloudflare account and add your domain

  1. Go to Cloudflare.com, sign up for a free account, and log in.
  2. Click on the "Add a website" button.
  3. Enter your domain name and follow the on-screen instructions to add your domain to Cloudflare.

Step 2: Configure your local machine to allow incoming SSH traffic

  1. Open a terminal on your local machine.
  2. Run the following command to allow incoming SSH traffic:
sudo ufw allow 22/tcp
Enter fullscreen mode Exit fullscreen mode

Step 3: Set up an SSH key pair on your local machine

  1. Open a terminal on your local machine.
  2. Run the following command to generate an SSH key pair:
ssh-keygen -t rsa
Enter fullscreen mode Exit fullscreen mode
  1. Follow the on-screen instructions to create the key pair.
  2. Copy the contents of the public key, which is usually located in ~/.ssh/id_rsa.pub, to your clipboard.

Step 4: Add the public key to your Cloudflare account

  1. Log in to your Cloudflare account.
  2. Navigate to the "Crypto" section.
  3. Scroll down to the "SSH/TLS Certificates" section.
  4. Click the "Add Certificate" button.
  5. Paste the public key you copied in step 3 into the "Public Key" field.
  6. Give the certificate a name and click the "Add Certificate" button.

Step 5: Create an SSH tunnel on your local machine

  1. Open a terminal on your local machine.
  2. Run the following command to create the SSH tunnel:
ssh -R 80:localhost:80 <username>@<domain>.ssh.cloudflare.com
Enter fullscreen mode Exit fullscreen mode
  1. Replace <username> with your Cloudflare username, and <domain> with your domain name.
  2. The -R flag tells OpenSSH to create a reverse tunnel from port 80 on the Cloudflare servers to port 80 on your local machine.

Step 6: Test the connection to your local web server

  1. Open a web browser and go to http://<domain>.
  2. You should see the default page of your web server, which is hosted on your local machine.

Once the SSH connection is established, you can now test the connection to your local web server. Testing the connection ensures that the setup is complete and you can now use your domain name to access your local web server from anywhere in the world.

Conclusion

Exposing your local web server to the world is a convenient and secure way to access your web server from anywhere, without having to worry about managing a public IP address or setting up dynamic DNS. Using an SSH tunnel through Cloudflare provides a secure and easy-to-set-up solution for accessing your local web server from anywhere in the world.

Note that this setup is intended for development and testing purposes only. If you want to host a public-facing website, you should consider using a more robust solution, such as a dedicated server or a cloud-based hosting service.

Oldest comments (0)