← PostsTOOLING

Always-On Terminal Logging for Security Labs (Zsh + Tmux)

A ~/.zshrc auto-logger using script(1) with per-target log folders, plus a tmux pipe-pane option for saving terminal output.

Contents
  1. The Zsh auto-logger
  2. Adding it to ~/.zshrc
  3. How it plays out in practice
  4. Quick alternative: logging a single Tmux pane

It’s frustrating to finish a lab and realize the command output you wanted for your notes was in a terminal tab you’ve already closed. Logging the session gives us something to go back to.

I put together a small Zsh snippet for my Kali box that automatically records every terminal session to disk, organized by target IP or box name. Below is the setup I use, plus a quick tmux alternative if you live inside tmux.

The Zsh auto-logger

The idea is simple: wrap each new shell automatically with script -f -q, store logs under ~/lab-logs/<box-name>/, and give yourself three helper functions:

Command What it does
set-box 10.10.10.5 Sets the active target context so new tabs log under ~/lab-logs/10.10.10.5/.
switch-box 10.10.10.5 Updates the context and immediately replaces the current tab so it logs to the new target.
current-box Prints the active target folder and exact log file for this tab.

The -f flag on script is the important part: it flushes output to disk immediately instead of buffering, so you can grep your logs from another tab in real time.

Adding it to ~/.zshrc

Append this to ~/.zshrc on your Linux/Kali VM:

# Always-On Lab Terminal Logger
# Append to ~/.zshrc

LOG_BASE="$HOME/lab-logs"
CONTEXT_FILE="$HOME/.current_lab_box"

if [[ -f "$CONTEXT_FILE" ]]; then
    CURRENT_BOX=$(cat "$CONTEXT_FILE")
else
    CURRENT_BOX="general"
fi

# Prevent infinite recursion when script launches zsh
if [ -z "${UNDER_SCRIPT}" ]; then
    LOGDIR="${LOG_BASE}/${CURRENT_BOX}"
    mkdir -p "${LOGDIR}"

    LOGFILE="${LOGDIR}/$(date +%Y-%m-%d_%H-%M-%S)_$$.log"
    export UNDER_SCRIPT="${LOGFILE}"

    echo "[REC] Auto-logging to: ${LOGFILE}"
    script -f -q "${LOGFILE}"
    exit
fi

set-box() {
    local NEW_BOX="$1"
    if [[ -z "$NEW_BOX" ]]; then
        echo "Current logging context: '$CURRENT_BOX'"
        echo "Usage: set-box <ip_or_name>"
        return 1
    fi
    echo "$NEW_BOX" > "$CONTEXT_FILE"
    echo "Global context set to '$NEW_BOX'."
    echo "Next tab will log to: $LOG_BASE/$NEW_BOX/"
    echo "This tab is still logging to: $LOG_BASE/$CURRENT_BOX/"
}

switch-box() {
    local NEW_BOX="$1"
    if [[ -z "$NEW_BOX" ]]; then
        echo "Usage: switch-box <ip_or_name>"
        return 1
    fi
    echo "$NEW_BOX" > "$CONTEXT_FILE"
    echo "Switching current session to '$NEW_BOX' (old log: $UNDER_SCRIPT)"
    unset UNDER_SCRIPT
    exec zsh
}

current-box() {
    echo "Current Context: $CURRENT_BOX"
    echo "Current Log: $UNDER_SCRIPT"
}

Reload with source ~/.zshrc (or open a fresh terminal), and you should immediately see [REC] Auto-logging to: ... at the top.

How it plays out in practice

  • Working one target: run set-box 10.10.10.5 in your first tab, then open tabs 2 and 3. Everything goes into ~/lab-logs/10.10.10.5/.
  • Multitasking across two targets: while tab 1 is still scanning Target 1, run set-box 10.10.10.6 and open a new tab. Tab 1 stays pointed at Target 1, and the new tab logs to Target 2.
  • Need to find something later: just search the folder:
grep -rn "password" ~/lab-logs/10.10.10.5/

When you close the terminal window, script closes cleanly and saves everything.

Quick alternative: logging a single Tmux pane

If you’re already in tmux and just want to capture the current pane without touching ~/.zshrc, hit Ctrl+b then : to open the tmux command prompt and run:

pipe-pane -o 'cat >> ~/tmux-pane-log-#{session_name}-#{window_index}-#{pane_index}.log'

Running pipe-pane again with no arguments stops logging for that pane.

One quick reminder: because terminal logs capture everything printed to your screen, keep your log directory out of Git repos and clean up sensitive credentials once a lab is finished.