RHEL

  1. sudo -e /etc/bashrc 

    and add this line:

    export PROMPT_COMMAND='RETRN_VAL=$?;logger -p local6.debug "$(whoami) [$$]: $(history 1 | sed "s/^[ ]*[0-9]\+[ ]*//" )"' 
  2. source /etc/bashrc 
  3. vim /etc/rsyslog.d/bash.conf 

    and add the following line:

    local6.*    /var/log/commands.log 
  4. vim /etc/logrotate.d/syslog 

    and add the following line:

    /var/log/commands.log 
  5. systemctl restart rsyslog 

UBUNTU

For BASH shells, edit the system-wide BASH runtime config file:

vim /etc/bash.bashrc

Append 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:

vim /etc/rsyslog.d/bash.conf 

contents...

local6.*    /var/log/commands.log 

Restart rsyslog:

systemctl restart rsyslog 

source /etc/bash.bashrc or logout/login

log rotation:

vim /etc/logrotate.d/rsyslog 

Add the new bash-commands log file to the list:

/var/log/commands.log