How to specify public port to receive tcp connections with ngrok

2 min read 06-10-2024
How to specify public port to receive tcp connections with ngrok


Bypassing the Default: Specifying Public Ports with ngrok for TCP Connections

Ngrok is a powerful tool for easily exposing local servers and applications to the internet. While it works seamlessly for most scenarios, you might encounter situations where you need to specify a specific public port for receiving TCP connections. This article will guide you through the process of configuring ngrok to achieve this.

The Problem:

By default, ngrok automatically assigns a random public port for every tunnel it creates. This dynamic port allocation works great for quick testing and basic access, but sometimes you need more control over which ports are used. For example, if you're working with a service that requires a specific port (like an FTP server on port 21), ngrok's default behavior can be an obstacle.

The Solution:

Ngrok provides a simple command-line flag to manually specify the desired public port for your TCP tunnel. Here's a breakdown of the steps:

  1. Install ngrok: If you haven't already, download and install ngrok from the official website: https://ngrok.com/download

  2. Launch your local server: Ensure your local server or application is running on the desired port. Let's assume your server listens on port 5000 for this example.

  3. Configure ngrok: Open your terminal and run the following command, replacing [your_ngrok_authtoken] with your ngrok authtoken if you're using a paid plan:

    ngrok tcp -bind=5000 -host-header=localhost -port=2222 [your_ngrok_authtoken]
    
    • tcp: Specifies that you want to create a TCP tunnel.
    • -bind=5000: Tells ngrok to bind the tunnel to your local server running on port 5000.
    • -host-header=localhost: Ensures that requests made to the public port will be correctly routed to your local server.
    • -port=2222: Specifies the desired public port for the tunnel (in this case, 2222).

Important Notes:

  • Port Availability: Make sure the desired public port (2222 in our example) is not already in use on your system.
  • Firewall Rules: You might need to configure your firewall to allow incoming connections on the public port you specify.
  • Authentication: If you have a paid ngrok account, you can use your authtoken to ensure more secure access to your tunnels.

Example Use Cases:

  • FTP Server: Create a secure FTP server on your local machine and expose it to the internet using ngrok. By specifying a public port like 21, you can connect to your FTP server remotely.
  • Database Access: Access your local database from remote applications by specifying the public port used by your database server.
  • API Testing: Set up a local API server and use ngrok to test it from other machines or environments.

Conclusion:

Specifying a public port with ngrok gives you more control over how your local services are exposed online. This is especially helpful when dealing with specific port requirements, security considerations, or when working with services that rely on predictable connections. By following the steps outlined above, you can easily configure ngrok to create a reliable and secure connection to your local applications.