16 January 2014

Resume ssh session with linux screen command

First look

You can extend the ssh connection time out by the param ServerAliveInterval as I have mentioned before. But if the connection is accidentally disconnect (maybe by network problem), how can you get resume to your ssh session again ?

Linux screen command is the rescure in such these situations. With screen command you can create as many virtual session terminal as you want.

I myself right after sucessfully ssh logon, I start screen immediately to get into virtual terminal. If there is any problem causing the connection lost, I can resume back to the right terminal easily.

How to use screen command

Check if you have installed the screen package. If not, just install it.

# yum install screen

- To enter a virtual screen : #screen
- To list if there is any screen open before :

[root@myPC ~]# screen -ls
There is a screen on:
        2664.pts-1.myPC (Detached)
1 Socket in /var/run/screen/S-root.

- To resume a screen : # screen -dr 2664

When you are already in a screen :

- To list all screens : Ctrl + a then "
- To create a new screen : Ctrl + a then c
- To jump to next screen : Ctrl + a then n
- To jump to previous screen : Ctrl + a then p
- To jump recycle screen : Ctrl + a then space
- To detach a screen : Ctrl + a then d

- Split into 2 sub-screen by horizontal : Ctrl + a then S
- To jump from sub-screen to other sub-screen : Ctrl + a then Tab 
- In this image, I run iostat and top command at the same time in two subscreen devided by horizontal.


- Split into 2 sub-screen by vertical : Ctrl + a then |
- To jump from sub-screen to other sub-screen : Ctrl + a then Tab 
- In this image, I run iostat and top command at the same time in two subscreen devided by vertical.


- To kill a screen : Ctrl + a then k or type exit

You can try resume by entering a screen and plug out the network cable to simulate the network lost connection, then you can resume by #screen -dr.

It works right ? and that's it.