It's not uncommon to encounter a situation where you connect to a Unix server via SSH and work with it, and then suddenly the connection drops. When you reopen the SSH session, you naturally don't find the work that was done before the disconnect - for example, if something was being edited in nano, ee or mcedit and you didn't save it - well, tough luck, as they say. Similarly, open programs, such as Midnight Commander, remain hanging in memory.
The desire to see your unfinished work again after reconnecting is understandable and makes sense. But "out of the box" SSH can't do this - you're on your own.
And what if you had some long operation running (say, the same kernel rebuild or compiling a bunch of packages on FreeBSD)? Here, losing the SSH connection can turn into, if not a disaster, then very unpleasant moments.
A utility called screen can come to our rescue. It doesn't allow you to restore the session as such, but it provides a tool along the lines of "prepare in advance - reap the benefits".
Its meaning is this: you log in to the server via SSH and, instead of doing something right away, you first launch the screen utility, and only after that - within its console - do you work as if in a regular SSH session. In case the connection drops, after reconnecting via SSH, the utility will let you reconnect to its session and continue working.
Installationscreen doesn't come out of the box on either Linux or FreeBSD. So let's install this valuable utility.
For Linux Debian/ubuntu:
$ sudo aptitude install screen
For Linux CentOS/RedHat:
$ sudo yum install screen
For FreeBSD:
$ cd /usr/ports/sysutils/screen
$ sudo make install clean
for the configuration question - leave everything at default.
Connecting (creating a session)After connecting via SSH, simply type in the console:
$ screen
and that's it. The utility will show you a splash screen, from which you can tell that it's free and that you need to press Enter or space to continue.
And then... then you'll find yourself back in the shell console (or bash - or whatever you use as your interpreter). That is, you'll see the same command line, as if the utility simply exited.
But it hasn't exited - from here you're working through screen and can not worry about the connection dropping.
How to use itIt's not enough to just start a screen session - you also need to learn how to use this utility.
Exiting
To exit the screen session and close the terminal (in this case you won't be able to reconnect to it) - just type exit - as if you were exiting an SSH session. After pressing Enter, you'll be thrown back to the parent SSH session.
Control within Screen
The utility supports a number of hot keys - for control within its screen. These commands are invoked by pressing Ctrl+A and then the key for the desired action.
The main combinations are listed below:
- Ctrl+A, ? : Show command help
- Ctrl+A, c : Create a new Screen session
- Ctrl+A, n : Switch to the next Screen session
- Ctrl+A, p : Switch to the previous Screen session
- Ctrl+A, # : Switch to the session with number # (instead of # - substitute the Screen window number)
- Ctrl+A, " : Show the list of open sessions
- Ctrl+A, Shift+C : Clear the window
- Ctrl+A, Shift+F : Fit the Screen session window size to the current SSH window size (for example, if you resized the PuTTY window or the X-Window terminal)
- Ctrl+A, Shift+K : Close the window (similar to exit, but forceful)
- Ctrl+A, d : Detach the window - i.e. you'll return to the SSH session, but all running programs will remain running in the Screen session.
How to reconnectSo, your SSH session has dropped, or you simply detached the Screen window using the key combination "Ctrl+A, d" - and now you need to reconnect.
You can get a list of current windows by running the following command:
$ screen -ls
There is a screen on:
61469.pts-2.mail (Detached)
1 Socket in /tmp/screens/S-user.
And to connect to the desired session:
$ screen -r 61469.pts-2.mail
where instead of 61469.pts-2.mail specify the session you see from the previous command.
That's it, in a nutshell, about this wonderful utility.
Applicable to: Linux Debian, CentOS/RedHat; FreeBSD...
Comments