linux系统下自动记录用户的操作

在对linux系统进行操作的时候经常多开好几个ssh窗口,执行很多命令,如果操作内容很多,时间一久就会忘记,很有必要把每一次的操作记录保存下来用于日后查看;还有一种情况就是多人协作的场景,经常会出现不同人在同一台服务器上同时操作,操作的内容互相影响,这时也非常需要有一个审计日志,可以很方便的查看哪个用户做了什么操作,执行了什么命令。

以下是记录 bash 命令日志的参考方法,借此可以实现Linux系统上记录用户操作的审计日志

1、编辑/etc/profile文件,在结尾增加如下代码:

#vi /etc/profile

#set user history
USER=`whoami`
USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
if [ "$USER_IP" = "" ]; then
    USER_IP=`hostname`
fi
if [ ! -d /var/log/history/${LOGNAME} ]; then
    mkdir -p /var/log/history/${LOGNAME}
    chown -R ${LOGNAME}:${LOGNAME} /var/log/history/${LOGNAME}
    chmod 300 /var/log/history/${LOGNAME}
fi
export HISTTIMEFORMAT='%F %T '
export HISTORY_FILE="/var/log/history/${LOGNAME}/bash_history"
export PROMPT_COMMAND='{ msg=$(history 1 | { read x y z; echo $z; });echo $(date +"%Y-%m-%d %H:%M:%S") [$USER@$LOGNAME@$USER_IP `pwd` ]" $msg" >> $HISTORY_FILE; }'

chmod 300 /var/log/history/${LOGNAME}目的是防止创建的日志文件被删除

read x y z; echo $z;这里取变量$z的原因是因为linux系统默认的history命令的输出结果第一列是行号 第二列是执行的命令,由于没有时间不知道啥时候执行的,不太方便排查问题,于是前期通过在/root/.bashrc文件中增加一行代码的方式,增加了一列用来显示时间,于是命令本身变成了第三列,也就是第三个变量。

export HISTTIMEFORMAT='%F %T '

这里为了方便就把这一行代码也放在了上述shell脚本的第12行。这样就不用单独去修改/root/.bashrc文件了。

2、执行如下命令,使配置生效

# source /etc/profile

3、验证

退出ssh,再登陆,查看bash_history文件是否有内容

#cat /var/log/history/${LOGNAME}/bash_history

4、简化操作
cat << "EOF" >> /etc/profile
#set user history
USER=`whoami`
USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
if [ "$USER_IP" = "" ]; then
    USER_IP=`hostname`
fi
if [ ! -d /var/log/history/${LOGNAME} ]; then
    sudo mkdir -p /var/log/history/${LOGNAME}
    sudo chown -R ${LOGNAME}:${LOGNAME} /var/log/history/${LOGNAME}
    sudo chmod 300 /var/log/history/${LOGNAME}
fi
export HISTORY_FILE="/var/log/history/${LOGNAME}/bash_history"
export PROMPT_COMMAND='{ msg=$(history 1 | { read x y; echo $y; });echo $(date +"%Y-%m-%d %H:%M:%S") [$USER@$LOGNAME@$USER_IP `pwd` ]" $msg" >> $HISTORY_FILE; }'
EOF
5、使用问题记录

实际使用发现上述方法有几点不足:

1、用户使用非root用户登录,然后sudo su之后的操作没有记录,这在审计时是非常严重的问题。

2、history日志保留在本地,一旦系统崩溃,操作记录也就无法查询了。

=========================================================================

6、所有用户的shell history保留在另外一台服务器上

这里有一个新的工具hishtory,可以将所有用户的shell history保留在另外一台服务器上,安装使用都很简单。

Getting Started

To install hishtory on your first machine:

curl https://hishtory.dev/install.py | python3 -

At this point, hishtory is already managing your shell history (for bash, zsh, and fish!). Give it a try by pressing Control+R and see below for more details on the advanced search features.

Then to install hishtory on your other computers, you need your secret key. Get this by running hishtory status. Once you have it, you follow similar steps to install hiSHtory on your other computers:

curl https://hishtory.dev/install.py | python3 -
hishtory init $YOUR_HISHTORY_SECRET

Now if you press Control+R on first computer, you can automatically see the commands you've run on all your other computers!

参考链接:

Bash Shell 中的 PROMPT_COMMAND - Jamin Zhang

https://www.jianshu.com/p/293661239c02

GitHub - ddworken/hishtory: Your shell history: synced, queryable, and in context

=========================================================================

history命令则更侧重于记录用户在命令行界面中的历史操作。它本质上是将用户输入的命令记录到syslog日志中,主要用于简单记录用户的命令操作历史。然而,history方式存在一些局限性,例如容易被修改或绕过,记录的信息相对简单且缺乏上下文信息,无法记录shell脚本内的操作或非登录操作等。

7、Auditd提供了更全面、详细的系统审计功能,适用于安全审计和合规性检查等场景。
apt -y install auditd
auditctl -l
# 编辑如下文件
/etc/audit/rules.d/audit.rules
# 根据系统位数选择增加以下规则
-a always,exit -F arch=b64 -S execve -k execve_tracking //记录64位系统的所有用户的shell命令
-a always,exit -F arch=b32 -S execve -k execve_tracking //记录32位系统的所有用户的shell命令

augenrules --load
auditctl -l
service auditd restart

ausearch -k execve_tracking
aureport -i -x --start today

参考文档

audit.rules(7) - Linux manual page

来自 <https://www.cnblogs.com/wangguishe/p/17285807.html>

ubuntu20.04 Auditd使用教程(监控所有用户命令输入记录,包括常用命令、运行脚本、pip安装、apt安装等)(操作监控、输入监控、命令监控、操作日志、命令日志、history命令)

来自 <ubuntu20.04 Auditd使用教程(监控所有用户命令输入记录,包括常用命令、运行脚本、pip安装、apt安装等)(操作监控、输入监控、命令监控、操作日志、命令日志、history命令)失败了_auditd.conf use_libwrap-CSDN博客>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值