selinux-网络服务安全

一、显示和设置selinux

 [root@localhost ~]# vim /etc/sysconfig/selinux //强制模式 许可模式 禁用模式
[root@localhost ~]# getenforce //查看当前SElinux的状态
[root@localhost ~]# setenforce //可以在强制模式(1) 许可模式(0)之间切换
[root@localhost ~]# sestatus //列出目录SELinux使用的策略(Policy)

 

二、查看安全上下文

进程

ps -ZC sshd

文件

ll -dZ /var/www/html/

端口

[root@localhost ~]# yum provides *bin/semanage
[root@localhost ~]# semanage port -l |egrep '<80>'
http_port_t tcp 80, 443, 488, 8008, 8009, 8443

三、修改selinux 上下文

cp 、 mv 对上下文影像

cp:会重新生成安全上下文

 mv:安全上下文则不变

Chcon:

1、  chcon –R httpd_sys_content_t  /webdata

2、  chcon –R –reference /var/www/html   /webdata     //将前一个文件的上下文作为参考,赋给/webdata

例:ftp匿名上传

  1. 文件系统的权限
    [root@localhost ~]# mkdir /var/ftp/music
    [root@localhost ~]# setfacl -m u:ftp:rwx /var/ftp/music/
    2. FTP服务器配置
    anonymous_enable=YES
    anon_upload_enable=YES
    anon_mkdir_write_enable=YES

修改上下文

chcon –R  public_content_rw_t  /var/ftp/music/

3.查看

[root@localhost ~]# getsebool -a | grep ftpd

4.设置

[root@localhost ~]# setsebool -P allow_ftpd_anon_write on

5.启动

[root@localhost ~]# systemctl start vsftpd

四、监控selinux策略冲突情况

部署selinux日志分析工具

1、安装setroubleshoot-server软件包,才能将SELinux消息发送至/var/log/messages

setroubleshoot-server侦听/var/log/audit/audit.log中的审核信息并将简短摘要发送至/var/log/messages
该摘要包括SELinux冲突的唯一标识符(UUIDs),可用于收集更多信息

2、systemctl  restart  rsyslog

Systemctl  restart  auditd

3、测试

[root@localhost ~]# vim /tmp/index.html

[root@localhost ~]# mv /tmp/index.html /var/www/html/

[root@localhost ~]# curl http://localhost

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

<html><head>

<title>403 Forbidden</title>

</head><body>

<h1>Forbidden</h1>

<p>You don't have permission to access /index.html                 //权限拒绝,不能访问

on this server.</p>

</body></html>

4、查看/var/log/messages

Nov 16 02:03:35 localhost setroubleshoot: SELinux is preventing httpd from open access on the file /var/www/html/index.html. For complete SELinux messages run: sealert -l f1243e54-7eb7-458b-a260-ca1f8ff61070

……………………………………………………..

If you want to fix the label.

/var/www/html/index.html default label should be httpd_sys_content_t.

Then you can run restorecon.

Do

# /sbin/restorecon -v /var/www/html/index.html

………………..

[root@localhost ~]# /sbin/restorecon -v /var/www/html/index.html

/sbin/restorecon reset /var/www/html/index.html context unconfined_u:object_r:user_tmp_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0

5、访问成功

[root@localhost ~]# curl http://localhost

------------------------------

this is a test web !

原文地址:https://www.cnblogs.com/sxchengchen/p/7840698.html