CentOS 5.x之后,SELinux已经是非常完善的内核模块了,建议不要关闭SELinux!

什么是 SELinux

回到顶部

SELinux是Security Enhanced Linux的缩写,意为安全增强的Linux!
SELinux作为内核模块,是内核的一个安全子系统,为了防止对linux资源的误用,SELinux可以对进程、文件等权限进行安全强化设置。
SELinux可以说是网络服务进程能否存取系统资源的一道关卡! SELinux由美国国家安全局(NSA)开发,官网:selinuxproject.org.

系统资源通过进程进行存取,传统的进程访问文件的权限控制方式称为DAC自主式访问控制(Discretionary Access Control), 当进程访问某个文件时,系统根据进程拥有者或群组比对文件的权限。
DAC自主式访问控制存在一些安全隐患,例如root具有最高权限,一旦某个恶意进程获取了root权限,那么整个linux主机的资源就可以任意存取了。
如果系统管理员网络完全意识太差,为了操作方便,将网站根目录/var/www/html/的权限设置为drwxrwxrwx。 这样任何进程都可以进入该目录读写,一旦www服务进程开启了写入功能,任何人都可以往该目录写入数据,系统很快就会因为硬盘爆满而崩溃,实在太危险了!

为了避免DAC可能发生的安全问题,SELinux引入了MAC委托式访问控制方式(Mandatory Access Control,MAC)!
MAC委托式访问控制可以对特定的进程和文件资源进行权限设置! MAC控制的主体是进程而不是用户!即使进程的拥有者是root,取得的权限也不一定是root,具体要根据该进程的设置。
主体进程也不能任意读写文件资源,因为每个文件也有针对该主体进程设置的权限!
整个系统的进程和文件特别多,一项项设置会很麻烦! SELinux提供一些默认的策略(Policy),并在该策略内提供多个规则(rule),用户可以选择是否开启某个规则!
在委托式访问控制下,进程能够活动的空间就变小了!
例如提供WWW服务的httpd进程, 在MAC的控制下仅能在/var/www/目录下存取文件, 如果httpd进程想要到其他目录去存取数据, 除了开放规则,目标目录也要设置成httpd可读取的类型(type).限制非常多!
这样即使黑客获取了httpd进程的控制权,也无权浏览/etc/shadow这样重要的系统配置文件!
下图说明了WWW网络服务器Apache进程在传统DAC访问控制和加上MAC访问控制后的区别: DAC和MAC的对比 以Apache为例说明DAC和MAC的区别
左边为DAC控制方式,apache进程的用户为root,可以在三个目录内读写文件,右边加上SELinux的MAC控制后, SELinux仅会针对Apache进程放行部份目录, 其他非正规目录就不会放行给Apache使用!即使apache本身具备root权限,也不能跨越MAC的限制!

SELinux运行原理

回到顶部

SELinux通过MAC方式来管控进程的权限,控制的主体是进程,目标则是进程主体访问的资源文件!
主体(Subject):SELinux管理的是进程主体,主体就是进程process;
目标(Object):进程主体访问的目标资源,一般就是文件,目标可以等同于文件系统;
策略(Policy):由于进程与文件数量庞大,因此SELinux会根据某些服务来制订基本的存取安全性策略。 这些策略内还有详细的规则(rule)来指定不同的服务开放某些资源的存取权限。
CentOS 7.x仅提供三个主要的策略,分别是:
targeted :为默认策略,针对网络服务限制较多,针对本机限制较少;
minimum :由targeted修改而来,仅针对选择的进程来保护!
mls: 完整的SELinux限制,限制较为严格。
建议使用默认的targeted策略。

安全上下文(security context)

回到顶部

主体能否访问目标除了策略指定外,主体与目标的安全上下文必须一致才能够顺利存取。 安全上下文(security context)有点类似文件系统的rwx! 安全上下文的设置是非常重要的! 如果设置错误,主体进程就无法存取文件目标资源,会出现权限不符错误信息!
SELinux各组件之间的关系:
SELinux各组件之间的关系 (1)主体进程先通过SELinux的策略规则放行,然后与目标资源进行安全上下文比对;
(2)比对失败则无法存取目标,若比对成功则最终进行传统的rwx权限比对!
CentOS 7.x的target策略已经制订了非常多的规则,只要开启和关闭某项规则即可。 可将安全上下文想成SELinux内必备的rwx!
安全上下文存在主体进程与目标文件中。进程在内存内,可以随时设置。 文件的安全上下文记录在文件的inode内,主体进程想要读取目标文件资源,需要读取inode, 比对inode中的安全上下文以及rwx等权限值。
使用ls -Z观察文件的安全上下文,我们先来看看/root目录下文件的安全上下文:(注意必须启动了SELinux)

              [root@initroot ~]# ls -Z
              -rw-------. root root system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
              -rw-r--r--. root root system_u:object_r:admin_home_t:s0 initial-setup-ks.cfg
              -rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 regular_express.txt
            
安全上下文分为三个字段,字段之间用冒号分隔:
Identify:role:type
身份识别:角色:类型
身份识别(Identify):相当于账号身份识别,常见的身份识别类型:
unconfined_u:不受限的用户,表示该文件由不受限的进程产生! 例如bash进程默认是不受SELinux管制的,因此bash进程所产生的文件, 其身份识别大多就是unconfined_u!
system_u:系统用户,大都是系统产生的文件!
基本上,系统或软件所提供的文件大多是system_u身份,例如某个网络服务所产生的文件,或某个系统服务产生的文件!
系统安装主动产生的anaconda-ks.cfs及initial-setup-ks.cfg就是system_u, 从网络下载的regular_express.txt就是unconfined_u!
角色(Role):角色字段表示本资源是进程、文件还是用户,字段后面的_r表示role!
object_r:代表的是文件或目录等文件资源;
system_r:代表的就是进程!普通用户也会被指定为system_r!
类型(Type)/域(domain):在文件资源(Object)上称为类型(Type),在主体进程(Subject)上则称为域(domain). 最重要的就是这个字段! 基本上,主体进程能不能读取文件资源,与该字段有关!进程的domain需要与文件的type匹配,才能读取文件资源!

通过ps -eZ观察系统进程的SELinux安全上下文信息:

              [root@initroot ~]# ps -eZ
              LABEL PID TTY TIME CMD
              system_u:system_r:init_t:s0 1 ? 00:00:03 systemd
              system_u:system_r:kernel_t:s0 2 ? 00:00:00 kthreadd
              system_u:system_r:kernel_t:s0 3 ? 00:00:00 ksoftirqd/0
              ......
              unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 31513 ? 00:00:00 sshd
              unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 31535 pts/0 00:00:00 bash
            
