Linux provides multiple tools and methods for logging terminal commands, each with unique capabilities and use cases.
graph TD
A[Logging Tools] --> B[Built-in Methods]
A --> C[Third-party Tools]
A --> D[Custom Solutions]
1. Built-in Shell Logging Methods
Bash History Configuration
Configuration Option |
Description |
Example |
HISTSIZE |
Controls number of commands stored |
export HISTSIZE=10000 |
HISTFILESIZE |
Maximum lines in history file |
export HISTFILESIZE=20000 |
HISTCONTROL |
Controls history recording behavior |
export HISTCONTROL=ignoredups |
Enhanced History Logging
## Enable timestamp logging
export HISTTIMEFORMAT="%F %T "
## Log commands immediately
export PROMPT_COMMAND='history -a'
2.1 Syslog
Captures system-wide command executions through /var/log/auth.log
## View authentication logs
sudo tail -f /var/log/auth.log
2.2 Script Command
Comprehensive terminal session recording utility
## Start recording session
script session.log
## Stop recording
exit
3. Advanced Logging Solutions
3.1 auditd
Enterprise-grade logging for security monitoring
## Install auditd
sudo apt-get install auditd
## Configure audit rules
sudo auditctl -w /usr/bin/sudo -p x
3.2 Custom Logging Scripts
Flexible logging solution using shell scripting
#!/bin/bash
LOG_FILE="/var/log/command_log.txt"
log_command() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $USER: $*" >> "$LOG_FILE"
}
## Trap commands before execution
trap 'log_command "$BASH_COMMAND"' DEBUG
Tool |
Purpose |
Key Features |
tmux |
Session logging |
Persistent terminal sessions |
asciinema |
Terminal recording |
Detailed session capture |
logrotate |
Log management |
Automatic log rotation |
Practical Considerations
- Performance overhead
- Storage requirements
- Privacy implications
LabEx recommends selecting logging methods based on specific use cases and system requirements.