SELinux(Security-EnhancedLinux)是美国国家安全局(NAS)对于强制访问控制的实现,字面上的意思就是安全强化的linux之意。

1.开启selinux 后要重新启动系统才能生效

 

2.也可以在修改

[root@localhost ~]# vim /etc/sysconfig/selinux 

6 SELINUX=enforcing         #设置为强制

[root@localhost ~]# vim /etc/sysconfig/i18n

LANG="zh_CN.UTF-8"   #中文界面

 

没有图像界面解决

2.安装apache 测试selinux 及排错

[root@localhost ~]# mount /dev/cdrom /mnt/cdrom/

[root@localhost Server]# rpm -ivh httpd-2.2.3-31.el5.i386.rpm 

设置默认主目录为/abc

[root@localhost ~]# mkdir /abc

[root@localhost ~]# vim index.html

[root@localhost ~]# cd /etc/httpd/conf/

[root@localhost conf]# vim httpd.conf 

281 DocumentRoot "/abc"

开启selinux以后,再次启动httpd的时候发现已经不能启动了。

而且selinux也会出现如下界面

 

点击一下就会出现如下界面:

 

如果没有安装图形界面可以通过查看日志:

[root@localhost ~]# tail -f /var/log/messages

最后一行提示

run sealert -l 6d4b54ef-9617-48f0-9613-b7cc1e18f21b

咱们试着运行一下:效果是一样的

 

先把selinux的参数修改一下试试

[root@localhost ~]# setenforce 0 /1 #0代表警告 但是可以运行,1代表强制

[root@localhost ~]# getenforce #得到selinux的状态。

Enforcing

这样就不用重启系统,比较方便。把参数值改为0的时候是可起来的。1就不能起来

只要把目标和源的上下文修改为一致就可以了

命令如下:

[root@localhost ~]# chcon -R -t httpd_sys_content_t /abc

设置完就可以正常启动和访问了。

下面修改一下监听端口,发现又启动不了了

[root@localhost ~]# cd /etc/httpd/conf

[root@localhost conf]# vim httpd.conf

134 Listen 800

查看selinux支持的http端口

[root@localhost ~]# semanage port -l |grep http

http_cache_port_t tcp 3128, 8080, 8118, 11211, 10001-10010

http_cache_port_t udp 3130, 11211

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

pegasus_http_port_t tcp 5988

pegasus_https_port_t tcp 59

修改方法,把800加入到selinux支持http的端口

[root@localhost ~]# semanage port -a -t http_port_t -p tcp 800

重启一下,就可以基于端口访问

例如:http://192.168.10.100:800/

二.基于ftpselinux配置

[root@localhost ~]# yum install vsftpd -y

添加用户如下

[root@localhost Server]# useradd u1

[root@localhost Server]# passwd u1

[root@localhost ~]# cd /home/u1

[root@localhost u1]# vim u1.txt #创建文件用于测试

U1登录

[root@localhost Server]# ftp 127.0.0.1

ftp> dir

227 Entering Passive Mode (127,0,0,1,49,10)

150 Here comes the directory listing.

226 Directory send OK.

ftp> pwd

257 "/home/u1"

并没有发现u1.txt这个文件

查看日志:

Dec 22 21:52:18 localhost setroubleshoot: SELinux is preventing the ftp daemon from reading users home directories (/home/u1/u1.txt). For complete SELinux messages. run sealert -l f4d80bc3-3352-46c7-8aed-882c00493214 #selinux拒绝访问自己的家目录文件

图形化解决

[root@localhost ~]# system-config-selinux

可以设置如下:把标准的选项勾选上就ok

也可以通过命令设置

[root@localhost ~]# getsebool -a

ftp_home_dir --> off

ftpd_connect_db --> off

ftpd_disable_trans --> off

ftpd_is_daemon --> on

将有关选项设置为ON

[root@localhost ~]# setsebool -P ftp_home_dir=ON

设置完以后就可以进行访问了

查看如下:

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

allow_ftpd_anon_write --> off

allow_ftpd_full_access --> off

allow_ftpd_use_cifs --> off

allow_ftpd_use_nfs --> off

allow_tftp_anon_write --> off

ftp_home_dir --> on

Samba服务器selinux的配置部分

安装

[root@localhost Server]# rpm -ivh perl-Convert-ASN1-0.20-1.1.noarch.rpm

[root@localhost Server]# rpm -ivh samba-3.0.33-3.14.el5.i386.rpm

[root@localhost Server]# mkdir /aa

[root@localhost Server]# cd /aa

[root@localhost aa]# touch aa.txt

[root@localhost aa]# vim /etc/samba/smb.conf

282 [public]

284 path = /aa

285 public = yes

添加smb用户,重启

[root@localhost aa]# smbpasswd -a u1

[root@localhost aa]# service smb start

访问如下:

输入用户和密码点击 u1public都无法访问

查看日志显示权限不够。

Dec 22 22:30:56 localhost smbd[4866]: '/home/u1' does not exist or permission denied when connecting to [u1] Error was 权限不够

命令修改如下

[root@localhost ~]# chcon -R -t samba_share_t /aa

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

samba_domain_controller --> off

samba_enable_home_dirs --> off

samba_export_all_ro --> off

samba_export_all_rw --> off

samba_share_nfs --> off

use_samba_home_dirs --> off

virt_use_samba --> off

[root@localhost ~]# setsebool -P samba_enable_home_dirs=ON

测试可以发现可以访问

操作命令:

Vim /etc/sysconfig/selinux

System-config-selinux

System-config-securitylevel

使用上述两个命令的话要重启。

Getenforce

Setenforce  1  |  0   1表示强制 表示警告  不需要重启

改变策略库的方法:

semanage

  semanage {login|user|port|interface|fcontext|translation} -l [-n]

       semanage login -{a|d|m} [-sr] login_name

       semanage user -{a|d|m} [-LrRP] selinux_name

       semanage port -{a|d|m} [-tr] [-p protocol] port | port_range

       semanage interface -{a|d|m} [-tr] interface_spec

       semanage fcontext -{a|d|m} [-frst] file_spec

       semanage translation -{a|d|m} [-T] level