Skip to content

Instantly share code, notes, and snippets.

@oeo
Created February 5, 2026 22:51
Show Gist options
  • Select an option

  • Save oeo/0e75a20469a323fe4efef907b52b2c2a to your computer and use it in GitHub Desktop.

Select an option

Save oeo/0e75a20469a323fe4efef907b52b2c2a to your computer and use it in GitHub Desktop.
Setup ngrok SSH tunnel from within WSL2
#!/bin/bash
# Setup ngrok tunnel for SSH access from within WSL2
# Run this script to create a public SSH tunnel
set -e
echo "╔══════════════════════════════════════════════════════════════╗"
echo "║ ngrok SSH Tunnel Setup (WSL2) ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""
# Check if running in WSL
if ! grep -q microsoft /proc/version; then
echo "ERROR: This script must be run from WSL2"
exit 1
fi
# Check if ngrok is installed
if ! command -v ngrok &> /dev/null; then
echo "[1/3] Installing ngrok..."
# Download ngrok
echo " → Downloading ngrok for Linux..."
cd /tmp
curl -s https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-linux-amd64.tgz -o ngrok.tgz
echo " → Extracting..."
tar xzf ngrok.tgz
echo " → Installing to /usr/local/bin..."
sudo mv ngrok /usr/local/bin/
sudo chmod +x /usr/local/bin/ngrok
echo " ✓ ngrok installed"
else
echo "[1/3] ngrok already installed ✓"
fi
# Ensure SSH is running
echo "[2/3] Checking SSH service..."
if ! sudo service ssh status | grep -q "running"; then
echo " → Starting SSH service..."
sudo service ssh start
sleep 2
fi
if sudo service ssh status | grep -q "running"; then
echo " ✓ SSH service is running"
else
echo " ✗ SSH service failed to start"
echo ""
echo "Try running: sudo service ssh restart"
exit 1
fi
# Check if SSH is listening on port 2222
if sudo netstat -tlnp 2>/dev/null | grep -q ":2222"; then
echo " ✓ SSH listening on port 2222"
else
echo " ✗ SSH not listening on port 2222"
echo ""
echo "Check SSH config: sudo nano /etc/ssh/sshd_config"
echo "Ensure: Port 2222"
exit 1
fi
# Start ngrok
echo "[3/3] Starting ngrok tunnel..."
echo ""
echo "╔══════════════════════════════════════════════════════════════╗"
echo "║ ngrok will display a URL like: ║"
echo "║ tcp://0.tcp.ngrok.io:12345 ║"
echo "║ ║"
echo "║ Share that URL to allow SSH access. ║"
echo "║ Press Ctrl+C to stop the tunnel. ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""
echo "Connection details:"
echo " Username: $(whoami)"
echo " Password: oijoij"
echo ""
echo "Starting ngrok tunnel on port 2222..."
echo ""
ngrok tcp 2222
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment