---
title: "selinux"
author: "lkf009"
date: "2011-12-28T18:42:06Z"
tags: []
categories: ["操作系统"]
slug: "selinux"
draft: false
lastmod: "2011-12-27T17:00:40Z"
id: 1327987
url: "https://lkf009.iteye.com/blog/1327987"
---

selinux

对超级管理员有效  
安全加强型linux  
自主访问体制 ：又大变小  
user         group           other  
        \\                  |                  /  
                    file  
                  /        |            \\  
             r      w       x  
强制访问体制 最小权限：由小该大  
打开SELINUX  
 sestatus 查看slinux状态  
vim/etc/sysconfig/selinux  
                enforcing - SELinux security policy is enforced.强制限制  
  4 #       permissive - SELinux prints warnings instead of enforcing. 警告但不限制  
  5 #       disabled - SELinux is fully disabled.  
 SELINUXTYPE=targeted 定义使用的SELINUX  保护大多数的网络服务  
1改为 permissive  
2.配置  
sealert -a /var/log/audit/audit.log 显示所有的selinux报错  
布尔直boolean 0关1开  
安全上下文  
man -k(模糊查询)ftp｜grep selinux  
ls －Zd      /var/ftp            安全上下文  
system\_u:object\_r:public\_content\_t:s0  
       身份         角色              类型

设置semanage fcontext -a -t public\_content\_rw\_t "/var/ftp/incoming(/.\*)?"

恢复 restorecon -R -v /var/ftp/incoming  
setenforce 0关只警告不阻止1开启 警告1警告和阻止  
setenforce  permissive &lt;===&gt; setenforce  0          0关只警告不阻止  
setenforce  enforcing  &lt;===&gt; setenforce  1            1警告并且阻止

1、开启FTP服务，并用普通系统帐号直接登录FTP服务器，selinux会不允许访问，上传。  
 如何配置selinux来解决？  
   思路：  
A、看日志  
  1、tail /var/log/messages ---  
    sealert -l xxxxxxxxxxxxxxxxxxxxx  看指定ID的警告提示消息  
  2、sealert -a /var/log/audit/audit.log 看所有的ID的警告消息  
  3、有一个程序会自动弹出提示(右上角的五角星SelinuxTroubleshooter程序）,此程序要想正常的运行，必须ps aux | grep setroubleshootd 进程存在（]# setroubleshootd 执行此命令便可）  
B、日志中会提示如何配置selinux,以便开放相应的权限  
  最好自行查询帮助来解决问题  
\[root@ mnt]# man ftpd\_selinux  
\[root@ mnt]# man httpd\_selinux  
\[root@ mnt]# man samba\_selinux  
\[root@ mnt]# man nfs\_selinux  
\[root@ mnt]# man -k ftp  | grep selinux  
ftpd\_selinux         (8)  - Security-Enhanced Linux policy for ftp daemons  
\[root@ mnt]# man 8 ftpd\_selinux  
\[root@ mnt]# man -k http  | grep selinux  
httpd\_selinux        (8)  - Security Enhanced Linux Policy for the httpd daemon

setenforce  permissive &lt;===&gt; setenforce  0  
setenforce  enforcing  &lt;===&gt; setenforce  1  
sestatus  
目的：设置匿名用户可以上传下载FTP  192.168.0.28  
1、设置selinux为只警告，不阻止状态  
 setenforce  0  
2、开始配置vsftpd  
vim /etc/vsftpd/vsftpd.conf  
anon\_root=/ftp /ftp共享目录  
anon\_upload\_enable=YES ／／允许下载  
anon\_mkdir\_write\_enable=YES  ／／允许上传  
anon\_other\_write\_enable=YES ／／允许其他人上传  
\---  
mkdir -p /ftp/public  
chown ftp /ftp/public  
chmod u=rwx /ftp/public  
\---  
service vsftpd restart  
\----  
3、客户端匿名访问，确实可以上传，说明 FTP配置文件权限设置正确，/ftp/public目录权限设置正确  
4、把selinux设置为enforcing状态。  
需要配置selinux允许匿名写:  
  man  8  ftpd\_selinux  
   a.根据Manual来修改匿名共享根目录的file type（ls -Z）  
 chcon -t public\_content\_rw\_t  /ftp/public  
 如果不改，默认第三段是default\_t  
 b.根据manual来修改布尔值,开启如下  
 setsebool -P allow\_ftpd\_anon\_write on 或者  
 setsebool -P allow\_ftpd\_anon\_write=1  
