Logging bash history to syslog using traps

Tags: , , ,

This is a handy way to log user’s bash histories to syslog without making any modifications to the bash source code itself. Simply drop the following snippet into either the per-user or system-wide bash profile (~/.bash_profile and /etc/profile, respectively)

function log2syslog
{
   declare COMMAND
   COMMAND=$(fc -ln -0)
   logger -p local1.notice -t bash -i -- "${USER}:${COMMAND}"
}
trap log2syslog DEBUG

This won’t guarantee you log consistency in the event of a compromised host but you’ll certainly have and increased ability to correlate events on your systems.

One Response to “Logging bash history to syslog using traps”

  1. Timo Juhani Lindfors Says:

    If you just hit enter multiple times the log claims that you actually executed the command multiple times too. Any suggestions on how to improve this?

Join the Conversation