码迷,mamicode.com
首页 > 其他好文 > 详细

权限管理禁止用户su - root并记录用户操作记录实验!

时间:2017-02-27 14:14:34      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:禁止用户   su - root   root   

  本文目的:整理在实验环境中配置的禁止普通用户用su - root来获取root权限的操作记录。实验还添加了history添加用户详细记录,并配置了使用script记录用户操作,和测试用户操作还原的过程。


一、修改禁止su - root权限的配置:
1、修改PAM配置文件:
#vi /etc/pam.d/su
#%PAM-1.0
auth            sufficient      pam_rootok.so
# Uncomment the following line to implicitly trust users in the "wheel" group.
#auth           sufficient      pam_wheel.so trust use_uid
# Uncomment the following line to require a user to be in the "wheel" group.
auth           required        pam_wheel.so use_uid
auth            include         system-auth
account         sufficient      pam_succeed_if.so uid = 0 use_uid quiet
account         include         system-auth
password        include         system-auth
session         include         system-auth
session         optional        pam_xauth.so

2、修改用户帐号限制配置:
echo “SU_WHEEL_ONLY yes” >> /etc/login.defs #添加语句到login.defs文件末尾;

二、修改禁止SSH连接root登录的配置;
vi /etc/ssh/sshd_config
PermitRootLogin no    找到此语句,删掉注释#号,并修改为no;

三、修改用户登录操作命令的历史记录;
1、[root@rongshu ~]# vi /etc/profile        ##文件尾部添加如下代码;
 #history
    USER_IP=`who -u am i 2>/dev/null| awk ‘{print $NF}‘|sed -e ‘s/[()]//g‘`
    HISTDIR=/opt/hislog
    if [ -z $USER_IP ]
    then
    USER_IP=`hostname`
    fi
    if [ ! -d $HISTDIR ]
    then
    mkdir -p $HISTDIR
    chmod 777 $HISTDIR
    fi
    if [ ! -d $HISTDIR/${LOGNAME} ]
    then
    mkdir -p $HISTDIR/${LOGNAME}
    chmod 300 $HISTDIR/${LOGNAME}
    fi
    export HISTSIZE=4000
    DT=`date +%Y%m%d_%H%M%S`
    export HISTFILE="$HISTDIR/${LOGNAME}/${USER_IP}.history.$DT"
    export HISTTIMEFORMAT="[%Y.%m.%d %H:%M:%S]"
    chmod 600 $HISTDIR/${LOGNAME}/*.history* 2>/dev/null  

2、修改log存储目录权限:
chmod -R 743 /opt/hislog    #即上段代码中的/opt/hislog目录;

3、修改用户变量文件,增加详细显示信息;
vi /etc/bashrc
#history
USER_IP=`who -u am i 2>/dev/null| awk ‘{print $NF}‘|sed -e ‘s/[()]//g‘`
HISTFILESIZE=4000
HISTSIZE=4000
HISTTIMEFORMAT="%F %T ${USER_IP} `whoami` "
export HISTTIMEFORMAT

四、配置记录用户行为轨迹:
1、修改配置文件:
[root@rongshu ~]# vi /etc/profile        ##添加如下代码;
-----------------------------------------
#Raw Log Session
if [ $UID -ge 500 ]; then

        exec /usr/bin/script -t 2>/opt/scriptlog/$USER-$UID-`date +%Y%m%d%H%M`.date -a -f -q /opt/scriptlog/$USER-$UID-`date +%Y%m%
d%H%M`.log

fi

-----------------------------------------

注:需注意你使用的用户的UID的值,创建的普通用户默认为500+;

2、修改log存储目录权限:
chmod -R 743 /opt/scriptlog    #即上段代码中的/opt/scriptlog目录;

五、使配置文件生效:
[root@rongshu ~]# /etc/init.d/sshd resart
用法: /etc/init.d/sshd {start|stop|restart|reload|force-reload|condrestart|try-restart|status}
[root@rongshu ~]# /etc/init.d/sshd restart
停止 sshd:                                                [确定]
正在启动 sshd:                                            [确定]
[root@rongshu ~]# source  /etc/profile
[root@rongshu ~]# source  /etc/bashrc


六、添加用户并测试:
1、添加用户:

  添加用户test1并加入wheel组,作为切换root使用;添加test2不加wheel组,做普通用户,测试su - root无法切换到root,操作如下。

[root@rongshu ~]# useradd -g wheel test1
[root@rongshu ~]# echo "hello123" | passwd --stdin test1
更改用户 test1 的密码 。
passwd: 所有的身份验证令牌已经成功更新。
[root@rongshu ~]#
[root@rongshu ~]#
[root@rongshu ~]# useradd test2
[root@rongshu ~]# echo "hello123" | passwd --stdin test2
更改用户 test2 的密码 。
passwd: 所有的身份验证令牌已经成功更新。

2、测试用户登录:

技术分享

↑使用root用户登录被拒绝


技术分享

↑使用test2用户切换root被拒绝


技术分享

↑使用test1用户切换root正常登录


3、使用命令还原用户的操作:

  执行下面的命令后,test1用户的操作会按时间先后,像播放视频记录一样被还原。

[root@rongshu scriptlog]# scriptreplay test1-500-201702270544.date test1-500-201702270544.log
[test1@rongshu ~]$
[test1@rongshu ~]$
[test1@rongshu ~]$ su - root
密码:
[root@rongshu ~]#
[root@rongshu ~]#
[root@rongshu ~]# cd /opt/scriptlog/
[root@rongshu scriptlog]# ls
test1-500-201702270544.date  test1-500-201702270544.log  test2-501-201702270544.date  test2-501-201702270544.log
[root@rongshu scriptlog]#
[root@rongshu scriptlog]# ll
总用量 16
-rw-r--r--. 1 test1 wheel 583 2月  27 05:45 test1-500-201702270544.date
-rw-r--r--. 1 test1 wheel 451 2月  27 05:45 test1-500-201702270544.log
-rw-rw-r--. 1 test2 test2  35 2月  27 05:44 test2-501-201702270544.date
-rw-rw-r--. 1 test2 test2 104 2月  27 05:44 test2-501-201702270544.log
[root@rongshu scriptlog]#

参考文章:
http://mengsir.blog.51cto.com/2992083/1665628
http://blog.csdn.net/a806267365/article/details/40581159

本文出自 “荣书” 博客,请务必保留此出处http://rongshu.blog.51cto.com/681368/1901489

权限管理禁止用户su - root并记录用户操作记录实验!

标签:禁止用户   su - root   root   

原文地址:http://rongshu.blog.51cto.com/681368/1901489

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!