SELinux
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了SELinux,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5014字,纯文字阅读大概需要8分钟。
内容图文
![SELinux](/upload/InfoBanner/zyjiaocheng/969/95dc5f9f42a0417781c2f13186a63b85.jpg)
一.SElinux
1.SELinux基本概念
1.SELinux(Security-Enhanced Linux) :
是一种访问控制体系,在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件。SELinux 默认安装在 Fedora 和 Red Hat Enterprise Linux 上,也可以作为其他发行版上容易安装的包得到。
ps:selinux是工作在内核中的
2.selinux与linux区别
1)linux:linux属于DAC,自主访问控制,若进程被劫持,则可通过被劫持的进程为跳板,获得启动进程的用户的所有权限。
2)selinux:selinux属于MAC,强制访问控制,进程被启动后,只能在设定范围内进程工作,若进程被劫持,也无法获得启动进程的用户的相关权限
3.SElinux的工作级别:
1)strict:每个进程都受到selinux的控制
2)targeted:仅有限个进程受到selinux控制,可用于监控容易被入侵的进程
4.linux进程的操作:subject operation object
1)subject:进程
2)object:进程或文件
3)operation 文件:open,read,write,close,chown,chmod
ps:subject:domain(域),object:type(类型)
使用-Z选项可添加SELinux安全文本信息
[root@7 ~]# ps auxZ | grep httpd
system_u:system_r:"httpd_t":s0 root 46369 0.0 0.5 226220 5160 ? Ss 20:41 0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0 apache 46379 0.0 0.3 228304 3156 ? S 20:41 0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0 apache 46380 0.0 0.3 228304 3156 ? S 20:41 0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0 apache 46381 0.0 0.3 228304 3156 ? S 20:41 0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0 apache 46382 0.0 0.3 228304 3156 ? S 20:41 0:00 /usr/sbin/httpd -DFOREGROUND
system_u:system_r:httpd_t:s0 apache 46383 0.0 0.3 228304 3156 ? S 20:41 0:00 /usr/sbin/httpd -DFOREGROUND
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 46388 0.0 0.0 112680 984 pts/0 S+ 20:41 0:00 grep --color=auto httpd
ls -Z添加SELinux安全文本信息
[root@7 ~]# ls -Z
-rw-------. root root system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
-rw-r--r--. root root system_u:object_r:admin_home_t:s0 initial-setup-ks.cfg
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 snap.tar.gz
5.SElinux为每个进程或文件提供了安全标签:user:role:type
1)user:SElinux的user
2)role:角色
3)type:类型;一般需要更仅有类型
ps:SELinux规则库中,规定了哪种域能访问哪种或哪些种类型内的文件
2.配置SELinux
1.配置SELinux
1)SELinux是否启用
2)给文件打安全标签,实现控制访问
3)设定某些布尔型特性,实现控制进程个各种功能
2.SELinux的状态:
1)enforcing:强制状态,每个受限的进程都必然受限
2)permissive:启用状态,每个受限的进程违规操作不会被禁止,但会被记录于审计日志
ps:审计日志位置/var/log/audit/audit.log
3)disabled:关闭状态
4)相关命令
"更改selinux状态"
1.getenforce:获取selinux当前状态
[root@7 ~]# getenforce
Enforcing
2.setenforce 0|1:指定selinux状态
[root@7 ~]# setenforce 0
[root@7 ~]# getenforce
Permissive
ps:selinux必须在启动状态下,才可使用setenforce进程更改其状态,并且使用命令更改selinux状态重启后会消失,要永久有效需要更改配置文件
3.selinux配置文件:
1)/etc/sysconfig/selinux
2)/etc/selinux/config
3)更改方式:SELINUX={enforcing|disabled|permissive}
"给文件打安全标签"
1.相关语法:
1) chcon [OPTION]... CONTEXT FILE...
2) chcon [OPTION]... [-u USER] [-r ROLE] [-t TYPE] FILE...
3) chcon [OPTION]... --reference=RFILE FILE...
4)-R选项:可递归进行打安全标签
2.还原文件的默认标签:restorecon [-R] /path/to/file
3.布尔型规则:
1.getsebool:查看布尔型标签
1)语法:getsebool [-a] [boolean]
2)-a选项:查看所有布尔型标签 getsebool -a
查看特定布尔型标签
[root@7 ~]# getsebool httpd_can_connect_ftp
httpd_can_connect_ftp --> off
2.setsebool:设置布尔型标签
1)语法:setsebool [ -P ] boolean value | bool1=val1 bool2=val2 ...
2)-P选项:保存于策略文件中,使其永久生效
[root@7 ~]# getsebool httpd_can_connect_ftp
httpd_can_connect_ftp --> off
[root@7 ~]# setsebool httpd_can_connect_ftp on
[root@7 ~]# getsebool httpd_can_connect_ftp
httpd_can_connect_ftp --> on
4.更改安全标签练习:
1)启用httpd服务,关闭防火墙,并在其对应目录下创建文件,查看其安全标签为,httpd_sys_content_t
[root@7 html]# systemctl start httpd.service
[root@7 html]# systemctl stop firewalld.service
[root@7 html]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@7 ~]# cd /var/www/html/
[root@7 html]# cat test.html
testing
2019.6.2
[root@7 html]# ls -Z test.html
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 test.html
此时的效果
2)更改其安全标签,确认selinux开启
[root@7 html]# chcon -t admin_home_t test.html
[root@7 html]# ls -Z test.html
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 test.html
[root@7 html]# getenforce
Enforcing
此时已经无法访问http
3)开启selinux,确认可以访问
[root@7 html]# setenforce 0
[root@7 html]# getenforce
Permissive
可以进行访问
5)恢复其默认标签
[root@7 html]# restorecon test.html
[root@7 html]# ls -Z test.html
-rw-r--r--. root root unconfined_u:object_r:"httpd_sys_content_t":s0 test.html
无论开启selinux与否,均可以访问
内容总结
以上是互联网集市为您收集整理的SELinux全部内容,希望文章能够帮你解决SELinux所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。