ssh heaven


My internet provider seems to disconnect open-but-inactive sockets; including my ssh sessions into my server. I have therefore found some low-tech solutions that not only resolves the issue, but dramatically improve my ssh sessions in general.


First of all, make sure tmux is installed, e.g apt-get install -y tmux, next make sure tmux launches automatically during ssh sessions by adding this to your .bashrc

1if [[ -n "$PS1" ]] && [[ -z "$TMUX" ]] && [[ -n "$SSH_CONNECTION" ]]; then
2  tmux attach-session -t ssh_tmux || tmux new-session -s ssh_tmux

Since we always attempt to re-enter the tmux session named ssh_tmux, if you want to resume where you left off, you can simply close the terminal tab once you're done. If you want to start a fresh tmux session each time, then ctrl d out of your tmux session before exiting your ssh session.

Secondly, add this to .tmux.conf:

1set -g status-right %H:%M:%S
2set -g status-interval 1

This adds seconds to the tmux digital clock at the bottom right, and updates the clock every second, which drum roll keeps sending data down the SSH session every second, and prevents your internet provider from terminating the session.

Pasted image 20210217075630.png


Another addition to this setup is mosh which is designed to provide stable ssh sessions over slow connections.

Mosh makes it feel like i'm connected to a local ssh session despite my server having a ping of over 300ms due to my geographical distance. The white line under the text represents the input that is still currently in transit. Reducing the typing latency down to 0ms makes typing over ssh sessions fun again.

ssh conf

As always, make sure you have configured your server, and localhost to automatically use your ssh keys, to never have to type an ssh session password again.

bash alias

To top it off, make sure you set an easy alias, to quickly jump into an ssh session whenever required.

1alias s='mosh'