通过"身份识别:角色"字段大概可以知道某个进程所代表的意义!
大部分进程分为两大类,一种是系统受限的system_u:system_r,另一种则是unconfined_u:unconfined_r, 大多本机用户自己执行程序而产生的比较不受限的进程就是unconfined_u:unconfined_r
这些字段在targeted策略下的对应意义如下:
身份识别 角色 该对应在targeted的意义
unconfined_u unconfined_r 没有受限的进程!大多数都是用户登入系统(网络或者本机登入取得可用的shell)后, 用来操作系统的进程!如bash, X window等。
system_u system_r 多为非交互式的系统进程!
默认的targeted策略下,最重要的字段是类型(type)字段, 主体与目标之间是否具有可读写的权限,与进程的domain及文件的type有关! 我们以crond进程及其相关文件为例说明这两者的关系,这些相关文件包括可执行程序文件/usr/sbin/crond, 配置文件/etc/crontab, /etc/cron.d等.
1. 先查看crond进程的安全上下文:
              [root@initroot ~]# ps -eZ | grep cron
              system_u:system_r:crond_t:s0-s0:c0.c1023 1338 ? 00:00:01 crond
              system_u:system_r:crond_t:s0-s0:c0.c1023 1340 ? 00:00:00 atd
              安全上下文的type类型为crond_t.
            
2. 再来看看可执行文件、配置文件等的安全上下文:
              [root@initroot ~]# ll -Zd /usr/sbin/crond /etc/crontab /etc/cron.d /var/spool/cron
              drwxr-xr-x. root root system_u:object_r:system_cron_spool_t:s0 /etc/cron.d
              -rw-r--r--. root root system_u:object_r:system_cron_spool_t:s0 /etc/crontab
              -rwxr-xr-x. root root system_u:object_r:crond_exec_t:s0 /usr/sbin/crond
            
可执行文件/usr/sbin/crond的type为crond_exec_t,运行该程序生成的crond进程的domain类型真是crond_t, 而crond进程能够读取的配置文件/etc/crontab和/etc/cron.d的type为则为system_cron_spool_t。
主体进程的domain与目标文件的type相互关系:
SELinux各组件之间的关系 crond进程读取配置文件的权限比对流程如下:
1. 可执行程序文件/usr/sbin/crond的类型为crond_exec_t;
2. 运行该文件产生的主体进程crond的域(domain)为crond_t;
3. 默认策略已经针对crond_t域制定了许多规则,其中包括这个域可以读取的目标资源类型, crond_t域被设置为可以读取system_cron_spool_t类型的目标文件(Object),因此/etc/cron.d/目录下的配置文件,都能被crond进程读取;
4. 最后进行传统的rwx权限比对!
上述流程的重点是策略内需制订详细的domain/type相关性; 若进程的domain和文件的type匹配错误,即使文件权限rwx设置为开放的777,主体进程也无法读取目标文件!
我们通过下面的示例验证一下,如果crond配置文件的type不是system_cron_spool_t会怎样.
1. 先在root家目录下新建一个如下的cron设置文件:
              [root@initroot ~]# vim checktime
              10 * * * * root sleep 60s
            
2. 很明显我们放错目录了,使用mv移动到正确目录:
              [root@initroot ~]# mv checktime /etc/cron.d
              [root@initroot ~]# ll /etc/cron.d/checktime
              -rw-r--r--. 1 root root 27 Aug 7 18:41 /etc/cron.d/checktime
              rwx权限是644,任何进程都可以读取
            
3. 重启crond,然后查看日志文件:
            [root@initroot ~]# systemctl restart crond
            [root@initroot ~]# tail /var/log/cron
            Aug 7 18:46:01 study crond[28174]: ((null)) 
            Unauthorized SELinux context=system_u:system_r:system_cronjob_t:s0-s0:c0.c1023 file_context=unconfined_u:object_r:admin_home_t:s0 (/etc/cron.d/checktime)
            Aug 7 18:46:01 study crond[28174]: (root) FAILED (loading cron table)
            
日志文件内说明了SELinux安全上下文(context)和文件上下文file_context的type不匹配,导致crond服务进程无法读取配置文件!

SELinux三种模式的启动、关闭与观察

回到顶部

SELinux有三种运行模式:

enforcing:强制模式,表示SELinux运行中,且已经正确的限制domain/type了;
permissive:宽容模式:表示SELinux运行中,但不会限制domain/type,只会有警告讯息。这种模式主要用于调试SELinux;
disabled:SELinux没有运行。
主体进程需要经过策略规则,安全上下文比对,rwx权限匹配后才能读取文件.
SELinux的三种模式与策略规则、安全上下文的关系如下图所示:
SELinux三种类型与实际运行流程图 并不是所有的进程都会被SELinux管制,因此最左边会出现一个有受限(confined)的进程主体!
我们通过ps -eZ观察crond与bash这两个进程的selinux安全上下文:
              [root@initroot ~]# ps -eZ | grep -E 'cron|bash'
              system_u:system_r:crond_t:s0-s0:c0.c1023 1340 ? 00:00:00 atd
              unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 13888 tty2 00:00:00 bash
              unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 28054 pts/0 00:00:00 bash
              unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 28094 pts/0 00:00:00 bash
              system_u:system_r:crond_t:s0-s0:c0.c1023 28174 ? 00:00:00 crond
            
在目前target策略规则下,只会比对类型(type)字段。可以看到crond是受限的主体进程,bash是不受限(unconfined_t)的,bash不需要经过selinux的流程,而是直接判断rwx。
对于受限的进程主体:
1.在Disabled模式,SELinux不会运行,受限的进程也不会经过SELinux, 而是直接判断rwx;
2.在permissive模式下如果没有通过策略规则或安全上下文的比对,SElinux不会阻止rwx比对,但是selinux会将该问题记录到日志文件中。
3.Enforcing模式下受限主体会进入规则比对、安全上下文比对流程,若失败,直接阻止主体进程的读写,并且记录到日志。如果通过,则进行传统的rwx权限比对!
getenforce命令显示SELinux模式:
              [root@initroot ~]# getenforce
              Enforcing
              显示目前的模式为Enforcing
            
使用sestatus命令查看SELinux的策略(Policy):
              [root@initroot ~]# sestatus [-vb]
            
