Sunday, November 4, 2012

"Borrowing" A Screen Session

GNU Screen is a tool that most everyone reading this will be aware of.  In a nutshell, it's a terminal multiplexer, that is, it allows multiple virtual terminals to be brought up in one screen session.  Very useful when you're on the terminal.

Another purpose that is used quite often is to allow long running scripts to run on a remote server, without having to worry about your SSH session getting disconnected.  One would simply start a screen session, run their script, and detach from the screen session, leaving the script to do it's work.

This can occasionally be a problem when one system administrator has started a long running process, and for whatever reason, another system administrator needs to get into the screen session, when it's owner isn't present.  Attempting to su user, then attach to the screen session will lead to the following error:


[14:43:06] [peter@server ~]$ sudo su user
[sudo] password for peter: 
[14:43:45] [user@server /home/peter]$ screen -l
Cannot open your terminal '/dev/pts/2' - please check.
[14:43:53] [user@server /home/peter]$

A quick check of the permissions of /dev/pts/2 reveals the problem:


[14:43:53] [user@server /home/peter]$ cd /dev/pts/
[14:51:42] [user@server /dev/pts]$ ls -lsa 2
0 crw--w---- 1 peter tty 136, 2 2012-11-04 14:51 2

As we can see, the terminal I'm connected to is owned by my user, not effective user that we assumed with su user.  One solution would be to chmod +w /dev/pts/2, but allowing other users to connect to our pts device poses a security risk.  Enter script.

Script is used to create a typescript recording of everything that happens in a terminal, as long as script is running.  It will connect to a new tty device when it is called:


[15:00:20] [user@server ~]$ script test
Script started, file is test
[15:00:25] [user@server ~]$ tty
/dev/pts/3
[15:00:32] [user@server ~]$ ls -lsa /dev/pts/3
0 crw--w---- 1 user tty 136, 3 2012-11-04 15:00 /dev/pts/3

As you can see, by calling script, we have obtained a new pts device, and it's owned by user, not by peter.  This means that we can now screen -d -r to attach to user's screens, even in his absence.

One last note:  If you don't have a purpose for the typescript file that is generated by script, just give it /dev/null as an output file:  script /dev/null, which will throw away the typescript.

No comments:

Post a Comment