Security Enhanced Linux(SELinux)是一个额外的系统安全层,主要目的是防止已遭泄露的系统服务访问用户数据。

对于一个服务来说,要关注SELinux的三个方面,一是文件SELinux安全上下文中的类型字段要和服务的类型字段匹配,二是服务SELinux允许端口,三是服务某个功能的布尔值。

下面以http服务为例子,一一阐述。

1.安装httpd包,查看是否安装成功 rpm -qa | grep http

httpd-2.4.6-17.el7.x86_64
httpd-tools-2.4.6-17.el7.x86_64

2.编辑配置文件,这里我们需要3个页面,因此需要准备3台虚拟主机,分别提供3个页面做测试。由于虚拟主机不能和本机一起使用,所以关闭本机服务。

vim /etc/httpd/conf/httpd.conf

#DocumentRoot "/var/www/html"

:wq

cp /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf /etc/httpd/conf.d/

vim /etc/httpd/conf.d/httpd-vhosts.conf

<VirtualHost 192.168.1.126:80>
    DocumentRoot "/var/www/html"
    ServerName www1.example.com
    ErrorLog "/var/log/httpd/dummy-host.example.com-error_log"
    CustomLog "/var/log/httpd/dummy-host.example.com-access_log" common
</VirtualHost>

Listen 808
<VirtualHost 192.168.1.126:808>
    DocumentRoot "/var/www/html2"
    ServerName www2.example.com
    ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"
    CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
</VirtualHost>

Listen 8088
<VirtualHost 192.168.1.126:8088>
    DocumentRoot "/html3"
    ServerName www3.example.com
    ErrorLog "/var/log/httpd/dummy-host2.example.com-error_log"
   CustomLog "/var/log/httpd/dummy-host2.example.com-access_log" common
</VirtualHost>

<Directory "/html3">
   AllowOverride None
    # Allow open access:
    Require all granted
</Directory>

:wq

3.由于防火墙规则限制,我们先开放httpd服务

firewall-cmd --permanent --add-service=http

firewall-cmd --permanent --add-port=808/tcp

firewall-cmd --permanent --add-port=8088/tcp

firewall-cmd --reload

4.准备3个页面

vim /var/www/html/index.html

<h1>www1.example.com</h1>

:wq

vim /var/www/html2/index.html

<h1>www2.example.com</h1>

:wq

vim /html3/index.html

<h1>www3.example.com</h1>

:wq

5.重启httpd服务

systemctl restart httpd

Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details.重启失败,是什么原因呢,根据系统提示查看日志

6.journalctl -xn

可以看到有段提示,这就是我们前面讲的端口问题,需要在SELinux中允许服务使用此端口

If you want to allow /usr/sbin/httpd to bind to network port 808
                                      Then you need to modify the port type.

7.可以使用命令查看http服务端口的类型名

semanage port -l | grep http

http_port_t                    tcp      80, 81, 443, 488, 8008, 8009, 8443, 9000

8.依据端口名添加使用端口

semanage port -a -t http_port_t -p tcp 808

semanage port -a -t http_port_t -p tcp 8088

9.修改完成后再次重启服务,此时服务正常运行

10.访问80端口的网页,成功访问

11.访问808端口的网页,成功访问

12.访问8088端口的网页,访问失败

13.这时候产生这个结果的原因是,文件SELinux安全上下文中的类型字段要和服务的类型字段不匹配
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0                 html

drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0  html2

drwxr-xr-x. root root unconfined_u:object_r:default_t:s0  html3

14.修改html3的类型字段

semanage fcontext -a -t httpd_sys_content_t '/html3(/.*)?'

restorecon -vFR /html3/

15.重启服务

16.访问8088端口的网页,访问成功

17.最后一个测试httpd服务某个功能的布尔值

18.一httpd为每个用户提供家目录展示功能为例

19.开启用户家目录访问功能

vim /etc/httpd/conf.d/userdir.conf

#    UserDir disabled

UserDir public_html

:wq

20.新建一个用户,并为其准备展示网页

useradd abc

mkdir /home/abc/public_html

vim /home/abc/public_html/index.html

<h1>abc.example.com</h1>

:wq

chmod o+rx /home/abc

21.重启服务

22.访问用户展示网页 http://192.1685.1.126/~abc

23.该访问被禁止的原因是该功能的布尔值为off,需要更改该功能的布尔值

getsebool -a | grep http   查看http服务支持的功能

httpd_enable_homedirs --> off      用户家目录展示功能

setsebool -P httpd_enable_homedirs on    开启该功能

24.重启服务

25.重新访问

26.到此基本管理SELinux已经介绍完毕