选项与参数: -v :检查/etc/sestatus.conf文件中列出的文件与进程的安全上下文;
-b :列出当前策略的规则布尔值,即是否开启(0/1)某个规则(rule);
列出当前SELinux的策略(Policy):
              [root@initroot ~]# sestatus
              SELinux status: enabled 是否启动SELinux
              SELinuxfs mount: /sys/fs/selinux SELinux的相关文件系统挂载点
              SELinux root directory: /etc/selinux SELinux根目录
              Loaded policy name: targeted 目前的策略
              Current mode: enforcing 目前的模式
              Mode from config file: enforcing 配置文件内规范的SELinux模式
              Policy MLS status: enabled 是否开启MLS的模式机制
              Policy deny_unknown status: allowed 是否默认抵挡未知的主体进程
              Max kernel policy version: 28
            
上面显示已启动selinux,并且是Enforcing模式,而由配置文件查询得知亦为Enforcing模式,目前的默认策略为targeted。
SELinux的配置文件是/etc/selinux/config:
              [root@initroot ~]# vim /etc/selinux/config
              SELINUX=enforcing enforcing|disabled|permissive
              SELINUXTYPE=targeted 目前仅有 targeted, mls, minimum三种策略
            
可在配置文件中直接修改运行模式和默认策略,更改运行模式和策略都需要重启selinux;
因为SELinux是整合到内核里的, 只可以在SELinux运行下切换运行模式. 如果getenforce显示disabled,可在配置文件中修改成为enforcing, 然后重新启动.
从disable切换到Enforcing或permissive模式时,系统要对文件写入安全性上下文信息(也称为SELinux Label), 开机过程需要等待一些时间,写完之后还得再次重启!
如果在Enforcing模式,由于一些设置的问题导致某些服务无法正常运行,可以将Enforcing模式暂时改为宽容(permissive)模式, 让SELinux只产生警告讯息而不直接抵挡主体进程的读取。
使用setenforce命令切换SELinux的enforcing和permissive模式:
              [root@initroot ~]# setenforce [0|1]
              选项与参数:
              0 :转成permissive宽容模式;
              1 :转成Enforcing强制模式
            
切换并观察SELinux运行模式:
              [root@initroot ~]# setenforce 0
              [root@initroot ~]# getenforce
              Permissive
              [root@initroot ~]# setenforce 1
              [root@initroot ~]# getenforce
              Enforcing
            
setenforce无法在Disabled模式下切换模式.
有时从Disabled切换到Enforcing后,会有一堆服务无法启动,会报错说/lib/xxx目录的数据没有权限读取。 大多是由于重新写入SELinux type (Relabel)出错.最简单的方法就是在Permissive模式下,使用restorecon -Rv /重新还原所有的SELinux类型.

SELinux策略的规则管理

回到顶部

SELinux的三种运行模式直接影响主体进程的权限。
Enforcing模式下影响主体进程的就是target策略内的各项规则(rules)了!
getsebool命令查询各条规则的布尔值,很简单的通过sestatus -b或getsebool -a:

              [root@initroot ~]# getsebool [-a] [规则的名称]
              -a :列出系统上所有的SELinux规则
            
              [root@initroot ~]# getsebool -a
              on表示开启该条规则,off表示关闭该条规则
              abrt_anon_write --> off
              abrt_handle_event --> off
              .......
              cron_can_relabel --> off 和cornd进程有关
              cron_userdomain_transition --> on
              .......
              httpd_enable_homedirs --> off http进程相关
              ......
            
每个规则后面都该条规则是允许放行还是不许放行的布尔值!
需要使用seinfo,sesearch等工具查看每条规则的详细信息,假设centos原版光盘挂载到/mnt下,先安装所需要的软件:
              [root@initroot ~]# yum install /mnt/Packages/setools-console-*
            
              [root@initroot ~]# seinfo [-Atrub]
            
选项与参数:
-A :列出 SELinux 的状态、规则布尔值、身份识别、角色、类别等所有信息;
-u :列出 SELinux 的所有身份识别 (user) 种类;
-r :列出 SELinux 的所有角色 (role) 种类;
-t :列出 SELinux 的所有类别 (type) 种类;
-b :列出所有规则的种类 (布尔值);
列出SELinux在此策略下的统计状态:
              [root@initroot ~]# seinfo
              Statistics for policy file: /sys/fs/selinux/policy
              Policy Version & Type: v.28 (binary, mls)
              Classes:         83   Permissions: 255
              Sensitivities:    1   Categories: 1024
              Types:         4620   Attributes: 357
              Users:            8   Roles: 14
              Booleans:       295   Cond. Expr.: 346
              Allow:       102249   Neverallow: 0
              Auditallow:     160   Dontaudit: 8413
              Type_trans:   16863   Type_change: 74
              Type_member:     35   Role allow: 30
              Role_trans:     412   Range_trans: 5439
              ......
            
从上面可以看到这个策略是targeted ,策略的安全上下文类别有4620个;各种SELinux的规则(Booleans)共制订了295条!
使用seinfo -u和seinfo -r查询所有的身份识别与角色! crond进程的type是crond_t, 能不能找一下crond_t能够读取的文件SELinux type有哪些呢?
              [root@initroot ~]# sesearch [-A] [-s 主体类别] [-t 目标类别] [-b 布尔值]
            
