Record and Replay Every Linux Terminal Action with script & scriptreplay
This guide shows how to use the built‑in Linux commands script and scriptreplay to capture complete terminal sessions—including timestamps, command output, and playback speed control—plus tips for silent recording, real‑time monitoring, and automatic logging on user login.
Linux provides a powerful way to capture an entire terminal session, including exact timestamps for each command, and replay it like a video with adjustable speed.
The two utilities script and scriptreplay are installed by default on most distributions.
Recording a session
script -t 2>time.file -a scriptfileThis command starts recording; time.file stores timing information, while scriptfile stores the commands and their output. Both file names are customizable.
Adding -q runs script in quiet mode, so the user does not see the start/stop messages.
Stop recording with Ctrl+D or by running exit.
Replaying the session
scriptreplay time.file scriptfileUse the -d (or --divisor) option to change playback speed (e.g., -d 0.1 for slow motion). Some older versions may lack this option.
If real‑time writing is needed, add -f (or --flush) to script so each command is flushed to the file immediately. script -f demo When scriptreplay supports -f, you can replay with live flushing: scriptreplay -f demo Otherwise, monitor the file with tail -f demo.
Running a command non‑interactively
script -qa "file.out" -c "/root/hello.sh"The -c option executes a command directly, storing its output in file.out.
Automatic logging on login
Insert a script invocation into a shell startup file (e.g., /etc/profile) to record every login session:
# Add automatic logging on login
script -t -f -q 2>/var/log/user_record/$USER-$UID-`date +%Y%m%d%H%M%S`.time -a /var/log/user_record/$USER-$UID-`date +%Y%m%d%H%M%S`.his
if [ "$SHLVL" = 1 ]; then
exit
fiAfter logging in, you can view the live log with tail -f /var/log/user_record/….
Typical use cases
Create tutorial videos that show exact command‑line techniques.
Share a recorded session file so others can follow the exact steps.
Capture fast troubleshooting sessions without needing a screen recorder.
Debug complex shell scripts by replaying the full output.
Audit user activity on a server to identify who performed which actions.
(Not recommended) Attempt to capture passwords—this is insecure and discouraged.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
