How to log all Bash commands by all users on a server.
For BASH shells, edit the system-wide BASH runtime config file:
sudo vi /etc/bash.bashrcAppend to the end of that file:
export PROMPT_COMMAND='RETRN_VAL=$?;logger -p local6.debug "$(whoami) [$$]: $(history 1 | sed "s/^[ ]*[0-9]\+[ ]*//" ) [$RETRN_VAL]"'Set up logging for "local6" with a new file:
vi /etc/rsyslog.d/bash.confAnd the contents:
local6.* /var/log/commands.logRestart rsyslog:
sudo service rsyslog restartLog out. Log in. Voila!
But I forgot about log rotation:
sudo vi /etc/logrotate.d/rsyslogThere is a list of log files to rotate the same way...
/var/log/mail.warn
/var/log/mail.err
[...]
/var/log/messageSo add the new bash-commands log file in that list:
/var/log/commands.logSave.
Last updated
Was this helpful?