红联Linux门户
Linux帮助

SELinux简单配置

发布时间:2014-07-27 09:32:54来源:linux网站作者:guodong810

SELinux(Secrity-Enhanced Linux,安全增强式Linux)是一种强制存取控制的实现,它的作法是以最小权限原则为基础提供一些更强、更安全的强制存取控制架构来与内核的主要子系统共同动作。


SELinux等级有三个:

Enforcing:  强制的,如果违反则直接阻止
Permissive: 在违反规则时只警告,但是不阻止
Disables:表示SELinux不启用


在启动SELinux时要重新启动系统才能使用。

SELinux可以通过不同的方式进行启动与关闭,也可以使它暂时的关闭与开启


永久关闭方法:

1>setupàFirewall configuration-SELinux 自己选择打开关闭

2>使用命令:[root@localhost ~]# system-config-securitylevel 也可以选择


暂时关闭方法:

setenforce 1  :暂时关闭SELinux

setenforce 0  :开启SELinux Permissive


简单查看SELinux的状态:

getenforce

SELinux中访问控制属性就叫安全上下文,所有的客体(如文件,进程间通信)和主体(进程)都有与其并联的安全上下文,一个安全上下文由用户、角色、和类型标识符3个部分组成。

在SELlinux中安全上下文中的用户和角色标识符除了对强制有一点约束之外对类型强制访问控制策略没有任何影响。我们看一个小例子来说明一下类型标识符的限制。


示例1、http的SELinux应用

[root@localhost www]# cd /mnt/cdrom/Server/

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

warning: httpd-2.2.3-31.el5.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186

Preparing... ################################### [100%]

1:httpd ################################### [100%]


现在我们可以正常的启动http服务

[root@localhost ~]# service httpd start

Starting httpd:  [  OK  ]


但是我们如果修改一下http默认的主目录,会出现怎么样的情况呢!

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf

将<Directory "/var/www/html">  修改为  <Directory "/html">


创建目录

[root@localhost ~]# mkdir /html


这时候我们再来重新启动一次

[root@localhost ~]# service httpd restart

Stopping httpd:  [  OK  ]

Starting httpd: httpd: Syntax error on line 306 of /etc/httpd/conf/httpd.conf: /etc/httpd/conf/httpd.conf:306: <Directory> was not closed.

[FAILED]

可以看到系统会出现错误,这个情况就属于类型标识符的使用

 
查看一下/var/www/html的目录标识符
 
[root@localhost ~]# ll -dZ /var/www/html
 
drwxr-xr-x  root root system_u:object_r:httpd_sys_content_t /var/www/html

 
再查看一下/html的目录标识符
 
[root@localhost ~]# ll -dZ /html
 
drwxr-xr-x  root root root:object_r:default_t          /html    
 
可以看出/var/www/html的目录标识符属于httpd域,而/html则属于default_t,而httpd_t域运行的httpd程序只能监听httpd_port-t类型的端口,并限制此域的进程只能存取标记为httpd_sys_content_t的网页文件,所以httpd服务不能正常启动

 
但是并不是开启了SELinux就不能够改变主目录的位置,我们可以把/html目录修改为httpd_t的域,可以使用chcon命令来更改,我们把/html的域更改为httpd_t的域
 
[root@localhost ~]# chcon -t httpd_sys_content_t /html
 
[root@localhost ~]# ll -dZ /html
 
drwxr-xr-x  root root root:object_r:httpd_sys_content_t /html

 
现在我们再次启动一次http服务
 
[root@localhost ~]# service httpd start
 
Starting httpd:                                            [  OK  ]
 
启动成功!

 
有时候我们又想改变http服务的端口,那么应该怎么办,可以直接启动吗,我们可以尝试着启动一次

 
把服务器端口更改为800
 
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
 
将Listen 80  改为:Listen 800


先安装一个调试工具
 
[root@localhost Server]# yum install setroubleshoot

 
启动setroubleshoot服务
 
[root@localhost Server]# service setroubleshoot start
 
Starting setroubleshootd:                                  [  OK  ]

 
启动http服务器
 
[root@localhost ~]# service httpd restart
 
Stopping httpd:                                            [  OK  ]
 
Starting httpd: (13)Permission denied: make_sock: could not bind to address [::]:800
 
(13)Permission denied: make_sock: could not bind to address 0.0.0.0:800
 
no listening sockets available, shutting down
 
Unable to open logs  [FAILED]

 
这次直接提示了:不能绑定本机的800端口,也可以查看日志
 
[root@localhost Server]# tail  /var/log/messages  可以看到错误提示
 
Dec 27 00:51:56 localhost setroubleshoot: SELinux is preventing tpvmgp (cupsd_t) "execute_no_trans" to /usr/lib/vmware-tools/bin32/appLoader (lib_t). For complete SELinux messages. run sealert -l 383d449b-4b8c-49aa-b7f0-f19bb5499555
 
并且让运行代码查看清楚的错误信息:sealert -l 383d449b-4b8c-49aa-b7f0-f19bb5499555,我们可以自行查看其详细信息。

 
现在提示是不能绑定800端口,那是因为SELinux把httpd的端口固定了几个默认的端口

[root@localhost Server]# 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      5989
 
可以看到关于http的所有端口使用情况,但是800并没有在默认使用行列中,所以系统不能绑定这个端口,也就启动不了服务。
 
但是我们可以通过semanage命令来把这个端口添加到http可以使用的行列中
 
[root@localhost ~]# semanage port -a -t http_port-t _p tcp 800

 
再查看一次端口使用行列
 
[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      800, 80, 443, 488, 8008, 8009, 8443
 
pegasus_http_port_t            tcp      5988
 
pegasus_https_port_t          tcp      5989

 
现在800端口已经可以被http服务使用了,我们可以正常启动http服务了
 
[root@localhost ~]# service httpd start
 
Starting httpd:                                            [  OK  ]
 
服务正常启动!