手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

SELinux 从入门到精通教程

[日期:2018-08-21] 来源:Linux社区  作者:Linux [字体: ]

一、介绍

1.官方意思
SELinux是一种基于 域-类型 模型(domain-type)的强制访问控制(MAC)安全系统,它由NSA编写并设计成内核模块包含到内核中,相应的某些安全相关的应用也被打了SELinux的补丁,最后还有一个相应的安全策略。任何程序对其资源享有完全的控制权。假设某个程序打算把含有潜在重要信息的文件扔到/tmp目录下,那么在DAC情况下没人能阻止他。SELinux提供了比传统的UNIX权限更好的访问控制。

2.简单意思
selinux就像是一种系统的保护机制,如果关闭它的话,也没什么问题,操作上不会因为selinux而感到阻碍和困难,但是你开启之后,你会感觉做什么都是错的,寸步难行。举个乐子:”当你打开了HTTP服务,内容能正常访问,但是你开启selinux之后就报错,那你会觉得是selinux的问题,然后你就设置好这个内容的selinux的上下文(类似用户权限),又可以正常访问了,然后你开开心心地将一个项目代码上传到网页根目录,不能打开了,你会想到selinux ~~ 首页能打开,但是其他jpg、gif等等不能正常显示,你又改权限,噼里啪啦地一顿操作猛如虎,都可以了,但是跳转到别人的网站又不行了,emmm ~~ 直接重装系统“

3.有点意思
selinux毕竟是保护机制,所以肯定能起到保护作用,保护着系统不被任意修改,保护着站点的权限又严格访问限制,你的文件都是等于数据,数据是钱买不了的,想好好地存在于互联网,就要做相对的安全(没有绝对的安全),通过各种手段保护你的数据不被修改等等,安全意识是非常重要的。

4.没什么意思
selinux是非常好的一种保护机制,但在企业中,大多数是没有使用这个安全机制的,毕竟都是直接关闭的,比如云服务器,你买的时候就已经默认是关闭selinux这个保护机制了。所以在这里演示,后期的软件服务和部署都不会开启。

二、Selinux入门--基本设置

1.关于selinux
selinux在系统的分布

[root@linuxidc ~]# find / -name selinux
/sys/fs/selinux
/etc/sysconfig/selinux
/etc/selinux
/usr/lib64/Python2.7/site-packages/selinux
/usr/share/selinux
/usr/libexec/selinux
[root@linuxidc ~]# 

2.分析

selinux目录组成:
/sys/:系统全局设备管理目录,比如cgroup、pstore、selinux、xfs都存在这个目录。
/etc/:服务的配置文件目录。
/usr/:存放二进制文件、共享文件、函数库文件、服务执行文件的目录。

3.命令式宽容模式或严格模式(以下设置全局有效)

查看当前selinux的状态
[root@linuxidc ~]# getenforce
Enforcing
[root@linuxidc ~]# 

设置宽容模式
[root@linuxidc ~]# setenforce 0
[root@linuxidc ~]# getenforce
Permissive
[root@linuxidc ~]# 

设置拒绝模式
[root@linuxidc ~]# setenforce 1
[root@linuxidc ~]# getenforce
Enforcing
[root@linuxidc ~]# 

4.配置文件说明(可以设置关闭、宽容、严格模式)
/etc/sysconfig/selinux 是 /etc/selinux/config 文件的软链接
所以修改这两个其中一个,文件内容都会改变。

5.配置文件

[root@linuxidc ~]# cat /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.        开启模式
#     permissive - SELinux prints warnings instead of enforcing.    宽容模式
#     disabled - No SELinux policy is loaded.       关闭模式
SELINUX=enforcing     #设置selinux的状态模式
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,    #目标模式
#     minimum - Modification of targeted policy. Only selected processes are protected.   #最小化权限控制
#     mls - Multi Level Security protection.    #多种selinux模式,根据文件的上下文设置而改变访问权限
SELINUXTYPE=targeted      #设置selinux的类型,默认目标模式

6.更新配置文件

