How to Record Linux Terminal Sessions?

Recording Linux terminal sessions is valuable for documenting commands, troubleshooting, sharing workflows, and creating tutorials. This article explores three popular methods to capture terminal activity: the built-in script command, the ttyrec/ttyplay combo, and the modern asciinema tool.

Method 1: Using the script Command

The script command is a built-in Linux utility that captures all terminal output to a plain text file. It records everything displayed in the terminal, including commands, output, and control characters.

Basic Usage

To start recording, run script followed by the output filename:

$ script my_session.txt
Script started, file is my_session.txt

Once started, all terminal activity is recorded. To stop recording, type exit or press Ctrl+D:

$ script my_session.txt
Script started, file is my_session.txt
$ ls -la
total 16
drwxr-xr-x  2 user user 4096 Dec  1 10:30 .
drwxr-xr-x 25 user user 4096 Dec  1 10:25 ..
$ whoami
user
$ exit
Script done, file is my_session.txt

Useful script Options

# Append to existing file instead of overwriting
$ script -a session.txt

# Include timing information for playback
$ script -t timing.txt session.txt

# Run quietly (suppress start/stop messages)
$ script -q session.txt

Method 2: Using ttyrec and ttyplay

The ttyrec command creates binary recordings that preserve exact terminal timing and formatting. These recordings can be played back with ttyplay, maintaining the original speed and appearance.

Recording with ttyrec

# Start recording
$ ttyrec my_recording.tty

# Stop recording by exiting the terminal or pressing Ctrl+D

Playback with ttyplay

# Play back the recording
$ ttyplay my_recording.tty

# Play at double speed
$ ttyplay -s 2 my_recording.tty

# Play with pauses between frames
$ ttyplay -p my_recording.tty

Method 3: Using asciinema

asciinema is a modern terminal recording tool that creates lightweight JSON files. It offers the best balance of file size, quality, and sharing capabilities.

Installation

# Ubuntu/Debian
$ sudo apt install asciinema

# CentOS/RHEL/Fedora
$ sudo yum install asciinema
# or
$ sudo dnf install asciinema

Recording and Playback

# Start recording
$ asciinema rec my_recording.cast

# Stop recording with Ctrl+D or exit

# Play back locally
$ asciinema play my_recording.cast

# Upload and share online
$ asciinema upload my_recording.cast

Comparison of Methods

Feature script ttyrec asciinema
File Format Plain text Binary JSON
File Size Large Medium Small
Human Readable Yes No Partially
Timing Preserved Optional Yes Yes
Installation Required Pre-installed Usually available Separate package
Online Sharing No No Yes
Best For Simple logging Exact playback Sharing & tutorials

Key Points

  • script is ideal for basic session logging and troubleshooting documentation

  • ttyrec/ttyplay provides pixel-perfect playback with exact timing

  • asciinema offers the best modern solution for sharing and collaboration

  • All methods can be combined with other tools like grep and less for analysis

  • Choose based on your needs: logging (script), exact replay (ttyrec), or sharing (asciinema)

Conclusion

Each terminal recording method serves different purposes: script for simple text logging, ttyrec for precise playback, and asciinema for modern sharing and collaboration. Choose the method that best fits your workflow and sharing requirements.

Updated on: 2026-03-17T09:01:38+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements