Immortal SSH Sessions

View Markdown Other Articles

Article written by a human: Mike Cardwell

For years now, I've been using a combination of mosh and screen to make my ssh sessions immortal. Here's how, and why:

On your server:

$ sudo apt install mosh screen

On your laptop:

$ sudo apt install mosh
$ echo 'mosh "$@" -- screen -s -/bin/bash -qRRUS "mosh-${HOSTNAME}"' | sudo tee /usr/local/bin/sshx
$ sudo chmod 755 /usr/local/bin/sshx

My Ansible common host role takes care of the above automatically for me.

Now instead of doing an ssh hostname you can do an sshx hostname

Why would you do this? Your SSH sessions can now survive network outages, they can survive your laptop changing networks and getting new IPs. They can survive your laptop being put to sleep for days. They can even survive rebooting your laptop; in that case you just need to type sshx hostname into a terminal again, after the reboot, to pick up where you left off.

I also use Terminator on my laptop for tabbed terminals which I can split horizontally and vertically as many times as I want. Well recommended.

No matter what commands I am running on my servers, I do not need to worry about losing my connection killing the command before it completes, and I can close my laptop lid without giving it a second thought. I do not have to re-ssh back into servers after waking my laptop.

The only complication is you'll probably want to learn a couple of things about screen. Most useful I've found is how to detach or re-attach a running screen and how to view the scrollback buffer. Here is a cheat sheet I found, which you might find useful.


  PayPal   Patreon   Bitcoin RSS   Atom   Mastodon   Bluesky
← Back to search results