RHEL简单管理SELINUX的更多相关文章

  1. Service系统服务(一):安装一个KVM服务器、KVM平台构建及简单管理、virsh基本管理操作、xml配置文件的应用、为虚拟机制作快照备份、快建新虚拟机

    一.安装一个KVM服务器 目标: 本例要求准备一台 RHEL7.2 服务器,将其搭建为KVM平台,主要完成下列操作: 1> 关闭本机的SELinux保护.防火墙服务   2> 挂载RHEL ...

  2. ssdb主从及双主模型配置和简单管理

    ssdb主从及双主模型配置和简单管理 levelDB是一个key->value 的数据存储库,其只能在本地保存数据,支持持久化,并且支持保存非常大的数据,单机redis在保存较大数据的时候数十G ...

  3. MacOS下对postgresql的简单管理操作

    如何安装在另一篇blog中有述,这里不再赘述.本篇简单说一下安装完postgresql之后的一些管理和查询操作. 首先安装完postgresql之后需要初始化数据库: initdb /usr/loca ...

  4. Centos7管理selinux及使用firewalld管理防火墙

    CentOS 7.0默认使用的是firewall作为防火墙 1.firewalld的基本使用 启动: systemctl start firewalld 查看状态: systemctl status ...

  5. django admin后台(数据库简单管理后台)

    只需要简单的几行胆码就可以生成一个完整的管理后台 这个就是django魅力之一 创建超级用户 python manage.py createsuperuser     ----  之后会提示输入用慕名 ...

  6. 简单管理用户SESSION小记(个人观点,欢迎斧正)

    做了几年码农,记录下一般涉及到用户session管理的方法. 问题说明: a.用户如果点击退出时,可以获取用户动作,这样可以销毁session. b.用户直接关闭浏览器或者直接意外关机情况,无法获取用 ...

  7. JavaScript学习记录总结(七)——dom对象应用之用户简单管理

    <!DOCTYPE html><html><head><title>users.html</title> <meta name=&qu ...

  8. haproxy配置文件简单管理

    版本:python3功能:对haproxy配置文件进行简单的查询.添加以及删除功能操作流程:1.根据提示选择相应的选项2.进入所选项后,根据提示写入相应的参数3.查询功能会返回查询结果,添加.删除以及 ...

  9. MySQL简单管理

    基础入门 ============管理MySQL========== .查看MySQL版本 mysqladmin --version .启动MySQL /etc/init.d/mysqld start ...

随机推荐

  1. CentOS下mysql数据库data目录迁移和配置优化

    目录迁移 关闭数据库服务 service mysqld stop 复制数据库 mv /var/lib/mysql /data/mysql # 或者使用cp -a复制 # 这两个命令都会带权限到新目录去 ...

  2. vue 学习中 版本、问题集锦

    看学习视频,因为年份比较早了,其实vue早已迭代到vue2.0了,遇到一些问题: v-for遍历数组,获取索引 注意:在2.0版是1~10中,$index已废除,索引 (item,index). 如下 ...

  3. GSS1

    于是我拿合并返回节点的线段树(我也不知道应该叫什么名)水了一下$GSS1$ 比$NOIp$之前写的不知道高到哪里去了,并且只用了$\frac{1}{3}$的时间 #include <iostre ...

  4. ssh: Could not resolve hostname git.*****-inc.com : Temporary failure in name resolution fatal: The remote end hung up unexpectedly

    问题出现的情景:使用git pull拉取开发的代码到测试服务器,报错: ssh: Could not resolve hostname git.****-inc.com : Temporary fai ...

  5. vi代码智能提示功能及相关配置

    vim是一款支持插件.功能无比强大的编辑器,无论你的系统是linux.unix.mac还是windows,都能够选择他来编辑文件或是进行工程级别 的coding.如果能把vim用好了,不仅编程效率能得 ...

  6. Java基础(含思维导图)

    很早之前整理的Java基础的一些知识点,思维导图: 1.'别名现象' 对一个对象赋值另一个对象,会指向新的对象引用,赋值前的对象引用会由于不再被引用而被gc回收: 而基本类型则不同.基本类型存储了实际 ...

  7. JDK1.7源码分析01-Collection

    同步发布:http://www.yuanrengu.com/index.php/20180221.html Java的集合类主要由两个接口派生而出:Collection和Map.Collection是 ...

  8. Sublime Text编辑远程Linux服务器上的文件

    sublime有个叫sftp的插件,可以通过它直接打开远程机器上的文件进行编辑,并在保存后直接同步到远程linux服务器上. 用Package Control安装插件 按下Ctrl+Shift+P调出 ...

  9. properties基本用法

    package control; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io. ...

  10. BroadcastReceiver工作过程

    动态注册过程: ContextWrapper.registerReceiver--> ContextImpl.registerReceiver--> ContextImpl.registe ...