在/etc/profile配置文件的末尾加入以下脚本代码就可以实现

    通过下面的代码可以看出来,在系统的/tmp新建个history目录(这个目录可以自定义),在目录中记录了所有的登陆过系统的用户和IP地址,这也是监测系统安全的方法之一。

  1. PS1="`whoami`@`hostname`:"'[$PWD]' 
  2. history 
  3. USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'` 
  4. if [ "$USER_IP" = "" ] 
  5. then 
  6. USER_IP=`hostname` 
  7. fi 
  8. if [ ! -d /tmp/history ] 
  9. then 
  10. mkdir /tmp/history 
  11. chmod 777 /tmp/history 
  12. fi 
  13. if [ ! -d /tmp/history/${LOGNAME} ] 
  14. then 
  15. mkdir /tmp/history/${LOGNAME} 
  16. chmod 300 /tmp/history/${LOGNAME} 
  17. fi 
  18. export HISTSIZE=4096 
  19. DT=`date +"%Y%m%d_%H%M%S"` 
  20. export HISTFILE="/tmp/history/${LOGNAME}/${USER_IP} history.$DT" 
  21. chmod 600 /tmp/history/${LOGNAME}/*history* 2>/dev/null