最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Linux记录使用过命令的shell脚本
时间:2016-04-26 编辑:简简单单 来源:一聚教程网
在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 <
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
-
下一个: linux命令学习笔记之dstat命令
相关文章
- Linux服务器netstat命令使用记录 03-13
- vps迁移用到的linux命令记录 12-21
- win11内核隔离和内存完整性介绍 10-31
- win10全屏缩放设置教程 10-31
- win10系统备份出错解决教程 10-31
- win10打开软件每次都要询问解决教程 10-31