这是一个将所有执行的命令发送到syslog服务器的解决方案.
摘自博客文章
以下是将用户执行的所有命令的副本发送到Syslog服务器的两种方法.第一个将使用Bash“陷阱”功能.第二个是在Bash源代码中应用的补丁.
使用陷阱
只需在/ etc / profile中添加以下行:
function log2syslog
{
declare command
command=$(fc -ln -0)
logger -p local1.notice -t bash -i — $USER : $command
}
trap log2syslog DEBUG
在Bash启动时解析并执行/ etc / profile.目标是使用陷阱功能,并在每次用户生成活动时调用函数.陷阱功能(log2syslog)将从历史记录中提取最后一个命令,并使用logger命令将其记录到Syslog.很容易实现,但这种方法:
spawns new process at each command logged (can have a negative effect when the server activity is high)
is not transparent to the user (regular users can’t edit /etc/profile but can read it!)
这就是为什么第二种方法将是首选的原因.
使用补丁
该方法是在Bash源树上应用补丁并重新编译shell.它需要一个带有编译器和源代码的环境,但这种方法将使用更少的CPU并且将完全透明!
可以使用here的补丁示例.将补丁手动应用于Bash 4源树需要五分钟.
以下是Syslog消息的示例:
Feb 27 19:30:51 honey bash: HISTORY: PID=21099 UID=1000 echo foo!