Skip to content

3 SELinux

层级:

image-20221218133507567

一:安全上下文

​ 用户:角色:类型

[root@localhost ~]# ls -lZ
-rw-------. root root system_u:object_r:admin_home_t:s0 anaconda-ks.cfg

httpd的默认类型:httpd_sys_content_t

设置为httpd的类型:chcon -R -t httpd_sys_content_t 目录

实验:更改网页主目录,验证安全上下文

[root@localhost ~]# yum -y install httpd
[root@localhost ~]# echo "this is 201..." > /var/www/html/index.html
[root@localhost ~]# ls -ldZ /var/www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/

[root@localhost ~]# getenforce 
Enforcing
[root@localhost ~]# mkdir /www
[root@localhost ~]# echo "this is another 201..." > /www/index.html
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf 
DocumentRoot "/www"
<Directory "/var/www">
[root@localhost ~]# systemctl enable httpd --now

此时未访问到自定义网页主目录

image-20221218134711579

[root@localhost ~]# chcon -R -t httpd_sys_content_t /www/
[root@localhost ~]# systemctl restart httpd

修改SELinux上下文后,成功访问

image-20221218134852107

二:命令:设置SELinux类型

chcon [-R] [-t 类型] [-u user] [-r role] 目录
        选项:     -R        #递归
                  -t        #后接类型
                  -u        #后接身份识别
                  -r        #后接角色
        例子: chcon -R -t httpd_sys_content_t /www/

三:命令:设置默认SELinux类型

restore [-Rv] 目录
        选项: 
                -R      #递归
                -v      #显示过程
        例子:
            restorecon -Rv /www/

四:SELinux布尔值

getsebool -a                #查看所有
setsebool -P samba_enable_home_dirs on          #开启samba的共享用户家目录
setsebool -P samba_enable_home_dirs=1           #开启samba的共享用户家目录

setsebool -P samba_export_all_ro on             #samba共享仅读(需要关闭共享用户家目录)

实验:测试安全上下文你,布尔值对samba共享的影响

#C7-1   samba-server
[root@localhost ~]# yum -y install samba            #部署samba服务端
[root@localhost ~]# vim /etc/samba/smb.conf
[public]
        comment = SHARE
        path = /share
        writeable = yes

[root@localhost ~]# mkdir /share                    #创建共享目录,samba用户
[root@localhost ~]# cd /share/
[root@localhost share]# touch {1..10}.txt
[root@localhost ~]# chmod -R 777 /share/
[root@localhost ~]# vim /etc/samba/smb.conf
[root@localhost ~]# systemctl enable smb --now
[root@localhost ~]# useradd yq
[root@localhost ~]# pdbedit -a yq

[root@localhost ~]# getsebool -a|grep samba                 #查看所有关于samba的布尔值选项
[root@localhost ~]# chcon -R -t samba_share_t /share/       #给共享目录设置samba的安全上下文,此时客户端能正常访问共享目录

[root@localhost ~]# setsebool -P samba_enable_home_dirs=1       #设置共享用户家目录,此时客户端能登录samba用户家目录,并有所有权限

[root@localhost ~]# setsebool -P samba_enable_home_dirs=0
[root@localhost ~]# setsebool -P samba_export_all_ro=1          #设置共享仅读,此时客户端能登录samba用户家目录,但仅读


#C7-2   samba-client
[root@localhost ~]# yum -y install samba-client
[root@localhost ~]# smbclient -U yq  //192.168.20.201/public        #登录公共共享目录
[root@localhost ~]# smbclient -U yq  //192.168.20.201/yq            #登录samba用户家目录