作业1：  
1 请问selinux开启的情况下，apache的默认页面是否可以查看 ，ftp的匿名访问是否可以访问  
2 请在selinux开启的情况下，配置apache的别名  
3 请在selinux开启的情况下，配置samba通过用户sambauser1访问家目录和访问指定的共享目录  
4 quota配额  
5 ext3 的acl属性 setfacl /getfacl  
6 apache+ldap验证  
SELINUX例子！  
\[root@localhost files]# system-config-selinux  
\[root@localhost files]# pwd  
/etc/selinux/targeted/contexts/files  
\[root@localhost files]# grep /var/www file\_contexts  
在selinux开启的情况下，策略为targeted时，配置网络服务时的思路如下  
  先setenforce  0 ----- permissive  
  然后配置服务  
  接下来找客户端测试服务正常  
   setenforce 1 ----- enforcing  
  然后客户端访问,看是否受限－－－－看日志／帮助－－－根据提示解决限制问题  
解决问题的方法有三种：  
1、看日志  
   得安装  
\[root@localhost www]# yum -y install  setroubleshoot-server  
  并启动此程序  
\[root@localhost www]# setroubleshootd  
   a.tail /var/log/messages  
Dec  5 18:29:32 localhost setroubleshoot: SELinux is preventing access to files with the default label, default\_t. For complete SELinux messages. run sealert -l a098bbc4-3b76-44d7-a717-1181a0c7253d  
   b.sealert -a /var/log/audit/audit.log  
   c.点击 \[系统]－－－［管理］－－－［SElinux Management］--五角星图标

2、看manual  
   man httpd\_selinux  
   man ftpd\_selinux  
   man samba\_selinux  
3、参照法

1、apache  
  apache别名  
查看日志法:  
sealert -a /var/log/audit/audit.log  
查看manual法：  
\[root@localhost www]# man httpd\_selinux  
\[root@localhost www]# chcon -R -t httpd\_sys\_content\_t /www  
参照法：  
\[root@localhost html]# chcon -R  --reference  /var/www/html/index.html   /www/  
\[root@localhost html]# ls -Zd /www/  
drwxr-xr-x  root root root:object\_r:httpd\_sys\_content\_t /www/

2、samba配置家目录共享和指定目录共享,并启动服务

\[root@localhost html]# sestatus  
\[root@localhost html]# setenforce 0  
\[root@localhost html]# yum -y install samba samba-common samba-client  
\[root@localhost html]# vim /etc/samba/smb.conf  
\[homes]  此段关于家目录的共享，默认存在，不用配置  
        comment = Home Directories  
        browseable = no  
        writable = yes  
;       valid users = %S  
;       valid users = MYDOMAIN\\%S  
\[myshare] 添加一段关于指定目录的共享，并且要求设置为用户sambauser1,sambauser2可读可写  
        comment = test for selinux  
        path    = /samba  
        writable = yes  
        browseable = yes  
        valid users = sambauser1 sambauser2

\[root@localhost home]# mkdir /samba  
\[root@localhost home]# useradd -s /sbin/nologin   sambauser1  
\[root@localhost home]# useradd -s /sbin/nologin   sambauser2  
\[root@localhost home]# smbpasswd -a sambauser1  
New SMB password:  
Retype new SMB password:  
\[root@localhost Server]# smbpasswd -a sambauser2  
New SMB password:  
Retype new SMB password:  
Added user sambauser2.  
\[root@localhost samba]# setfacl -m u:sambauser1:rwx /samba/  
\[root@localhost samba]# setfacl -m u:sambauser2:rwx /samba/

找客户端验证samba的家目录共享 和 指定目录共享  
\[root@localhost Server]# smbclient -L 192.168.0.5 -U sambauser1  
\[root@localhost Server]# smbclient -L 192.168.0.5

\[root@localhost /]# mount -t cifs //192.168.0.5/homes  /mnt -o user=sambauser2,pass=123 说明家目录共享实现成功  
\[root@localhost /]# mount.cifs //192.168.0.5/myshare  /net -o user=sambauser1,pass=1234 说明自定义共享实现成功

接下来！！！！！  
开启selinux为enforcing状态

\[root@localhost samba]# setenforce 1

]# chcon -t samba\_share\_t /samba  
]# setsebool -P samba\_enable\_home\_dirs＝1  
]# setsebool -P samba\_export\_all\_rw=1  
]# setsebool -P samba\_export\_all\_ro=1

\[root@localhost html]# restorecon -vR a  
restorecon reset /var/www/html/a context root:object\_r:default\_t:s0-&gt;system\_u:object\_r:httpd\_sys\_content\_t:s0

那么倒底哪种TYPE值应该被设置呢？  
提供了两大种帮助 ：  
man  
      man -k http | grep selinux  
     man -k ftp | grep selinux  
     
selinux的故障诊断器  
    图形的  
        \[root@ ~]# setroubleshootd 直接启动这个程序，会有一个黄色的一角星，在selinux阻止某些事件时，自动弹出帮助 ！  
        \[root@ ~]# ps uax | grep setroubleshootd  
    文本的日志  
       ]# sealert -a  /var/log/audit/audit.log  
        其实/var/log/messages文件中也会有帮助  
         