选项与参数:
-A :列出后面数据中,允许读取或放行的相关数据
-t :后面接类别,例如-t httpd_t
-b :后面接SELinux规则,例如-b httpd_enable_ftp_server
找出crond_t这个主体进程能够读取的文件SELinux type:
              [root@initroot ~]# sesearch -A -s crond_t | grep spool
              allow crond_t system_cron_spool_t : file { ioctl read write create getattr ..
              allow crond_t system_cron_spool_t : dir { ioctl read getattr lock search op..
              allow crond_t user_cron_spool_t : file { ioctl read write create getattr se..
              allow crond_t user_cron_spool_t : dir { ioctl read write getattr lock add_n..
              allow crond_t user_cron_spool_t : lnk_file { read getattr } ;
            
allow后面接主体进程以及文件的SELinux type,表示crond_t可以读取system_cron_spool_t类型的文件/目录!
找出crond_t是否能够读取/etc/cron.d/checktime这个我们自定义的配置文件:
              [root@initroot ~]# ll -Z /etc/cron.d/checktime
              -rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 /etc/cron.d/checktime
              # 两个重点,一个是 SELinux type 为 admin_home_t,一个是文件 (file)
              [root@initroot ~]# sesearch -A -s crond_t | grep admin_home_t
              allow domain admin_home_t : dir { getattr search open } ;
              allow domain admin_home_t : lnk_file { read getattr } ;
              allow crond_t admin_home_t : dir { ioctl read getattr lock search open } ;
              allow crond_t admin_home_t : lnk_file { read getattr } ;
            
虽然有crond_t admin_home_t,但是这是总体的信息,并没有针对某些规则的寻找~ 所以还是不确定checktime能否被读取。
所以,现在我们知道/etc/cron.d/checktime这个我们自己复制过去的文件会没有办法被读取的原因,就是因为SELinux type错误啦! 根本就无法被读取~好~那现在我们来查一查,那getsebool -a里面看到的httpd_enable_homedirs到底是什么? 又是规范了哪些主体进程能够读取的SELinux type呢?
              [root@initroot ~]# semanage boolean -l | grep httpd_enable_homedirs
              SELinux boolean State Default Description
              httpd_enable_homedirs (off, off) Allow httpd to enable homedirs
            
httpd_enable_homedirs的功能是允许httpd进程去读取用户家目录.
列出httpd_enable_homedirs规则中,主体进程能够读取的文件SELinux type:
              [root@initroot ~]# sesearch -A -b httpd_enable_homedirs
              Found 43 semantic av rules:
              allow httpd_t home_root_t : dir { ioctl read getattr lock search open } ;
              allow httpd_t home_root_t : lnk_file { read getattr } ;
              allow httpd_t user_home_type : dir { getattr search open } ;
              allow httpd_t user_home_type : lnk_file { read getattr } ;
              ......
            
这个规则主要是放行 httpd_t 能否读取用户家目录的文件!如果这个规则没有启动,基本上httpd_t进程就无法读取用户家目录下的文件!
修改SELinux规则的布尔值setsebool
那么如果查询到某个SELinux rule,并且以sesearch知道该规则的用途后,想要关闭或启动他,又该如何处置?
              [root@initroot ~]# setsebool [-P] 规则名称 [0|1]
              -P:直接将设置值写入配置文件,该设置数据未来会生效的!
            
查询httpd_enable_homedirs这个规则的状态,并且修改这个规则成为不同的布尔值
              [root@initroot ~]# getsebool httpd_enable_homedirs
              httpd_enable_homedirs --> off
              #结果是 off ,依题意给他启动看看!
              [root@initroot ~]# setsebool -P httpd_enable_homedirs 1# 会跑很久很久!请耐心等待!
              [root@initroot ~]# getsebool httpd_enable_homedirs
              httpd_enable_homedirs --> on
            
setsebool最好加上-P选项,这样才能将此设置写入配置文件!一定要学会使用getsebool与setsebool才行!

修改SELinux安全上下文

回到顶部

受限的主体进程读写目标资源需要经过SELinux的三道关卡,第一关考虑SELinux的三种运行模式,第二关考虑SELinux的策略规则是否放行,第三关则是比对SELinux type! 可以通过sesearch来找到主体进程与文件的SELinux type关系! 为了让主体进程能够读到正确的文件,使用chcon修改文件的SELinux type:

              [root@initroot ~]# chcon [-R] [-t type] [-u user] [-r role] 文件
              [root@initroot ~]# chcon [-R] --reference=范例文件 文件
            
选项与参数: -R :连同该目录下的次目录也同时修改;
-t :后面接安全上下文的类型字段!例如httpd_sys_content_t ;
-u :后面接身份识别,例如 system_u(不重要);
-r :后面接角色,例如 system_r;
-v :若有变化成功,请将变动的结果列出来(不重要);
--reference=范例文件:拿某个文件当范例来修改后续接的文件的类型!
将/etc/cron.d/checktime文件的类型改为和/etc/hosts的一样:
              查询/etc/hosts的SELinux type:
              [root@initroot ~]# ll -Z /etc/hosts
              -rw-r--r--. root root system_u:object_r:net_conf_t:s0 /etc/hosts
              [root@initroot ~]# chcon -v -t net_conf_t /etc/cron.d/checktime
              changing security context of ‘/etc/cron.d/checktime’
              [root@initroot ~]# ll -Z /etc/cron.d/checktime
              -rw-r--r--. root root unconfined_u:object_r:net_conf_t:s0 /etc/cron.d/checktime
            
直接将/etc/shadow文件的SELinux type复制到/etc/cron.d/checktime文件上:
              [root@initroot ~]# chcon -v --reference=/etc/shadow /etc/cron.d/checktime
              [root@initroot ~]# ll -Z /etc/shadow /etc/cron.d/checktime
              -rw-r--r--. root root system_u:object_r:shadow_t:s0 /etc/cron.d/checktime
              ----------. root root system_u:object_r:shadow_t:s0 /etc/shadow
            
使用restorecon命令将文件恢复默认的SELinux type:
              [root@initroot ~]# restorecon [-Rv] 文件或目录
              选项与参数:
              -R :连同次目录一起修改;
              -v :将过程显示到屏幕上;
            
将/etc/cron.d/目录下的文件恢复成默认的SELinux type:
              [root@initroot ~]# restorecon -Rv /etc/cron.d
              restorecon reset /etc/cron.d/checktime context system_u:object_r:shadow_t:s0->system_u:object_r:system_cron_spool_t:s0
            
重新启动crond,看有没有正确读取checktime文件:
              [root@initroot ~]# systemctl restart crond
              [root@initroot ~]# tail /var/log/cron
            
可以看到/var/log/cron文件中没有错误信息了. 相比chcon命令了,restorecon要简单的多,直接恢复默认的SELinux type. 建议只使用restorecon搭配-Rv 加上某个目录,修改SELinux的type就变得非常的轻松!
semanage默认目录的安全上下文查询与修改
restorecon可以恢复默认的SELinux type是因为有个地方纪录了每个文件或目录的SELinux默认类型, 使用semanage命令可以查询,增加,修改,删除默认的SELinux type:
              [root@initroot ~]# semanage {login|user|port|interface|fcontext|translation} -l
              [root@initroot ~]# semanage fcontext -{a|d|m} [-frst] file_spec
              选项与参数:
              fcontext :主要用在安全上下文方面的用途,
              -l 为查询的意思;
              -a :增加的意思,可以增加一些目录的默认安全上下文类型设置;
              -m :修改的意思;
              -d :删除的意思;
            
查询/etc /etc/cron.d的默认SELinux type:
              [root@initroot ~]# semanage fcontext -l | grep -E '^/etc |^/etc/cron'
              SELinux             fcontext   type Context
              /etc                all files  system_u:object_r:etc_t:s0
              /etc/cron\.d(/.*)?  all files  system_u:object_r:system_cron_spool_t:s0
            
所以在/etc/cron.d目录下使用vim建立新文件时,默认的SELinux type就是正确的! 现在我们要建立一个/srv/mycron目录,我们希望这个目录的默认type也是system_cron_spool_t:
1. 先建立/srv/mycron同时在内部放入配置文件,同时观察SELinux type:
              [root@initroot ~]# mkdir /srv/mycron
              [root@initroot ~]# cp /etc/cron.d/checktime /srv/mycron
              [root@initroot ~]# ll -dZ /srv/mycron /srv/mycron/checktime
              drwxr-xr-x. root root unconfined_u:object_r:var_t:s0 /srv/mycron
              -rw-r--r--. root root unconfined_u:object_r:var_t:s0 /srv/mycron/checktime
            
2. 观察一下上层/srv的SELinux type:
              [root@initroot ~]# semanage fcontext -l | grep '^/srv'
              SELinux fcontext type Context
              /srv all files system_u:object_r:var_t:s0
            
怪不得mycron会是var_t!
3. 将mycron默认值改为system_cron_spool_t:
              [root@initroot ~]# semanage fcontext -a -t system_cron_spool_t "/srv/mycron(/.*)?"
              [root@initroot ~]# semanage fcontext -l | grep '^/srv/mycron'
              SELinux                    fcontext    type
              Context/srv/mycron(/.*)?   all files   system_u:object_r:system_cron_spool_t:s0
            
4. 恢复/srv/mycron及其子目录的SELinux type:
              [root@initroot ~]# restorecon -Rv /srv/mycron
              [root@initroot ~]# ll -dZ /srv/mycron /srv/mycron/*
              drwxr-xr-x. root root unconfined_u:object_r:system_cron_spool_t:s0 /srv/mycron
              -rw-r--r--. root root unconfined_u:object_r:system_cron_spool_t:s0 /srv/mycron/checktime
            
有了默认值就不怕被乱改了.
semanage的功能很多,主要用到的只有fcontext项目,可以使用semanage查询所有目录默认值,也可以增加默认值!

一个网络服务案例及登录文件协助

回到顶部

上面谈到的各个命令,尤其是 setsebool, chcon, restorecon 等,都是当你的某些网络服务无法正常提供相关功能时, 才需要进行修改的一些命令动作。但是,我们怎么知道哪个时候才需要进行这些命令的修改啊? 我们怎么知道系统因为 SELinux 的问题导致网络服务不对? CentOS 7.x提供auditd与setroubleshootd服务来侦测并记录SELinux产生的错误!
setroubleshoot 错误讯息写入 /var/log/messages 几乎所有和SELinux相关的程序都会以se开头,setroubleshoot服务也是以se开头,troubleshoot就是故障排除的意思!所以一定要启动这个服务。 setroubleshootd服务会将SELinux的错误信息与解决方法记录到/var/log/messages和/var/log/setroubleshoot/*文件中, setroubleshoot需要两个软件,分别是setroublshoot和setroubleshoot-server,可使用yum安装!
原本SELinux信息是由两个服务来记录的,分别是auditd与setroubleshootd。 CentOS 6.x (含 7.x)之后将两者整合在auditd中!没有setroubleshootd服务了! 因此安装好setroubleshoot-server之后,必须要重启auditd,否则setroubleshootd的功能不会被启动。
setroubleshootd的运行方式如下:
(1)先由auditd调用audispd服务;
(2)然后audispd服务启动sedispatch 程序,
(3)sedispatch再将原本的auditd讯息转成setroubleshootd的讯息,进一步储存下来!
setroubleshoot默认都是会安装的!使用下面的命令查询是否正确安装:

              [root@initroot ~]# rpm -qa | grep setroubleshoot
              setroubleshoot-plugins-3.0.59-1.el7.noarchsetroubleshoot-3.2.17-3.el7.x86_64
              setroubleshoot-server-3.2.17-3.el7.x86_64
            
可使用yum install安装,安装完毕最好重新启动auditd服务! 刚刚装好且顺利启动后, setroubleshoot还是不会有作用,因为我们并没有任何受限的网络服务主体进程在运行! 早期传送大容量文件以FTP协议为主,在CentOS 7.x下,默认FTP服务器进程是vsftpd!
这里简单的利用vsftpd服务与FTP协议存取系统上的文件来讲解SELinux的问题与错误解决。 FTP客户端需要使用FTP账号才能登入FTP服务器! 也可以使用匿名(anonymous)账号登入系统!但是匿名账号只能存取某一个特定的目录,也无法离开该目录!
在vsftpd中,普通用户与匿名用户的家目录说明如下:
匿名用户:使用浏览器联机到FTP服务器,默认就是匿名登入系统。匿名用户的家目录默认在/var/ftp目录中! 匿名用户在家目录下只能下载数据,不能上传数据,匿名用户也无法离开FTP服务器的/var/ftp目录.
普通FTP账号:默认的情况下,所有UID大于1000的账号,都可以使用FTP登入系统!所有登录系统的账号都能够取得自己家目录下的文件数据!当然默认是可以上传、下载文件的!
我们先建立一个名为ftptest的账号,账号密码为myftp123:
              [root@initroot ~]# useradd -s /sbin/nologin ftptest
              [root@initroot ~]# echo "myftp123" | passwd --stdin ftptest
            
接下来安装vsftpd服务软件,启动服务,并设置开机默认启动:
              [root@initroot ~]# yum install /mnt/Packages/vsftpd-3*
              [root@initroot ~]# systemctl start vsftpd
              [root@initroot ~]# systemctl enable vsftpd
              [root@initroot ~]# netstat -tlnp
              Active Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
              tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1326/sshd
              tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2349/master
              tcp6 0 0 :::21 :::* LISTEN 6256/vsftpd
              tcp6 0 0 :::22 :::* LISTEN 1326/sshd
              tcp6 0 0 ::1:25 :::* LISTEN 2349/master
            
上面的特殊字体表示vsftpd服务已启动.
假设要将/etc/securetty和/etc/sysctl.conf文件提供给所有人下载,将这两个文件复制到/var/ftp/pub目录下:
              [root@initroot ~]# cp -a /etc/securetty /etc/sysctl.conf /var/ftp/pub
              [root@initroot ~]# ll /var/ftp/pub
              -rw-------. 1 root root 221 Oct 29 2014 securetty
              -rw-r--r--. 1 root root 225 Mar 6 11:05 sysctl.conf
              # 先假设你没有看到这个问题!
            
一般默认给用户下载的FTP文件会放到/var/ftp/pub目录! 我们使用简单的终端机浏览器curl观察,看能不能查询到上述两个文件的内容.
1. 先看看FTP根目录下有什么文件存在:
              [root@initroot ~]# curl ftp://localhost
              drwxr-xr-x 2 0 0 40 Aug 08 00:51 pub
            
确实有一个名为pub的目录文件,那就是/var/ftp目录下的pub!
2. 再看能不能看到pub内的文件:
              [root@initroot ~]# curl ftp://localhost/pub/
              因为是目录,要加上/
              -rw------- 1 0 0 221 Oct 29 2014 securetty
              -rw-r--r-- 1 0 0 225 Mar 06 03:05 sysctl.conf
            
3. 继续看一下sysctl.conf的内容:
              [root@initroot ~]# curl ftp://localhost/pub/sysctl.conf
              # System default settings live in /usr/lib/sysctl.d/00-system.conf.
              # To override those settings, enter new settings here, or in an /etc/sysctl.d/<name>.conf file
              #
              # For more information, see sysctl.conf(5) and sysctl.d(5).

            
可以看到文件的内容,是可以让vsftpd读取到这个文件的!
4. 再来看看securetty:
              [root@initroot ~]# curl ftp://localhost/pub/securetty
              curl: (78) RETR response: 550
            
看不到!原因应该是权限问题!vsftpd默认放在/var/ftp/pub内的资料,不论什么SELinux type,都可以被读取的才对.所以需要修改下文件的权限
5. 修改 securetty 文件权限后再一次观察:
              [root@initroot ~]# chmod a+r /var/ftp/pub/securetty
              [root@initroot ~]# curl ftp://localhost/pub/securetty
            
此时就可以看到文件内容了!
6. 修改 SELinux type 的内容 (非必备):
              [root@initroot ~]# restorecon -Rv /var/ftp
            
上面的例子说明在遇到类似的问题时,要先检查文件的权限,如果无法被读取,可能是因为没有设置r或rx,并不一定是由 SELinux 引起的! 下面用普通账号ftptest登录FTP服务器,并下载文件。 命令行的FTP客户端软件, 默认会将用户丢到根目录而不是家目录,因此URL可能需要修订一下!
0. 为了让文字浏览器curl可以传输数据,我们先在 ftptest 家目录建立一些数据:
              [root@initroot ~]# echo "testing" > ~ftptest/test.txt
              [root@initroot ~]# cp -a /etc/hosts /etc/sysctl.conf ~ftptest/
              [root@initroot ~]# ll ~ftptest/
              -rw-r--r--. 1 root root 158 Jun 7 01:05 hosts
              -rw-r--r--. 1 root root 225 Mar 6 11:05 sysctl.conf
              -rw-r--r--. 1 root root 9   Aug 8 2013 test.txt
            
1. 普通账号直接登入 FTP 服务器,同时切换目录到家目录:
              [root@initroot ~]# curl ftp://ftptest:myftp123@localhost/~/
              -rw-r--r-- 1 0 0 158 Jun 07 2013 hosts
              -rw-r--r-- 1 0 0 225 Mar 06 03:05 sysctl.conf
              -rw-r--r-- 1 0 0 8 Aug 08 17:05 test.txt
            
可以看到文件,并且文件最左边的权限也没有问题,来读一下test.txt的内容看看
2. 下载 test.txt, sysctl.conf 等有阅读权限的文件:
              [root@initroot ~]# curl ftp://ftptest:myftp123@localhost/~/test.txt
              curl: (78) RETR response: 550
            
竟然没有权限!rwx权限是没有问题的,那是否有可能是SELinux的问题呢?
3. 先将SELinux 从 Enforce 转成 Permissive,观察日志文件
              [root@initroot ~]# setenforce 0
              [root@initroot ~]# curl ftp://ftptest:myftp123@localhost/~/test.txt
              testing
              还原回Enforcing模式:
              [root@initroot ~]# setenforce 1
            
转成Permissive模式后就可以看到数据内容了!确定是SELinux造成无法读取,要改规则?还是改type?
此时可以查看一下日志文件,看有没有相关的信息可以参考:
            [root@initroot ~]# vim /var/log/messages
            Aug 9 02:55:58 station3-39 setroubleshoot: SELinux is preventing /usr/sbin/vsftpd 
            from lock access on the file /home/ftptest/test.txt. For complete SELinux messages.
            run sealert -l 3a57aad3-a128-461b-966a-5bb2b0ffa0f9
            Aug 9 02:55:58 station3-39 python: SELinux is preventing /usr/sbin/vsftpd from
            lock access on the file /home/ftptest/test.txt.
            *****
            Plugin catchall_boolean (47.5 confidence) suggests
            ******************
            If you want to allow ftp to home dir
            Then you must tell SELinux about this by enabling the 'ftp_home_dir' boolean.
            You can read 'None' man page for more details.
            Do 
            setsebool -P ftp_home_dir 1
            *****
            Plugin catchall_boolean (47.5 confidence) suggests
            ******************
            If you want to allow ftpd to full access
            Then you must tell SELinux about this by enabling the 'ftpd_full_access' boolean.
            You can read 'None' man page for more details.
            Do 
            setsebool -P ftpd_full_access 1
            *****
            Plugin catchall (6.38 confidence) suggests
            **************************
            .....(底下省略).....
            
上面至少提供了三个解决方案(最后一个没列出来),哪种才是正确的?
为了了解正确的解决方案,我们还是先执行一下sealert 那行.
4. 通过 sealert 的解决方案来处理问题:
              [root@initroot ~]# sealert -l 3a57aad3-a128-461b-966a-5bb2b0ffa0f9
              SELinux is preventing /usr/sbin/vsftpd from lock access on the file /home/ftptest/test.txt.
              底下说有 47.5% 的机率是由于这个原因所发生,并且可以使用 setsebool 去解决!
              *****
              Plugin catchall_boolean (47.5 confidence) suggests
              ******************
              If you want to allow ftp to home dir
              Then you must tell SELinux about this by enabling the 'ftp_home_dir' boolean.
              You can read 'None' man page for more details.
              Do
              setsebool -P ftp_home_dir 1# 底下说也是有 47.5% 的机率是由此产生的!
              *****
              Plugin catchall_boolean (47.5 confidence) suggests
              ******************
              If you want to allow ftpd to full access
              Then you must tell SELinux about this by enabling the 'ftpd_full_access' boolean.
              You can read 'None' man page for more details.
              Do
              setsebool -P ftpd_full_access 1
              底下说,仅有 6.38% 的可信度是由这个情况产生的!
              *****
              Plugin catchall (6.38 confidence) suggests
              **************************
              If you believe that vsftpd should be allowed lock access on the test.txt file by default.
              Then you should report this as a bug.
              You can generate a local policy module to allow this access.
              Do
              allow this access for now by executing:
              # grep vsftpd /var/log/audit/audit.log | audit2allow -M mypol
              # semodule -i mypol.pp
              # 底下就重要了!是整个问题发生的主因~最好还是稍微瞧一瞧!     
              Additional Information:
              Source Context system_u:system_r:ftpd_t:s0-s0:c0.c1023
              Target Context unconfined_u:object_r:user_home_t:s0
              Target Objects /home/ftptest/test.txt [ file ]
              Source vsftpd
              Source Path /usr/sbin/vsftpd
              Port <Unknown>
              Host station3-39.gocloud.vm
              Source RPM Packages vsftpd-3.0.2-9.el7.x86_64
              Target RPM Packages
              Policy RPM selinux-policy-3.13.1-23.el7.noarch
              Selinux Enabled True
              Policy Type targeted
              Enforcing Mode Permissive
              Host Name station3-39.gocloud.vm
              Platform Linux station3-39.gocloud.vm 3.10.0-229.el7.x86_64
              #1 SMP Fri Mar 6 11:36:42 UTC 2015 x86_64 x86_64
              Alert Count 3
              First Seen 2015-08-09 01:00:12 CST
              Last Seen 2015-08-09 02:55:57 CST
              Local ID 3a57aad3-a128-461b-966a-5bb2b0ffa0f9Raw Audit Messages
              type=AVC msg=audit(1439060157.358:635): avc:
              denied
              { lock } for
              pid=5029 comm="vsftpd"
              path="/home/ftptest/test.txt" dev="dm-2" ino=141 scontext=system_u:system_r:ftpd_t:s0-s0:
              c0.c1023 tcontext=unconfined_u:object_r:user_home_t:s0 tclass=file
              type=SYSCALL msg=audit(1439060157.358:635): arch=x86_64 syscall=fcntl success=yes exit=0
              a0=4 a1=7 a2=7fffceb8cbb0 a3=0 items=0 ppid=5024 pid=5029 auid=4294967295 uid=1001 gid=1001
              euid=1001 suid=1001 fsuid=1001 egid=1001 sgid=1001 fsgid=1001 tty=(none) ses=4294967295
              comm=vsftpd exe=/usr/sbin/vsftpd subj=system_u:system_r:ftpd_t:s0-s0:c0.c1023 key=(null)
              Hash: vsftpd,ftpd_t,user_home_t,file,lock
            
经过上面的测试,我们知道主要的问题发生在SELinux 的type 不是 vsftpd_t 所能读取的原因 经过仔细观察 test.txt 文件的类型,我们知道他原本就是家目录,因此是 user_home_t 也没啥了不起的啊! 是正确的~ 因此,分析两个比较可信 (47.5%) 的解决方案后,可能是与 ftp_home_dir有关! 所以,我们应该不需要修改 SELinux type, 修改的应该是 SELinux rules 才对!
1. 先确认一下 SELinux 的模式,然后再看看能否下载 test.txt
              [root@initroot ~]# getenforce
              Enforcing
              [root@initroot ~]# curl ftp://ftptest:myftp123@localhost/~/test.txt
              curl: (78) RETR response: 550
            
确定无法读取!
              [root@initroot ~]# setsebool -P ftp_home_dir 1
              [root@initroot ~]# curl ftp://ftptest:myftp123@localhost/~/test.txt
              testing
            
处理完毕!现在用户可以在自己的家目录上传下载文件了!
2. 下载其他文件试试:
              [root@initroot ~]# curl ftp://ftptest:myftp123@localhost/~/sysctl.conf
              # System default settings live in /usr/lib/sysctl.d/00-system.conf.
              # To override those settings, enter new settings here, or in an /etc/sysctl.d/<name>.conf file
              #
              # For more information, see sysctl.conf(5) and sysctl.d(5).
            
没问题!通过修改 SELinux rule 布尔值,现在就可以使用普通账号在FTP服务器上传/下载数据了! 普通账号用户从非正规目录上传/下载文件
假设我们还想要提供 /srv/gogogo 这个目录给 ftptest使用: 1. 先处理好所需要的目录数据:
            [root@initroot ~]# mkdir /srv/gogogo
            [root@initroot ~]# chgrp ftptest /srv/gogogo
            [root@initroot ~]# echo "test" > /srv/gogogo/test.txt
            
2. 直接使用 ftp 观察一下数据:
            [root@initroot ~]# curl ftp://ftptest:myftp123@localhost//srv/gogogo/test.txt
            curl: (78) RETR response: 550
            
有问题!看看日志文件:
              [root@initroot ~]# grep sealert /var/log/messages | tail
              Aug 9 04:23:12 station3-39 setroubleshoot: SELinux is preventing /usr/sbin/vsftpd from
              read access on the file test.txt. 
              For complete SELinux messages. run sealert -l 08d3c0a2-5160-49ab-b199-47a51a5fc8dd
              [root@initroot ~]# sealert -l 08d3c0a2-5160-49ab-b199-47a51a5fc8dd
              SELinux is preventing /usr/sbin/vsftpd from read access on the file test.txt.
              # 虽然这个可信度比较高~不过,因为会全部放行 FTP ,所以不太考虑!
              *****
              Plugin catchall_boolean (57.6 confidence) suggests
              ******************
              If you want to allow ftpd to full access
              Then you must tell SELinux about this by enabling the 'ftpd_full_access' boolean.
              You can read 'None' man page for more details.
              Do
              setsebool -P ftpd_full_access 1
              # 因为是非正规目录的使用,所以这边加上默认 SELinux type 恐怕会是比较正确的选择!
              *****
              Plugin catchall_labels (36.2 confidence) suggests
              *******************
              If you want to allow vsftpd to have read access on the test.txt file
              Then you need to change the label on test.txt
              Do
              # semanage fcontext -a -t FILE_TYPE 'test.txt'
              where FILE_TYPE is one of the following: NetworkManager_tmp_t, abrt_helper_exec_t, abrt_tmp_t,
              abrt_upload_watch_tmp_t, abrt_var_cache_t, abrt_var_run_t, admin_crontab_tmp_t, afs_cache_t,
              alsa_home_t, alsa_tmp_t, amanda_tmp_t, antivirus_home_t, antivirus_tmp_t, apcupsd_tmp_t, ...
              Then execute:
              restorecon -v 'test.txt'
              *****
              Plugin catchall (7.64 confidence) suggests
              **************************
              If you believe that vsftpd should be allowed read access on the test.txt file by default.
              Then you should report this as a bug.
              You can generate a local policy module to allow this access.
              Doallow this access for now by executing:
              # grep vsftpd /var/log/audit/audit.log | audit2allow -M mypol
              # semodule -i mypol.pp
              Additional Information:
              Source Context system_u:system_r:ftpd_t:s0-s0:c0.c1023
              Target Context unconfined_u:object_r:var_t:s0
              Target Objects test.txt [ file ]
              Source vsftpd
              .....(底下省略).....
            
因为是非正规目录,所以感觉上似乎与 semanage 那一行的解决方案比较相关~接下来就是要找到FTP 的 SELinux type! 我们查一下 FTP 相关的数据!
3. 先查看一下 /var/ftp的 SELinux type:
              [root@initroot ~]# ll -Zd /var/ftp
              drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /var/ftp
            
4. 以 sealert 建议的方法处理SELinux type:
              [root@initroot ~]# semanage fcontext -a -t public_content_t "/srv/gogogo(/.*)?"
              [root@initroot ~]# restorecon -Rv /srv/gogogo
              [root@initroot ~]# curl ftp://ftptest:myftp123@localhost//srv/gogogo/test.txt
              test
            
终于搞定!在这个例子中,我们修改了 SELinux type !与前一个修改SELinux rule不太一样!
无法变更 FTP 联机端口问题分析与解决
在某些情况下,可能你的服务器软件需要开放在非正规的端口,举例来说, 如果因为某些策略问题,导致 FTP 启动的正常的 21 号端口无法使用, 因此你想要启用在 555 号端口时,该如何处理呢? 基本上,既然 SELinux 的主体进程大多是被受限的网络服务,没道理不限制放行的端口!所以,很可能会出问题~那就得要想想办法才行!
1. 先处理 vsftpd 的配置文件,加入换 port 的参数才行!
              [root@initroot ~]# vim /etc/vsftpd/vsftpd.conf
            
按下大写的G到最后一行,增加如下行,前面不可以留白:
listen_port=555
2. 重启vsftpd并观察日志文件的变化:
              [root@initroot ~]# systemctl restart vsftpd
              [root@initroot ~]# grep sealert /var/log/messagesAug
              9 06:34:46 station3-39 setroubleshoot: SELinux is preventing /usr/sbin/vsftpd from
              name_bind access on the tcp_socket port 555. For complete SELinux messages. run
              sealert -l 288118e7-c386-4086-9fed-2fe78865c704
              [root@initroot ~]# sealert -l 288118e7-c386-4086-9fed-2fe78865c704
              SELinux is preventing /usr/sbin/vsftpd from name_bind access on the tcp_socket port 555.
              *****
              Plugin bind_ports (92.2 confidence) suggests
              ************************
              If you want to allow /usr/sbin/vsftpd to bind to network port 555
              Then you need to modify the port type.
              Do
              # semanage port -a -t PORT_TYPE -p tcp 555
              where PORT_TYPE is one of the following: certmaster_port_t, cluster_port_t,
              ephemeral_port_t, ftp_data_port_t, ftp_port_t, hadoop_datanode_port_t, hplip_port_t,
              port_t, postgrey_port_t, unreserved_port_t.
              .....(后面省略).....
            
看一下信任度,高达 92.2%!比较重要的是解决方案里面,那个 PORT_TYPE 有很多选择~但我们是要开启 FTP 端口! 所以就由后续数据找到 ftp_port_t 那个项目!
3. 实际带入 SELinux 端口修订后,重新启动 vsftpd:
              [root@initroot ~]# semanage port -a -t ftp_port_t -p tcp 555
              [root@initroot ~]# systemctl restart vsftpd
              [root@initroot ~]# netstat -tlnp
              Active Internet connections (only servers)
              Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
              tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1167/sshd
              tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1598/master
              tcp6 0 0 :::555 :::* LISTEN 8436/vsftpd
              tcp6 0 0 :::22 :::* LISTEN 1167/sshd
              tcp6 0 0 ::1:25 :::* LISTEN 1598/master
            
4. 看看这个port能不能用:
              [root@initroot ~]# curl ftp://localhost:555/pub/
              -rw-r--r-- 1 0 0 221 Oct 29 2014 securetty
              -rw-r--r-- 1 0 0 225 Mar 06 03:05 sysctl.conf
            
通过上面几个小练习,你应该知道如何处理SELinux的问题了.

总结

回到顶部

程序 (program):二进制(binary)可执行文件,存放在硬盘等储存媒介中;
进程 (process):被加载到内存中运行的程序,执行者的权限与属性、程序的程序代码与所需数据等都会被加载到内存中,操作系统为每个进程分配一个标识符(PID),进程就是正在运行中的程序。
进程之间具有父子关系。所有进程的父进程是PID为1号的init进程。
父进程以fork-and-exec的方式产生子进程.父进程复制(fork)一个一模一样的子进程,被复制出来的子进程再以exec的方式执行程序。
常驻在内存中的进程通常提供一些系统服务功能,称为守护进程(daemon)。
进程管理的命令有:ps, top, pstree等;
进程之间是可以互相控制的,kill命令可以跨进程传递信号(signal);
进程是有优先级(Priority)的,PRI是内核动态调整的,用户只能使用nice值微调PRI,nice值可以通过如下命令设置: nice, renice, top 等命令;
在shell作业管理(job control)中,带命令提示符可以操作的环境称为前台(foreground),其他工作可以放入后台(background)暂停或运行。
与job control有关的快捷键与关键词有: &, [ctrl]-z, jobs, fg, bg, kill %n 等;
vmstat命令可以观察系统资源使用情况;
SELinux设计的初衷是为了避免用户资源的误用,SELinux使用的是MAC委托式存取设置;
SELinux的重点在于主体进程 (Subject)能否存取目标文件资源(Object) ,这中间牵涉到策略(Policy)内的规则, 以及实际的安全上下文类别(type);
安全上下文的一般设置为:
Identify:role:type
其中又以 type 最重要;
SELinux 的模式有: enforcing, permissive, disabled 三种,而启动的策略 (Policy) 主要是 targeted SELinux 启动与关闭的配置文件在: /etc/selinux/config;
SELinux 的启动与观察: getenforce, sestatus 等命令;
重设SELinux的安全上下文可使用 restorecon 与 chcon;
在 SELinux 有启动时,必备的服务至少要启动 auditd 这个!
若要管理默认的 SELinux 布尔值,可使用 getsebool, setsebool 来管理!
程序 (program) 是系统上面可以被执行的文件。当程序被执行后,就会启动成进程 (process), 一个 program 可以被不同的用户或者相同的用户重复的执行成为多个进程, 且该程序所造成的进程还因为不同的用户,而有不同的权限,且每个 process 几乎都是独立的。
查询 crontab 命令可以使用 man crontab 或 info crontab ,至于查询 /etc/crontab ,则可以使用 man 5 crontab!
ps -lA | grep crond查询 crond 这个 daemon 的 PID 与他的 PRI 值;
先以 ps aux 找到 crond 的 PID 后,再以: renice -n number PID 来调整crond 这个 PID 的优先级!
普通用户仅能调整自己的 PID 进程,不能调整不属于自己进程的 nice 值,并且只能将 nice 值调高,不能调低,例如调整为 10 之后,就不能降回 5!
可以使用 dmesg 来视察网卡在开机的过程中有没有被识别到!

initroot编辑整理,转载请注明www.initroot.com