tunnel via multiple hops in SSH

  • I am working locally on localhost
  • host1 is accessible to localhost
  • host2 only accepts connections from host1
  • I need to create a tunnel from localhost to host2

You basically have three possibilities:

  1. Tunnel from localhost to host1:
    ssh -L 9999:host2:1234 -N host1

    As noted above, the connection from host1 to host2 will not be secured.
  2. Tunnel from localhost to host1 and from host1 to host2:
    ssh -L 9999:localhost:9999 host1 ssh -L 9999:localhost:1234 -N host2

    This will open a tunnel from localhost to host1 and another tunnel from host1 to host2. However the port 9999 to host2:1234 can be used by anyone on host1. This may or may not be a problem.
  3. Tunnel from localhost to host1 and from localhost to host2:
    ssh -L 9998:host2:22 -N host1
    ssh -L 9999:localhost:1234 -N -p 9998 localhost

    This will open a tunnel from localhost to host1 through which the SSH service on host2 can be used. Then a second tunnel is opened from localhost to host2 through the first tunnel.

Normally, I’d go with option 1. If the connection from host1 to host2 needs to be secured, go with option 2. Option 3 is mainly useful to access a service on host2 that is only reachable from host2 itself.

References
https://superuser.com/questions/96489/an-ssh-tunnel-via-multiple-hops

Debug Remotely on Android using adb via SSH Tunnel

Start adb daemon on remote device

adb devices
$ adb devices
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
List of devices attached 
5200fe4259bcc000	device

do client to server port forwarding using ssh on port 5037

References
https://dontbelievethebyte.github.io/articles/2015/01/15/debug-remotely-on-android-via-ssh-tunnel/
https://developer.android.com/studio/command-line/adb
https://stackoverflow.com/questions/2604727/how-can-i-connect-to-android-with-adb-over-tcp

Tmux Cheat Sheet & Quick Reference

Start a new session

tmux
tmux new
tmux new-session

Start a new session with the name mysession

tmux new -s mysession

kill/delete session mysession

tmux kill-session -t mysession

kill/delete all sessions but the current

tmux kill-session -a

kill/delete all sessions but mysession

tmux kill-session -a -t mysession

Show all sessions

tmux ls
tmux list-sessions

Attach to a session with the name mysession

tmux a -t mysession
tmux at -t mysession
tmux attach -t mysession
tmux attach-session -t mysession

Rename session

Ctrl + b $

Detach from session

Ctrl + b d

Create window

Ctrl + b c

Rename current window

Ctrl + b ,

Close current window

Ctrl + b &

Previous window

Ctrl + b p

Next window

Ctrl + b n

Switch/select window by number

Ctrl + b 0 ... 9

Scroll

Ctrl-b [
# Press q to quit scroll mode
Ctrl-b PgUp

References
https://tmuxcheatsheet.com/
https://gist.github.com/MohamedAlaa/2961058

Keep Alive SSH Sessions

To enable the keep alive system-wide (root access required), edit /etc/ssh/ssh_config;
to set the settings for just your user, edit ~/.ssh/config (create the file if it doesn’t exist). Insert the following:

Host *
    ServerAliveInterval 300
    ServerAliveCountMax 2

You can also make your OpenSSH server keep alive all connections with clients by adding the following to /etc/ssh/sshd_config:

ClientAliveInterval 300
ClientAliveCountMax 2

References
https://patrickmn.com/aside/how-to-keep-alive-ssh-sessions/