Skip to content

Instantly share code, notes, and snippets.

@shinux
Forked from nepsilon/how-to-list-ssh-tunnel.md
Last active September 21, 2018 09:05
Show Gist options
  • Save shinux/431f0adda6481729a751e4ba516572e5 to your computer and use it in GitHub Desktop.
Save shinux/431f0adda6481729a751e4ba516572e5 to your computer and use it in GitHub Desktop.
How to list open SSH tunnels — First published in fullweb.io issue #34

How to list open SSH tunnels?

Very often a database in production only accept connections from localhost, and we resort to use SSH tunneling to access it remotely. While tunnels are running in the background and we connect to the database on localhost, we need to know if we’re really talking to our localhost or to the remote machine.

example for connect remote elasticsearch server

ssh -C -f -N -g -L 9200:127.0.0.1:9200 user@ipaddress

List all open SSH tunnels with:

sudo lsof -i -n | grep ssh

Some useful options to consider:

# -i shows the internet addresses
# -n does not resolve IP addresses

Remove all tunnels

pgrep ssh | xargs kill
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment