If you face an issue where you need to login to a remote machine but due to frequent disconnections, you have to start all over again to be in the same state when it got disconnected, then GNU Screen comes to your rescue. By same state what I mean is this: say if you have some code files opened in some deep rooted directories, additionally you are building some code, which represents the state. After the disconnection once you reconnect to remote machine, you will need to reopen all those files and start build again etc. to come back to the same state where you originally were when disconnection happened.
In the steps shown below I assume:
- You are logging to a remote machine using putty
- And you have install permissions / sudo access on the remote machine
Setting up GNU Screen:sudo apt-get install screen
That’s all!!! It will also install another related package called screen-profiles on Ubuntoo, which helps in configuring the screen.
Now you just need to know a few keystrokes to learn to use it.
Using GNU Screen
In the keystrokes listed below, say CTRL-a c, you need to press ‘CTRL’ key followed by ‘a’ key in quick succession and then ‘c’ key.
- Now start new screen session using:
- Create additional screen tabs using:
- Cycle through or go to next or previous screen tabs using:
- Open some files in each of these tabs
- Now assume that you face a disconnection, and you have to get back to the same state. To simulate this, simply close the putty window.
- Now relogin to the remote machine using putty
- Once you are done with this, you can get back to the same state where you left the previous session simply by attaching to a running screen using:
- You can detaching from a screen session, and the screen session can keep running in the background, until you kill it or until you reboot the remote machine. To detach from the screen session, type
- To kill a screen session, type
- In case you have multiple screens running on the remote machine, you can list all the running screens using:
- This will show the output similar to the following:
There are screens on: 11431.pts-4.ssaurabh-desktop (02/03/2010 11:43:58 AM) (Attached) 10695.pts-3.ssaurabh-desktop (02/03/2010 11:43:14 AM) (Attached) 2 Sockets in /var/run/screen/S-ssaurabh.
- To attach to one of the running screens, you can specify the screen with which you want to connect in this fashion:
- One can attach to the same screen session from multiple remote login windows. Interestingly this can be used in such a way that two users can login to a remote machine and attach to the same screen and thus one user can explain something to the other user. As soon as one of the user does something, the other user will immediately see the typed keystrokes of the first user.
Screen shot with the first tab displayed:Screen shot with the first tab displayed: