提升服务器安装:自定义history命令历史功能
2015-07-03 13:37
453 查看
相信大家都知道linux的history命令可以查看用户的曾经执行的命令历史,而默认在用户的home下有个.bash_history的文件存放用户的命令历史,但是他的功能太简单了,下面是我从其他网页整理的脚本只需要放到你的/etc/profile文件尾部即可。
我们想要的功能:
记录命令执行时间,和用户
记录更多的命令历史
history实时查看执行的命令
####大篇幅代码预警,非战斗人员请迅速撤离####
第一段代码会让你的命令历史变成这样:
第二段代码会让你的history文件变成这样:
在/var/log/history/目录下会以每个用户的用户名创建一个文件夹,然后用户每一次登录执行的历史命令都会保存成一个文件,文件名包含了“root@192.168.16.221_20150703_03:55:56”登录用户、登录IP以及登录时间。
当然,这个history的位置是可以自定义的,建议有shell脚本经验的童鞋可以修改。
好了,以上是所有内容了.
如果你有更好的idea,欢迎来和我分享。E-mail:luchuanjia@msn.com
我们想要的功能:
记录命令执行时间,和用户
记录更多的命令历史
history实时查看执行的命令
####大篇幅代码预警,非战斗人员请迅速撤离####
# The frist section # Set the history file size. export HISTFILESIZE=999999999 #Bigger the better. # Set the history order number. export HISTSIZE=999999 #Bigger the better. # Real-time record history command, prevent overlap between multiple users. export PROMPT_COMMAND="history -a" #Record each command execution time and user. export HISTTIMEFORMAT="%F %T `whoami` " # The second section # Check for interactive bash and that we haven't already been sourced. [ -z "$BASH_VERSION" -o -z "$PS1" -o -n "$BASH_COMPLETION_COMPAT_DIR" ] && return # Check for recent enough version of bash. bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.} if [ $bmajor -gt 4 ] || [ $bmajor -eq 4 -a $bminor -ge 1 ]; then [ -r "${XDG_CONFIG_HOME:-$HOME/.config}/bash_completion" ] && \ . "${XDG_CONFIG_HOME:-$HOME/.config}/bash_completion" if shopt -q progcomp && [ -r /usr/local/share/bash-completion/bash_completion ]; then # Source completion code. . /usr/local/share/bash-completion/bash_completion fi fi unset bash bmajor bminor 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 ]; then mkdir /var/log/history chmod 777 /var/log/history fi if [ ! -d /var/log/history/${LOGNAME} ]; then mkdir /var/log/history/${LOGNAME} chmod 300 /var/log/history/${LOGNAME} fi export HISTSIZE=4096 DT=`date +"%Y%m%d_%H:%M:%S"` export HISTFILE="/var/log/history/${LOGNAME}/${USER}@${USER_IP}_$DT" chmod 600 /var/log/history/${LOGNAME}/*history* 2>/dev/null
第一段代码会让你的命令历史变成这样:
第二段代码会让你的history文件变成这样:
在/var/log/history/目录下会以每个用户的用户名创建一个文件夹,然后用户每一次登录执行的历史命令都会保存成一个文件,文件名包含了“root@192.168.16.221_20150703_03:55:56”登录用户、登录IP以及登录时间。
当然,这个history的位置是可以自定义的,建议有shell脚本经验的童鞋可以修改。
好了,以上是所有内容了.
如果你有更好的idea,欢迎来和我分享。E-mail:luchuanjia@msn.com
相关文章推荐
- 使用ajax和history.pushState无刷新改变页面URL示例
- 基于jQuery的history历史记录插件
- JavaScript中的History历史对象
- JavaScript入门教程(7) History历史对象
- linux查看历史命令history
- history
- Linux中exec命令相关
- 配置python命令tab键自动补全
- 让history命令显示时间
- 如何让linux的history命令显示时间记录
- zabbix 数据库备份
- Linux入门 Part1: 使用控制台(4)-找到想要的命令
- 实验5:使用历史记录、系统日志及调试工具
- History命令
- IIS Admin Service 服务因下列错误而停止: 拒绝访问
- What is “Cloud Computing”?
- Debian中VI的配置方法简析
- history
- 2013-12-11 学习笔记
- Linux运维工程师笔试题