Exposing Local TCP Services to the Public Internet with Ngrok Tunnels
Learn how to expose local databases and TCP-based services using ngrok's tunneling protocol to bypass NAT and firewalls without manual port forwarding.
03 Aug 2026, 15:47 UTC

Exposing a local-only service—such as a PostgreSQL database, SSH server, or custom API—to a remote client is often blocked by Network Address Translation (NAT) or restrictive corporate firewalls. Manual port forwarding on a router is frequently impossible in cloud environments or shared network setups. The most efficient solution is using a reverse tunnel to create a secure bridge between a public edge server and your local machine, allowing external traffic to reach your local loopback interface without changing network-level settings.
The Mechanism of TCP Tunneling
Ngrok operates by establishing a persistent TLS-encrypted connection between the local ngrok agent and the ngrok cloud platform. When a remote client connects to the public endpoint provided by ngrok, the cloud platform multiplexes that traffic through the established tunnel. The local agent then decapsulates the traffic and forwards it to the specified local port. This bypasses the need for inbound firewall rules because the connection is initiated from inside the network.
Configuration: Exposing a Local Database
To expose a local PostgreSQL instance running on default port 5432, you can use the command line for quick testing. However, for repeatable engineering tasks, using a configuration file is preferred to ensure consistency across session restarts.
Quick Start via CLI
Run the following command in your terminal (where the ngrok agent is installed):
ngrok tcp 5432
Once executed, the terminal will display a 'Forwarding' address, similar to 0.tcp.ngrok.io:12345. This address is what your remote client will use to connect.
Production-Ready Setup with ngrok.yml
To avoid ephemeral ports changing every time you restart the agent (on paid tiers), define a static tunnel in your ngrok.yml file (typically located in ~/.ngrok/ or the binary directory):
ngauthtoken:
tunnelsunnels:
db-tunnel:
proto: tcp
addr: localhost:5432
Start the tunnel using the configuration file:
ngrok start --config ngrok.yml db-tunnel
Verification and Diagnostics
After the tunnel is active, you must verify that the traffic is flowing correctly.
- Check the Local UI: Ngrok provides a local web interface at
http://localhost:4040. This dashboard shows active connections, request latency, and traffic logs. - Test Connectivity: Use a remote client or a database GUI to connect to the public endpoint. For example, using telnet from a remote machine:
telnet 0.tcp.ngrok.io 12345
Engineering Limits and Security Considerations
While tunneling solves connectivity issues, it introduces specific risks and constraints that must be managed:
- Attack Surface: Exposing a service to the public internet makes it visible to automated scanners. Ensure the underlying service has robust application-level authentication (e.g., strong passwords, SSH keys, or DB tokens).
- Free Tier Constraints: Free accounts receive dynamic TCP addresses. If the ngrok agent restarts, the port number will change, requiring updates to your client-side connection strings.
- Latency: Because traffic travels through an ngrok edge node before reaching your machine, there will be inherent latency compared to a direct connection. Choose the region closest to you using the
--regionflag (e.g.,--region us).
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.