[root@linuxidc ~]# vim /etc/selinux/config 
[root@linuxidc ~]# cat /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled    #修改配置文件,需要重启系统以生效
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

[root@linuxidc ~]# 

7.重启系统
重启系统方式有很多:
第一种方法:
[root@linuxidc ~]# reboot

第二种方法:
[root@linuxidc ~]# init 6

这是系统启动级别命令,参考我另一个博客文章:
http://blog.51cto.com/linuxidcheng/2161336

第三种方法:

虚拟机--》鼠标右击--》电源--》重新启动客户机
SELinux 从入门到精通教程

8.查看selinux状态

[root@linuxidc ~]# getenforce
Disabled

三、Selinux进阶---上下文

提醒:如果想使用上下文,必须开启selinux才能有效,如果设置了上下文,却没有开启selinux,那等于白忙了。

这里举个例子:其他设置,类比就好

1.安装Apache服务
[root@linuxidc ~]# yum install -y httpd #提供网页服务

2.安装网络工具
[root@linuxidc ~]# yum install -y net-tools #提供查询系统网络状态的工具

3.启动网页服务并检查网站端口(默认端口为80)

[root@linuxidc ~]# systemctl start httpd && netstat -tunlp |grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      1633/httpd          
[root@linuxidc ~]# 

4.访问网页(默认网页)
SELinux 从入门到精通教程

5.开启selinux

[root@linuxidc ~]# getenforce
Enforcing
[root@linuxidc ~]# 

6.设置自定义网页
SELinux 从入门到精通教程

7.查看web上下文

[root@linuxidc html]# ls -Z
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html
[root@linuxidc html]# 

上下文为:httpd_sys_content_t

8.在家目录创建网页文件,并移动到网站根目录

[root@linuxidc html]# cd
[root@linuxidc ~]# vim linuxidc.html
[root@linuxidc ~]# cat linuxidc.html 
<h1 align=center>test2 web</h1>
<h2 align=center>linuxidc</h2>
[root@linuxidc ~]# mv linuxidc.html /var/www/html/
[root@linuxidc ~]# cd /var/www/html/
[root@linuxidc html]# ls -Z
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 linuxidc.html
[root@linuxidc html]# 

9.访问新创建的网页文件(显示没权限访问该文件)
SELinux 从入门到精通教程

10.设置文件selinux,并访问网页(可正常访问该网页文件)
SELinux 从入门到精通教程

11.chcon命令掌握

[root@linuxidc html]# chcon --help
Usage: chcon [OPTION]... CONTEXT FILE...
  or:  chcon [OPTION]... [-u USER] [-r ROLE] [-l RANGE] [-t TYPE] FILE...
  or:  chcon [OPTION]... --reference=RFILE FILE...

选项如下:
--reference     #引用其他文件上下文
--dereference    #不引用其他文件上下文    
-h, --no-dereference     #影响符号链接而不是引用文件
-u, --user=USER      #指定用户
-r, --role=ROLE     #指定角色
-t, --type=TYPE     #指定上下文
-l, --range=RANGE      #指定上下文的范围
--no-preserve-root      #不区分对待根目录
--preserve-root    #区分对待根目录
--reference=RFILE      #直接引用该文件的上下文
-R, --recursive        #递归,一般用在目录
-v, --verbose          #对每个文件输出信息

12.显示一下版本信息

[root@linuxidc html]# chcon --version
chcon (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Russell Coker and Jim Meyering.
[root@linuxidc html]# 

SELinux管理原则  https://www.linuxidc.com/Linux/2018-08/153603.htm
Linux SELinux 工作原理及日志管理详解 https://www.linuxidc.com/Linux/2018-08/153454.htm
深入理解SELinux https://www.linuxidc.com/Linux/2017-10/148081.htm
查看SELinux状态&关闭SELinux  https://www.linuxidc.com/Linux/2016-11/137723.htm

Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址https://www.linuxidc.com/Linux/2018-08/153625.htm

linux
相关资讯       SELinux  SELinux入门 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款