了解selinux,设置给文件selinux的安全上下文件,复制、移动对selinux规则的影响,设置apache、vsftpd的selinux规则

1.了解selinux

1)DAC:指用户访问资源的控制,即权限

MAC:selinux标签,限制进程访问资源,进程归用户所有;当用户调用进程去访问资源(file)时,检查selinux安全标签,匹配了才能访问。

selinux使用selinux用户、角色、类型和级别控制进程访问资源。

selinux属性的格式:user:role:type:level

ls -Z install.log

-rw-r--r--. root root system_u:object_r:admin_home_t:s0 install.log

clip_image002

2)selinux用户:不同于系统用户,selinux用户使用selinux策略的一部分,和系统用户有映射关系,一个系统用户对应一个selinux映射。semange查看关系。

需要安装软件:

yum -y install policycoreutils-python

clip_image004

semanage user -l

clip_image006

3)角色role:用户空间,进程的访问范围标记,如object_r

4)类型或者安全上下文,资源的标记,admin_home_t

5)安全级别:s0最常用

查看:进程、用户、文件的selinux属性

ps -eZ |grep http ##查看http进程的selinux

clip_image008

id -Z ##当前用户的selinux设置

clip_image010

ls -ldZ /var/www/html ##查看http网页跟目录的selinux属性

clip_image012

2.设置文件的selinux属性中安全上下文(type)

1.临时设置

echo 123123 >/var/www/html/index.html

cd /var/www/html

clip_image014

ls -Z index.html ##TYPE:httpd_sys_content_t,只允许apache的进程访问,访问测试

clip_image016

clip_image018

chcon -t admin_home_t index.html ##访问测试,无法访问到文件

clip_image020

clip_image022

restorecon -F -v index.html ##还原selinux属性,-F表示强制,-v详细

clip_image024

clip_image026

目录的selinux设置,在文件的基础上加“-R”选项即可。

2.永久生效

语法:semanage fcontext -{a|d|m} 文件 ##-a表示增加,-d删除,-l显示,-m修改

注意文件、目录使用绝对路径

文件:

semanage fcontext -a -t httpd_sys_content_t /web/a.file ##添加

clip_image028

cat /etc/selinux/targeted/contexts/files/file_contexts.local ##发现a.file的selinux属性

clip_image030

restorecon -v /web/a.file ##立即生效,restorecon恢复的依据就是file_contexts.local文件中的规则。

clip_image032

semanage fcontext -d -t httpd_sys_content_t /web/a.file ##删除a.file的selinux属性,相当于删除了file_contexts.local中的规则,不能使用vi等删除。

clip_image034

restorecon -F -v /web/a.file ##立即生效

clip_image036

目录:

semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?" ##如果只写目录,目录下的内容的selinux属性不会被修改,使用正则匹配目录下的所有内容,"/web(/.*)"

clip_image038

restorecon -R -F -v /web/ ##立即生效

clip_image040

semanage fcontext -d -t httpd_sys_content_t "/web(/.*)?" ##删除

restorecon -R -F -v /web/

clip_image042

3.复制、移动对selinux规则的影响

移动:selinux不变,覆盖时会覆盖selinux属性

clip_image044

mv /root/index.html /var/www/html/

ls -Z /var/www/html/index.html ##selinux未发生变化

clip_image046

cd /home/

touch index.html

ls -Z /home/index.html ##文件的type:home_root_t

clip_image048

mv /home/index.html /var/www/html/

ls -Z /var/www/html/index.html ##selinux被覆盖,type变为home_root_t

clip_image050

复制:使用目标selinux的属性,覆盖时selinux属性不变

cp /root/install.log /var/www/html

ls -Z /var/www/html/install.log ##selinux的type:httpd_sys_content_t

clip_image052

cp /root/install.log /var/www/html/ ##覆盖,selinux属性不变

clip_image054

注意:对比移动和复制,复制更有利于保持selinux属性、推荐使用。

4.tar打包备份与selinux;特殊selinux type:file_t,default_t,user_tmp_t

1)tar打包时默认selinux丢失,使用“--selinux|--xattrs”可以保持selinux属性。

tar -zcvf /tmp/test-sel.tar.gz /var/www/html/ --selinux

clip_image056

2)特殊type:

file_t:文件没有selinux属性

default_t:文件或目录的selinux与file-context配置文件定义模式不匹配。

两种类型文件,设置了selinux属性的进程都不能访问。

user_tmp_t:用户临时文件的,所有的进程都可以访问。

5.vsftpd/apache

vsftpd:允许匿名:

clip_image058

getsebool -a |grep httpd |grep on$ ##查看sebool

clip_image060

setsebool allow_ftpd_anon_write=on

clip_image062

chcon -R -t public_content_rw_t /var/ftp/pub

chmod 777 /var/ftp/pub/

clip_image064

http:设置

setsebool httpd_enable_cgi=off

semanage fcontext -a -t httpd_sys_content_t "/usr/local/httpd/htdocs(/.*)?"

clip_image066

restorecon -R -v /usr/local/httpd/htdocs

clip_image068