Home / Community / Blog / Log all bash commands to file or remote server

Log all bash commands to file or remote server

On CentOS 7 you can log all commands to syslog an then to local file or even to remote server.

Send all commands to syslog

Create file /etc/sysconfig/bash-prompt-xterm:

RETRN_VAL=$?;logger -p local6.debug "$(whoami) [$$]: $(history 1 | sed "s/^[ ]*[0-9]\+[ ]*//" ) [$RETRN_VAL]"

and change, to be executable:

chmod a+x /etc/sysconfig/bash-prompt-xterm

Configure syslog to send messages from local6 facility to separate file

Create file /etc/rsyslog.d/bash.conf:

local6.*    /var/log/commands.log

finally:

service restart rsyslog

Now you can monitor commands:

tail -f /var/log/commands.log

Log command using audit

Alternatively you can use audit - create /etc/audit/rules.d/bash_history.rules:

-a exit,always -F arch=b64 -S execve
-a exit,always -F arch=b32 -S execve

but logs are not very human friendly:

grep EXECVE /var/log/audit/audit.log

and you may also want to log execvp, execl, execveat etc.

Sources:

https://askubuntu.com/questions/93566/how-to-log-all-bash-commands-by-all-users-on-a-server

https://unix.stackexchange.com/questions/86000/how-can-you-log-every-command-typed

http://whmcr.com/2011/10/14/auditd-logging-all-commands/

Comments

Log in or create a user account to comment.

Just my blog...

Mon Tue Wed Thu Fri Sat Sun
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31