\------  
setroubleshoot-server-2.0.5-3.el5.noarch.rpm

－－－－  
apache定义别名：  
man -k httpd| grep selinux  
man httpd\_selinux  
 The following  file  contexts  types  are  
       defined for httpd:

              httpd\_sys\_content\_t  
              - Set  files  with  httpd\_sys\_con-  
              tent\_t for content which is avail-  
              able from all  httpd  scripts  and  
              the daemon.

chcon –Rt httpd\_sys\_content\_t  /newdocumentroot  
\-t选项指出文件的类型应该改变而安全上下文的剩余部分保持不动。  
chcon --reference /var/www/html  /newdocumentroot  
\---------------------------------------  
cd  /etc/selinux/targeted/contexts  
grep –Rh httpd\_sys\_content\_t  .

SELINUX配置的时候，一般涉及到  
   1安全上下文中的 type字段 ，通过chcon  
   2配置相应的布尔值，来表示开启或关闭某种功能

\~~~~~~~~~~~~~~~~~~~~~~~~  
setsebool -P ftp\_home\_dir on  
\[root@stu254 contexts]# getsebool -a | grep ftp  
allow\_ftpd\_anon\_write --&gt; off  
allow\_ftpd\_full\_access --&gt; off  
allow\_ftpd\_use\_cifs --&gt; off  
allow\_ftpd\_use\_nfs --&gt; off  
allow\_tftp\_anon\_write --&gt; off  
ftp\_home\_dir --&gt; off  
ftpd\_disable\_trans --&gt; off  
ftpd\_is\_daemon --&gt; on  
httpd\_enable\_ftp\_server --&gt; off  
tftpd\_disable\_trans --&gt; off  
\[root@stu254 contexts]# setsebool -P ftp\_home\_dir on 

\[root@stu254 ~]# restorecon /install.log  
\[root@stu254 ~]# fixfiles relabel

=========回顾=========  
\[root@ ftp]# ls /etc/sysconfig/selinux  
\[root@ ftp]# setenforce 0  
\[root@ ftp]# setenforce 1  
\[root@ ftp]# ls /selinux/booleans/  
\[root@ user1]# ls /selinux/booleans/  
\[root@ user1]# getsebool -a | grep ftp\_home\_dir  
ftp\_home\_dir --&gt; on

\[root@ ftp]# ls /etc/selinux/targeted/contexts/  
\[root@ ftp]# id -Z |ps auxZ | ls -Z  
\[root@ ftp]# man -k samba | grep selinux  
samba\_selinux        (8)  - Security Enhanced Linux Policy for Samba  
\[root@ ftp]# man -k nfs | grep selinux  
nfs\_selinux          (8)  - Security Enhanced Linux Policy for NFS

\[root@ ftp]# tail  /var/log/messages  
\[root@ ftp]# sealert -a /var/log/audit/audit.log  
\[root@ ftp]# setroubleshootd  
\[root@ ftp]# setroubleshootder----故障浏览器

dhcp  dns  ftp nfs samba postfix apache squid  mysql  
extmail

\-------------------------  
\[root@ ftp]# chcon -R --reference=/var/www/html/index.html  /www2

恢复上下文

 1 restorecon -R  dir/file  
    restorecon -RF dir/file

2  \[root@ home]# touch /.autorelabel  
    \[root@ home]# reboot

++++++++++++++++++++++++  
1、配置HTTPD的别名  
chcon -R -t  httpd\_sys\_content\_t   /dir  
2、配置FTP允许系统用户访问  
在selinux为enforcing的模式下，FTP的匿名访问及下载是允许的！

如果需要系统用户通过FTP读写家目录，则需要配置SELINUX开放权限

方法：  
读日志获取信息或者查看手册 ：  
tail /var/log/messages ---&gt; sealert  -l   xxxxxxxxxxxxxxxxxxxxxxxx  
man ftpd\_selinux  
根据提示配置如下布尔值：  
setsebool -P ftp\_home\_dir=1 或者  
setsebool -P ftp\_home\_dir on  
 getsebool -a  查看所有布尔值

3、配置FTP允许匿名上传下载 /public/ftp目录  
\[root@host68 ~]# mkdir -p /public/ftp  
\[root@host68 ~]# chown ftp.ftp /public/ftp/  允许ftp用户写目录  
\[root@host68 ~]# ls -ld /public/ftp/  
drwxr-xr-x 2 ftp ftp 4096 02-12 14:15 /public/ftp/  
\[root@host68 ~]# vim /etc/vsftpd/vsftpd.conf  
anon\_root=/public  指定匿名访问根位置  
anon\_upload\_enable=YES 允许匿名上传  
anon\_mkdir\_write\_enable=YES  允许匿名的创建  
anon\_other\_write\_enable=YES  允许匿名的删除  
\[root@host68 ~]# service vsftpd restart

