How to record your ssh session using screen.

Per the man page – “Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells)”.
Screen is most commonly used to create multiple sessions to remote hosts within a single terminal window or even run multiple commands locally without leaving your shell terminal. For instance, you could be tailing the log file in one session, then run a long process, then ssh into other machine etc. all within a single window.

Screen is the go to tool when setting up a remote connection, such as ssh, and you want to continue your work at any time or from any other host without worrying of a dropped connection.

In this post, I will show you how you can record your bash session.

Installation –

yum install screen        (Debian/Ubuntu)
apt-get install screen    (Redhat/CentOS)

My local environment and the remote host I am sshing to –

daniel@linubuvma:/tmp$ screen -v
Screen version 4.01.00devel (GNU) 2-May-06
daniel@linubuvma:/tmp$ uname -r
3.13.0-106-generic
daniel@linubuvma:/tmp$ cat /etc/issue
Ubuntu 14.04.5 LTS \n \l

daniel@linubuvma:/tmp$ ssh ns2 'uname -r ; cat /etc/issue'
2.6.32-642.6.1.el6.x86_64
CentOS release 6.8 (Final)
Kernel \r on an \m

The ‘-L’ option of screen is used to record your session, the session log is automatically saved in a file named ‘screenlog.n’ in your current directory.

daniel@linubuvma:/tmp$ ls
config-err-hbzs5e  one          ssh-4yheApHRgMBF  ssh-RK7GpeFuzUB8  VMwareDnD    vmware-root-2347660412
gpg-kZux7q         screenlog.0  ssh-BBblvGtb5284  vmware-daniel     vmware-root
daniel@linubuvma:/tmp$ free -m
             total       used       free     shared    buffers     cached
Mem:          3946       2489       1457          6        547       1031
-/+ buffers/cache:        911       3035
Swap:         4092          0       4092
daniel@linubuvma:/tmp$ exit
[screen is terminating]
daniel@linubuvma:/tmp$ 

The whole bash session will be logged in screenlog.0 in this case –

daniel@linubuvma:/tmp$ cat screenlog.0 
daniel@linubuvma:/tmp$ ls
config-err-hbzs5e  one          ssh-4yheApHRgMBF  ssh-RK7GpeFuzUB8  VMwareDnD    vmware-root-2347660412
gpg-kZux7q         screenlog.0  ssh-BBblvGtb5284  vmware-daniel     vmware-root
daniel@linubuvma:/tmp$ free -m
             total       used       free     shared    buffers     cached
Mem:          3946       2489       1457          6        547       1031
-/+ buffers/cache:        911       3035
Swap:         4092          0       4092
daniel@linubuvma:/tmp$ exit
exit
daniel@linubuvma:/tmp$ 

Recording your session of an ssh connection to a remote host is also similar, with ‘-L’ option followed by the command to ssh to remote host.
Option -fn (with no flow-control)
Option -t (title bar name) in this case ‘practice’.

daniel@linubuvma:/tmp$ screen -fn -t practice -L  ssh ns2
Last login: Tue Dec 27 09:46:10 2016 from linubuvma.home.net

[daniel@kauai ~]$ hostname -f
kauai.example.net
[daniel@kauai ~]$ uptime
 10:08:18 up 18 days, 10:02, 14 users,  load average: 0.19, 0.49, 0.64
[daniel@kauai ~]$ exit
[screen is terminating]


daniel@linubuvma:/tmp$ cat screenlog.0 
Last login: Tue Dec 27 09:46:10 2016 from linubuvma.home.net
[daniel@kauai ~]$ hostname -f
kauai.example.net
[daniel@kauai ~]$ uptime
 10:08:18 up 18 days, 10:02, 14 users,  load average: 0.19, 0.49, 0.64
[daniel@kauai ~]$ exit
logout
Connection to ns2 closed.
daniel@linubuvma:/tmp$ 

Additional resources –

https://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/
https://linux.die.net/man/1/screen