DevOps / Scripts bash Intermediate

Port Forward Tunnel Script

Open an SSH tunnel to forward a remote service port to localhost.

bash
#!/bin/bash
REMOTE_HOST=${1:-bastion.example.com}
REMOTE_PORT=${2:-5432}
LOCAL_PORT=${3:-15432}

ssh -N -L $LOCAL_PORT:localhost:$REMOTE_PORT $REMOTE_HOST &
TUNNEL_PID=$!
echo "Tunnel open: localhost:$LOCAL_PORT -> $REMOTE_HOST:$REMOTE_PORT (PID $TUNNEL_PID)"
echo "Kill with: kill $TUNNEL_PID"

Prerequisites

  • ssh
  • bastion host access

Pro Tips

  • → Add -o ServerAliveInterval=60 to keep idle tunnels open.

Was this module useful?

Share your experience — it helps the team improve.

Leave Feedback

Commercial Licensing

Acquire a production access token or template license for enterprise deployment.

Module Info

Language
bash
Level
Intermediate
Read time
2 min
Published
Jul 2026
License
Commercial License

Newsletter

New modules every week, directly to your inbox.