1. 程式人生 > >記錄linux遠端使用者登陸操作

記錄linux遠端使用者登陸操作

root 使用者下

第一步
vi /etc/profile –新增如下內容

PS1="[`whoami`@`hostname`: "'$PWD]#'
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/whodone ]
then
mkdir /tmp/whodone
chmod 777 /tmp/whodone
fi
if [ ! -d /tmp/whodone/${LOGNAME}
] then mkdir /tmp/whodone/${LOGNAME} chmod 300 /tmp/whodone/${LOGNAME} fi export HISTSIZE=4096 PROMPT_COMMAND='history -a' export HISTTIMEFORMAT="%F %T" DT=`date "+%Y%m%d_%H:%M:%S"` export HISTFILE="/tmp/whodone/${LOGNAME}/${USER_IP}_done_$DT" chmod 600 /tmp/whodone/${LOGNAME}/*done* 2>/dev/null

第二步
source /etc

/profile

第三步(重新登陸)
cd /tmp/whodone/
ll

清理180天history
vi clear_history.sh

#!/bin/bash
var=/tmp/whodone/
cd $var
user=`ls $var`
for i in ${user};
do
cd $var$i
find . -name *done* -mtime +180 | xargs rm -rf
done
exit 0