在/etc/profile中写一个shell脚本来记录登陆后的IP地址和某用户名所操作的历史记录!!!
PS1="`whoami`@`hostname`:"'[$PWD]' (Linux系统提示符是用系统变量PS1来定义的)
history
USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'` (who -u am i 会显示系统中登陆进来的用户及登陆从哪个IP登陆进来的,这里后面过滤了就取值一个登陆进来的IP)
if [ "$USER_IP" = "" ]
then
USER_IP=`hostname`
fi
if [ ! -d /tmp/ruige ]
then
mkdir /tmp/ruige
chmod 777 /tmp/ruige
fi
if [ ! -d /tmp/ruige/${LOGNAME} ]
then
mkdir /tmp/ruige/${LOGNAME}
chmod 300 /tmp/ruige/${LOGNAME}
fi
export HISTSIZE=4096
DT=`date '+%Y:%m:%d %r'`
export HISTFILE="/tmp/ruige/${LOGNAME}/${USER_IP} ruige.$DT"
chmod 600 /tmp/ruige/${LOGNAME}/*ruige* 2>/dev/null
会在系统的/tmp新建个ruige目录,在目录中记录了所有的登陆过系统的用户和IP地址,还有历史命令!我们还可以用这个方法来监测系统的安全性。
注意:最好再给ruige这个目录建议你加个t位!!
chmod o+t ruige 或者chmod o+t ruige
补充知识点:
一:linux系统中who am i 和whoami的区别!
whoami 显示的是有效用户ID.!!!
login:root
$who am i
root pts/0 2007-08-16 13:16 (:0.0)
$whoami
root
su tongrui
#who am i
root pts/0 007-08-16 13:16 (:0.0)
#whoami
tongrui
1.whoami显示effective current username(即通过su username1之后,
显示的是username1);
2.who am i显示的是original username(即在使用su命令之前的username).
转载于:https://blog.51cto.com/lovehuhu/845859