How to save and retrieve a ssh session with the Unix/Linux screen program
Often times while performing critical work as a Systems Administrator, a session will become disconnected. Whether it be a high-latency vpn, or simply low timeout thresholds, often times efforts must be duplicated when performing some kind of work remotely over ssh.
One solution to the problem is to use the Unix screen utility. The screen utility will allow a session to be saved without an interruption the job or process that is running as a child process under screen. Screen comes in handy when performing work over high-latency networks, connections with low timeout thresholds, or just simply coming back to work on something without having to start the process that was being worked on from the very beginning.
For example, lets say a systems administrator wants to start a vi session to edit /etc/snmp/snmpd.conf. During the process of editing this file, the ssh connection drops off and the vi process is still running with the configuration file open. When the systems administrator logs back on to the machine and tries to edit the same file with vi again, a warning appears that the original vi process is still running, the original edit was never saved (so the changes are lost) and it’s just a mess overall.
Screen is the answer to this problem. To use screen do the following
1. Login to the remote server via SSH.
2. Start the screen session.
# screen
The original bash shell will re-appear
3. Here you can run commands.
For example: Start to edit any file or run any process.
# vi test.sh
4. To come out of screen then use “Ctrl-A” + “d”.
The screen session will now return to the normal SSH session.
5. Exit from the SSH session.
# exit.
6. To take control back to the “vi” process,
Again, login to system using SSH.
7. To list the currently running screen sessions use following command
# screen -ls
The following is the sample output.
<snip>
[root@test~]# screen -ls
There is a screen on:
7920.pts-3.test (Detached)
1 Socket in /var/run/screen/S-root.
</snip>
8. Connect to the screen session.
# screen -r 7920.pts-3.tes
For more information about screen, see the following link, or the screen man page:
http://www.rackaid.com/resources/linux-tutorials/general-tutorials/linux-screen.cfm
You must be logged in to post a comment.

















Leave a Reply