How to Record Linux Terminal Sessions?


Introduction

Sometimes it can be helpful to record a terminal session in Linux so that you can review the commands you ran, the output they produced, and any other text that was displayed in the terminal. In this article, we'll discuss three different ways to record terminal sessions in Linux − using the script command, using the ttyrec command, and using the asciinema tool.

Method 1: Using the script command

The script command is a simple and easy-to-use tool for recording terminal sessions in Linux. It captures all the text that is displayed in the terminal, as well as all the commands you enter.

To start a recording with script, simply open a terminal and run the script command followed by the name of the file you want to save the recording to. For example

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

The script command will start recording immediately and will display a message indicating that the recording has started and the name of the file where the recording will be saved.

To stop the recording, simply type exit at the prompt and press Enter. The recorded output will be saved to the file my_recording.txt. Here's an example of a simple terminal session that was recorded using script

$ script my_recording.txt
Script started, file is my_recording.txt
$ ls
Desktop Documents Downloads Music Pictures Videos
$ exit
Script done, file is my_recording.txt

Method 2: Using the ttyrec command

The ttyrec command is similar to script, but it creates a binary file that can be played back using the ttyplay command. To start a recording with ttyrec, run the command followed by the name of the file you want to save the recording to. For example

$ ttyrec my_recording.tty

The ttyrec command will start recording immediately and will display a message indicating that the recording has started.

To stop the recording, simply exit the terminal by closing the window or pressing CTRL+D. The recorded output will be saved to the file my_recording.tty.

To play back a recording that was created with ttyrec, use the ttyplay command followed by the name of the recorded file. For example

$ ttyplay my_recording.tty

Method 3: Using the asciinema tool

asciinema is a free and open source tool that allows you to record and share terminal sessions. It produces a file in JSON format that can be played back using the asciinema command line tool or uploaded to the asciinema.org website for sharing.

To install asciinema, run the following command

$ asciinema rec my_recording.json

The asciinema command will start recording immediately and will display a message indicating that the recording has started.

To stop the recording, press CTRL+D or type exit at the prompt and press Enter. The recorded output will be saved to the file my_recording.json, which you can then share with others or play back using the asciinema play command.

To play back a recording that was created with asciinema, use the asciinema play command followed by the name of the recorded file. For example

$ asciinema play my_recording.json

You can also upload the recorded file to the asciinema.org website for sharing. Simply log in to the website, click the "New Cast" button, and select the recorded file to upload.

Comparing the Different Methods

In this section, we'll take a closer look at the pros and cons of each of the three methods for recording terminal sessions in Linux.

The script command is a simple and easy-to-use tool that captures all the text and commands entered in the terminal. One advantage of script is that it produces a plain text file that can be easily read and edited with any text editor. However, the recorded output can be quite verbose, as it includes all the terminal control characters and other low-level details. This can make the recorded file large and difficult to read.

The ttyrec command is similar to script, but it creates a binary file that can be played back using the ttyplay command. One advantage of ttyrec is that it produces a smaller and more compact file than script, as it only captures the text and not the terminal control characters. However, the recorded file is not human-readable and can only be played back using the ttyplay command.

The asciinema tool is the latest and most powerful open source tool that allows you to record and share terminal sessions. It produces a file in JSON format that can be played back using the asciinema command line tool or uploaded to the asciinema.org website for sharing. One advantage of asciinema is that it produces a small and easy-to-read file that can be played back or shared with others. However, it requires the installation of an additional tool and may not be as widely supported as the other two methods.

Overall, the best choice for recording terminal sessions in Linux will depend on your specific needs and preferences. If you want a simple and easy-to-use tool that produces a plain text file, script may be the best choice. If you want a more compact and efficient file that can be played back using a separate tool, ttyrec may be a better option. And if you want a small and easy-to-read file that can be shared with others, asciinema may be the best choice.

Conclusion

In this article, we discussed three different ways to record terminal sessions in Linux: using the script command, using the ttyrec command, and using the asciinema tool. Each of these methods has its own benefits and drawbacks, and the best choice for you will depend on your specific needs and preferences. Whether you want to review a terminal session for your own records, share a session with others, or simply save a copy for posterity, these methods provide an easy way to do so.

Updated on: 04-Jan-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements