How do I access a private database from my laptop through a jump server?

Quick Linux Tip #39:

Try: ssh -L 3307:db-internal:3306 -N -f linuxteck@jumphost

Info: -L local_port:target_host:target_port forwards local traffic through SSH to a remote server. -N skips the remote shell, and -f sends SSH to the background.

Examples:

  • $ ssh -L 8080:localhost:80 user@server  # Access remote web app locally
  • $ ssh -R 9000:localhost:9000 user@server  # Expose local port on remote server
  • $ ssh -D 1080 user@server  # Dynamic SOCKS proxy for web traffic

Note: Kill active background tunnels with pkill -f 'ssh -L 3307'. Add -o ExitOnForwardFailure=yes so SSH fails immediately if the local port is already bound.




LinuxTeck.com
linuxteck@ubuntu:~$ ssh -L 3307:db-internal:3306 -N -f linuxteck@jumphost

linuxteck@ubuntu:~$ lsof -i :3307
COMMAND   PID    USER       FD   TYPE DEVICE SIZE/OFF NODE NAME
ssh      14209   linuxteck   3u  IPv4   89201      0t0  TCP localhost:3307 (LISTEN)

linuxteck@ubuntu:~$ mysql -h 127.0.0.1 -P 3307 -u dbuser -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Server version: 8.0.35-0ubuntu0.22.04.1
mysql>

PREVIOUS ARTICLE Quick Linux Tip #38: How do I make my custom app automatically restart on crash? NEXT ARTICLE Quick Linux Tip #40: Which files changed during the incident window last Tuesday?
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.