SElinux配置httpd

一、启用SELinux策略并安装httpd服务,改变网站的默认主目录为/website,添加SELinux文件标签规则,使网站可访问

    1、修改selinux策略并重启

[root@localhost ~]# vim /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 

    2、安装http服务

yum install -y httpd

    3、修改默认的Directory 项指定的目录为/website

[root@localhost ~]vim /etc/httpd/conf/httpd.conf
<Directory "/website">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>
# Further relax access to the default document root:
<Directory "/website">
DocumentRoot "/website"

    4、添加目录,创建index.html文件

[root@localhost ~]# mkdir /website

[root@localhost ~]# cd /website/

[root@localhost website]# echo "<center><h1>hello world" > index.html

    5、修改index文件selinux标签

[root@localhost website]# chcon -t httpd_sys_content_t index.html  

[root@localhost website]# ls -Z

-rw-r–r–. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html

    6、重启httpd服务,使用网页访问:

[root@localhost website]# systemctl restart httpd
[root@localhost website]# ss -tan
State       Recv-Q Send-Q           Local Address:Port     Peer Address:Port              
            .......(省略).........
LISTEN      0      128              :::80             :::*                  
                  .......(省略).........
注:如遇不能访问,查看防火墙是否打开,使用iptables -F 命令关闭。

blob.png

二、修改上述网站的http端口为9527,增加SELinux端口标签,使网站可访问

    1、编辑httpd配置文件,将80端口替换为9527

[root@localhost website]# vim /etc/httpd/conf/httpd.conf
Listen 9527
使用vim内置命令,直接将80端口替换为9527(:%s/80/9527/)

    2、添加httpd的端口

(需要使用semanage 命令,如没有,使用$(yum install -y policycoreutils-python)安装
1)查看当前http服务端口
[root@localhost website]# semanage port -l | grep http_port_t   
http_port_t                    tcp      80, 81, 443, 488, 8008, 8009, 8443, 9000
2)添加http服务端口9527

    3、重启httpd服务,并查看端口

[root@localhost website]# systemctl restart httpd
[root@localhost website]# ss -tan
State       Recv-Q Send-Q       Local Address:Port                     Peer Address:Port                              
LISTEN      0      128          :::9527    /http端口变为9527监听             :::*

    4、使用网页访问,ip地址加端口号9527(因为web服务默认端口为80,如不手动添加端口,则不能访问)

blob.png

三、启用相关的SELinux布尔值,使上述网站的用户student的家目录可通过http访问

   (如要添加家目录中的网页能通过http访问,需将httpd服务selinux限制家目录e的规则开启:)

    1)查看其相关的规则:

~]# getsebool -a | grep homedir
git_cgi_enable_homedirs --> off
git_system_enable_homedirs --> off
httpd_enable_homedirs --> off   //此规则处于off状态,需要设置为on
.....(省略).....

    2)将http家目录规则开启为on

[root@localhost website]# setsebool -eP httpd_enable_homedirs on   //-P选项为永久更改,直接修改进规则库保存
[root@localhost website]# getsebool -a | grep homedir  
git_cgi_enable_homedirs --> off
git_system_enable_homedirs --> off
httpd_enable_homedirs --> on   //此时此规则为开启
(使用命令"semanage boolean -l"  可查看所有SElinux布尔型规则)
[root@localhost website]# semanage boolean -l 
SELinux boolean                State  Default Description
ftp_home_dir                   (off  ,  off)  Allow ftp to home dir
smartmon_3ware                 (off  ,  off)  Allow smartmon to 3ware
mpd_enable_homedirs            (off  ,  off)  Allow mpd to enable homedirs
xdm_sysadm_login               (off  ,  off)  Allow xdm to sysadm login
.........(省略)........
(使用命令"semanage boolean -l  -C "  可查看所有更改过SElinux布尔型规则)
[root@localhost website]# semanage boolean -l -C
SELinux boolean                State  Default  Description
httpd_enable_homedirs          (on   ,   on)   Allow httpd to enable homedirs

    3)修改http服务的配置文件,将其中的禁用家目录字段开启

        (注:Centos6和Centos7配置文件地址不相同)        
        Centos6配置文件:/etc/httpd/conf/httpd.conf
        Centos7配置文件:/etc/httpd/conf.d/userdir.conf