\[root@host68 ~]# chcon -R -t public\_content\_rw\_t /public/ftp/  
\[root@host68 ~]# setsebool -P allow\_ftpd\_anon\_write on

4、配置NFS实现共享 /nfspublic目录可读可写  
\[root@host68 ~]# mkdir /nfspublic  
\[root@host68 ~]# chmod o+w /nfspublic/  
\[root@host68 ~]# ls  
drwxr-xrwx 2 root root 4096 02-12 14:55 /nfspublic/  
\[root@host68 ~]# tail -n 1  /etc/exports  
/nfspublic  \*(rw)  
\[root@host68 ~]# service portmap restart  
\[root@host68 ~]# service nfs restart  
客户端访问：  
\[root@localhost /]# showmount -e 192.168.1.68  
\[root@localhost /]# mount -t nfs 192.168.1.68:/nfspublic /mnt  
\[root@localhost /]# mkdir /mnt/cba  
man  nfs\_selinux得知如下的帮助 ：  
 If you want to share files read/write you must  
       set the nfs\_export\_all\_rw boolean.

       setsebool -P nfs\_export\_all\_ro＝1  
       chcon -R -t public\_content\_rw\_t /nfspublic/  
5、配置SAMBA共享目录/smbpublic允许指定用户u1可读可写  
     并允许u1用户可通过samba服务访问自己的家目录  
\[root@host68 ~]# mkdir /smbpublic  
\[root@host68 ~]# useradd -s /sbin/nologin u1  
\[root@host68 ~]# chmod o+w /smbpublic/  
\[root@host68 ~]# vim /etc/samba/smb.conf  
290 \[public]  
291         comment = Public Stuff  描述  
292         path = /smbpublic   共享路径  
293         public = yes     
294         valid users = u1  允许的用户  
295         writable = yes        可写  
\[root@host68 ~]# smbpasswd  -a  u1  
New SMB password:  
Retype new SMB password:  
\[root@host68 ~]# service smb restart

\[root@localhost /]# smbclient -L 192.168.1.68 -U u1  
Password: 输入正确的密码  
Domain=\[HOST68] OS=\[Unix] Server=\[Samba 3.0.33-3.28.el5]

        Sharename       Type      Comment  
        ---------       ----      -------  
        public          Disk      Public Stuff  
        IPC$            IPC       IPC Service (Samba Server Version 3.0.33-3.28.el5)  
        u1              Disk      Home Directories  家目录共享不用定义，配置文件中已经有配置共享  
Domain=\[HOST68] OS=\[Unix] Server=\[Samba 3.0.33-3.28.el5]

        Server               Comment  
        ---------            -------

        Workgroup            Master  
        ---------            -------  
        MYGROUP               
在selinux状态为permissive的情况下，客户访问共享：  
\[root@localhost /]# mount -t cifs //192.168.1.68/public  /mnt -o user=u1,pass=u1  
\[root@localhost /]# cd /mnt  
\[root@localhost mnt]# ls  
\[root@localhost mnt]# mkdir a  
\[root@localhost mnt]# ls  
a  
\[root@localhost mnt]# cd ..  
\[root@localhost /]# umount /mnt

\[root@localhost /]# mount -t cifs //192.168.1.68/u1  /mnt -o user=u1  
Password:  
接下来，把selinux设置为enforcing状态，发现家目录的共享不能挂载，于是看日志或者man  
解决方法：  
\[root@host68 ~]# setsebool -P samba\_enable\_home\_dirs=1  
指定的共享smbpublic目录也不可访问的，于是查手册 man samba\_selinux   
解决方法如下：  
\[root@host68 ~]# chcon -t samba\_share\_t /smbpublic/ -R  
用户,文件,进程都有上下文  
 id － Z  
 ls - Z  
 ps -Z  
身份                         角色               类型  
root                       system\_r(用户)         xxxxx  
system\_u(系统文件)        object\_r (文件)     
user\_u  
selinux 描述了一种进程对文件的操作权限。  
selinux在策略中定义 某些上下文的进程 要操作的文件的上下文 必须所指定的字符。指定的字符如何得知呢？通过MAN手册中的帮助来得知。不需要死记。  
restorecon -v  filename  
\- R 递归  
恢复上下文

       ftpd\_selinux(8),   named\_selinux(8),   rsync\_selinux(8),   httpd\_selinux(8),  
       nfs\_selinux(8), samba\_selinux(8),  nis\_selinux(8), ypbind\_selinux(8)