百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>服务器> Linux记录使用过命令的shell脚本
分享文章到:

Linux记录使用过命令的shell脚本

发布时间:01/15 来源: 浏览: 关键词:
Linux记录使用过命令的shell脚本可以帮助我们记录系统使用过的命令了这个可以非常好的记录系统中每一条命令了,这个我相信大家都想用吧。

在Linux中有个很重要的命令可以帮助我们记录曾经使用过的命令,对于后续排错或者入侵检测都是很有用。本文中把配置整理成一个脚本,直接运行脚本即可生效


#!/bin/bash
/bin/cp /etc/bashrc /etc/bashrc.`date +"%Y-%m-%d-%s"`
sed -i '/timestamp_history/,+10d' /etc/profile
grep "HISTTIMEFORMAT" /etc/profile >/dev/null || echo "export HISTTIMEFORMAT='%F %T '" >> /etc/profile
sed -i '/timestamp_history/,+10d' /etc/bashrc
 
/bin/cat >> /etc/bashrc <<EOF
timestamp_history(){
export infodate=\`date "+ %c"\`    #记录时间
export infohis=\`history 1|cut -c 8-\`  #记录运行命令
export user_ip=\`who -u am i 2>/dev/null | awk '{print \$NF}' | sed -e 's/[()]//g'\`   #记录用户ip
export user=\`who -u am i 2>/dev/null | awk '{print \$1}'\`  ##记录用户
echo \$infodate" => "\$user_ip" => "\$user" => "\$infohis >> /var/log/.history-timestamp
}
export PROMPT_COMMAND=timestamp_history
export HISTTIMEFORMAT="\`whoami\` "
export HISTCONTROL=ignoreboth
EOF
 
#每月备份一次
echo '/bin/mv /var/log/.history-timestamp /var/log/.history-timestamp.`date +"%Y-%m-%d-%s"`
touch /var/log/.history-timestamp
chmod 0772 /var/log/.history-timestamp' > /etc/cron.monthly/backup_history
/bin/chmod 755 /etc/cron.monthly/backup_history
运行后日子格式如下图:

做好的脚本可以放在一台web上面,其他机器直接通过命令去获取安装

wget -O - http://ip/setHis.sh | sh ; source /etc/bashrc

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有4人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板