[root@localhost website]# vim /etc/httpd/conf.d/userdir.conf
<IfModule mod_userdir.c>
#
....(省略)......
#
#UserDir disabled     //此项原来为开启,将其注释掉,
# To enable requests to /~user/ to serve the user's public_html
# directory, remove the "UserDir disabled" line above, and uncomment
# the following line instead:
# 
UserDir web   //此项原来为禁止,将其开启,意为使用用户目录访问,访问方式上面的描述已经说明了“To enable requests to /~user/ to serve the user's public_html ”,由于我更改了目录名称,“public_html”则就为web

    4)在用户家目录下创建web目录,在其中创建index.html文件,更改其他用户访问权限,查看权限及标签信息

[root@localhost ~]# su - li 
[li@localhost ~]$ mkdir web
[li@localhost ~]$ echo "<center><h3>This is user:<h1> li</h1>home" > ./web/index.html
[li@localhost ~]$ cd web/
[li@localhost web]$ chmod 711 index.html
[li@localhost web]$ ls -Z
-rwx--x--x. li li unconfined_u:object_r:httpd_user_content_t:s0 index.html

    5)重启服务,使用格式:"IP地址/~用户名/web/index.html" 访问

[root@localhost ~]# systemctl restart httpd

SElinux配置httpd

原创文章,作者:Lii,如若转载,请注明出处:http://www.178linux.com/47604

(4)
LiiLii
上一篇 2016-09-19
下一篇 2016-09-19

相关推荐

  • 第七周作业,分区格式化及一些脚本练习

    1、创建一个10G分区,并格式为ext4文件系统;    (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;       fidsk划分10G分区,partx命令强制内核重新读磁盘分区表,cat /proc/partitions是否内核识别到新分区 …

    Linux干货 2017-02-01
  • Linux运维学习历程-第二天-虚拟机的配置

    学习Linux我自己的感觉是可以按Linus的哲学思想来学习 比如一切皆文件,那我们首先可以记住一些重要的常见的路径和文件,并知道有什么作用,这样在初期学习时,我们要干什么时,知道在哪里找;   而命令我们可以每天记忆并练习一些,本身Linux的基本命令都是一下短小精悍的而且有些命令名本身就是英文单词,像date命令就是和系统时间有关的命令用来显示…

    Linux干货 2016-08-03
  • Linux中软链接和硬链接的区别

    Linux中软链接和硬链接的区别 链接文件:   Linux中包括两种链接:硬链接(Hard Link)和软链接(Soft Link),软链接又称为符号链接(Symbolic link)。 Inode 文件除了纯数据本身之外,还必须包含有对这些纯数据的管理信息 文件名; 访问权限; 文件的属主以; 该文件的数据所对应的磁盘数据块; 文件的时间戳; …

    Linux干货 2016-10-20
  • CentOS7中nmcli网络管理及使用详解

    一、网络接口配置工具    在CentOS7系统中,强烈推荐使用nmcli管理网卡。下面记录的是nmcli的使用详解。    网络接口配置工具NetworkManager(简称为nmcli),该命令的作用是:可以查询网络连接的状态,也可以用来管理网络(设置系统每个网卡的特性)。该命令如何使用呢,其实可以用"n…

    Linux干货 2016-09-11
  • 网卡别名与bonding配置

    我们知道,一般来说,一个硬件地址对应与一个IP地址。但在一些情况下,我们可以通过一些方法打破硬件地址与IP地址的一一对应关系。下面就来说一下网卡别名与bonding配置。 网卡别名就是将多个IP地址绑定到一个网络接口上,其命名方式为IFACE:num,如eth0:1等。需要注意的是,网卡别名仅对虚拟主机有效。创建网卡别名有两种方式: 1、ifconfig命令…

    Linux干货 2016-09-05
  • 批量部署lxc虚拟机

    前言:lxc是一种操作系统层虚拟化(Operating system–level virtualization)技术,为Linux内核容器功能的一个用户空间接口。它将应用软件系统打包成一个软件容器(Container),内含应用软件本身的代码,以及所需要的操作系统核心和库。通过统一的命名空间和共用API来分配不同软件容器的可用硬件资源,创造出应用程…

    Linux干货 2016-03-20

评论列表(1条)

  • 马哥教育
    马哥教育 2016-09-20 13:55

    文章内容较为完整,但是有乱码,好像不是第一次出现这种情况了哦,需要花点时间找找原因啊。