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.
The problem
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 remain active within the remote server and your command will keep running. You can then reconnect to the server and restore the previous session.
The solution
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 INPUT 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:
$ screen
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 -ls
to get a list of all the existing screen sessions; - Execute
screen -r <session number>
to reattached to the session.
Voilà.
Updates
- September 20, 2022: Sal left a comment reminding us that if your mosh session is disconnected the process will remain active on the server and you’ll have to kill it manually the next time you log in. Thanks for the reminder, Sal! 😄
Have to kill the previous mosh session, too.