Creating reattachable SSH sessions with mosh + screen
This is a work in progress post that I’m writing for myself when I need it in the future.
You connect to a remote server via SSH and run a command that takes too long to execute (e.g. a huge backup routine). Then, for whatever reason, your SSH session is terminated and the command you were running stops running and you have to do it all over again.
How it should work
You connect to the remote server via SSH and execute your commands. If for any reason you are disconnected, your session will remaing active within the remote server and your command will keep running. You can then reconnect to the server and restore the previous session.
In order to achieve what we want, we’re gonna use mosh to increase connection stability and screen to create and manage reattachable sessions.
For this tutorial, I will assume we’re using Ubuntu on both the client and the server. Some commands may differ in other Linux distributions.
Step 1: Prepare the client
All you have to do on your client is to install mosh:
$ sudo apt install mosh
Step 2: Prepare the server
$ sudo apt install mosh screen
On the server you’ll install both mosh and screen:
In order for mosh to work you need to make sure that the firewall allows connections on the 60000-61000 UDP range on the server.
Step 3: Connect to the server using mosh
There is no secret here. You will connect to the your server just like you do with SSH:
$ mosh <user>@<address>
Mosh will establish a tunnel with the server and then initiate a SSH session through this tunnel. The Mosh tunnel is very resilient and won’t disconnect easily. You can even go offline for several seconds and as soon as you’re back online your Mosh session will resume as if nothing had happened.
Step 4: Start a Screen session
One you are logged in the remote server, you can create a session with screen. This is what will ensure that if anything goes wrong and you are disconnected, you’ll be able to reattach to your session and resume what you were doing.
First, simply execute the command:
Then, create a new window by pression Ctrl+a then c and you’re set.
Screen is very powerful and has a lot of feaures that can make your life much easier. There are many guides all over the internet covering its usage, like this one one Linuxize.com.
Now, if for any reason you are disconnected from the server, all you have to do to reconnect to your previous sessions is to follow a simple 3-step procedure:
- Reconnect to the server using Mosh;
- Execute the command
screen -lsto get a list of all the existing screen sessions;
screen -r <session number>to reattached to the session.