Centos记录所有登陆用户的IP操作日志

将以下代码添加到/etc/profile文件底部

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

 

添加保存以后执行source /etc/profile(或者. /etc/profile)使其生效。

之后效果是会记录每个登录用户的操作命令,登录时间,日志存放在 "/var/log/history/用户名" 目录下,每次用户退出登陆后会生成日志,可供查阅。

最受欢迎

linux服务器通过top看CPU性能指标

服务器变慢负载高时,需要先查看cpu负载是否过高,如果cpu负载高再看查看具体是什么进程占用cpu资源。一. cpu负载:    通过top命令查看:  主要看“load...

Linux修改配置dns生效教程

vi /etc/resolv.conf #比如修改:nameserver 8.8.8.8 改为nameserver 114.114.114.114...

CentOS更改ssh默认连接端口

在使用linux过程中为了安全,我们有可能需要更改ssh默认端口来防止黑客的扫描。我们要知道默认的22端口是不安全的。 首先,使用ssh连接工具登录服务器输入: vi...

CentOS安装软件环境很慢?升级一下吧!

有时我们在安装宝塔或一些程序时候因他们的脚本设计都会先检查你系统环境是否最新,那么此过程较为漫长该怎么解决呢?...

CentOS开启iptables UDP端口

若没有安装请先安装:yum install -y iptables 随后禁用firewalld默认防火墙: #停止firewalld服务 systemctl...