linux 记录操作记录

October 17, 2018

记录操作记录

方案一

vim /etc/profile
# 把如下内容加入/etc/profile文件里面
PS1="`whoami`@`hostname`:"'[\u@\h \W]\$'
PS1="[\u@\h \W]\$" # 和上面的线上不一样,这种比较常规
history
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 /tmp/history ] 
then
mkdir /tmp/history
chmod 777 /tmp/history
fi
if [ ! -d /tmp/history/${LOGNAME} ] 
then
mkdir /tmp/history/${LOGNAME} 
chmod 300 /tmp/history/${LOGNAME} 
fi
export HISTSIZE=4096 
DT=`date +"%Y%m%d_%H%M%S"` 
export HISTFILE="/tmp/history/${LOGNAME}/${USER_IP} history.$DT"
chmod 600 /tmp/history/${LOGNAME}/*history* 2>/dev/null

# source一下
source /etc/profile
# 日志记录在 /tmp/history 目录下

方案二

vim /etc/profile
# 把如下内容加入到/etc/profile中
#设置history格式
export HISTTIMEFORMAT="[%Y-%m-%d %H:%M:%S] [`who am i 2>/dev/null| \
awk '{print $NF}'|sed -e 's/[()]//g'`] "

#登录时清空当前缓存
echo "" > .bash_history

#记录shell执行的每一条命令
export PROMPT_COMMAND='\
if [ -z "$OLD_PWD" ];then
export OLD_PWD=$PWD;
fi;
if [ ! -z "$LAST_CMD" ] && [ "$(history 1)" != "$LAST_CMD" ]; then
logger -t `whoami`_shell_cmd "[$OLD_PWD]$(history 1)";
fi ;
export LAST_CMD="$(history 1)";
export OLD_PWD=$PWD;'
#source一下
source /etc/profile
# 日志记录
/var/log/messages

LRF 记录学习、生活的点滴