如何禁用SELinux内核保护? (selinux 内核禁用)

SELinux是Linux内核中的一种安全模块,由美国国家安全局(NSA)为了提供额外的安全防护而开发的。它通过强制应用程式安全策略,限制系统中进程的访问,从而提高了系统的安全性。尽管它是一个有益的安全特性,但在某些情况下,需要禁用SELinux保护。怎么办呢?

本文将讨论如何禁用SELinux内核保护。我们将了解SELinux是什么,为什么要禁用它,以及如何禁用它。

SELinux是什么?

SELinux是Linux内核中的一种安全模块。它为Linux提供了一个额外的安全层,以限制应用程式和系统中进程的访问权限。SELinux强制实施安全策略,要求每个进程都使用最小的权限来运行。这个安全框架基于强制访问控制(MAC)技术,使得系统管理员可以根据用户、文件、目录和网络端口等要素来管理安全策略。

为什么要禁用SELinux?

虽然SELinux对于许多用例非常有用,但在一些情况下,它可能会对服务器上的操作造成一些限制。有时,由于安全策略的限制,某些应用程式可能无法运行。期望的访问权限可能被错误地阻止或者限制。虽然这种情况并不常见,但在某些情况下禁用SELinux是必要的。

另一个可能需要禁用SELinux的原因是,有些软件不支持SELinux。这是因为某些软件(例如Docker),会使用自己的安全机制并不需要SELinux来保护容器。

如何禁用SELinux?

在禁用SELinux之前,首先需要知道,SELinux是为了系统安全而存在的。禁用它将增加一些安全风险,因此建议只有在非常必要的情况下才禁用SELinux。此外,应该对服务器上的其他安全机制(例如防火墙)进行升级,以加强安全性保护。

在Linux中,禁用SELinux最常用的方法是通过修改配置文件。以下是具体步骤:

步骤1:打开SELinux配置文件。

要打开SELinux配置文件,请运行以下命令:

sudo vim /etc/selinux/config

步骤2:禁用SELinux。

在SELINUX=后面输入disabled,以禁用SELinux。请注意大写字母。

SELINUX=disabled

步骤3:保存更改。

按“ESC”退出编辑模式,输入“:wq”并按Enter键以保存更改。

步骤4:重新启动服务器。

禁用SELinux之后,应该重新启动服务器以应用更改。请注意,在某些情况下可能需要使用特定命令启动服务器。

结论

SELinux是Linux内核中的一个安全模块,为系统管理员提供了更多的安全保护。虽然它非常有用,但在某些情况下,需要禁用SELinux。禁用时应该提高其他安全措施来弥补禁用SELinux的风险。禁用SELinux的最常用方法是通过修改配置文件。通过了解SELinux的基本知识以及应该在何时禁用它,您可以更好地保护您的服务器。

相关问题拓展阅读:

linux 为什么要关闭selinux

多数情况下是因为没有专业运维,或者运维懒氏茄拦。。。

除了我这么无聊的人,应该不会有多少非专职运维去读那个麻烦死歼胡的SELinux文档->_->

其纳弊实多数情况下你们的东西运行不了只要一个restorecon就可以搞定->_->

一般安装linux课程时都把SELinux与iptables安排在后面,使初学者配置linux服务器时不成功,却没有头绪,那是因为在RedHat linux操作系统中默认开启了防火墙,SELinux也处于启动状态,一般状桐逗悄态为enforing。致使很多服务端口默认是关闭的。

所以好多服务初学者明明

配置文件

正确,等验证时有时连ping也ping不通。建议初学者在未学到SELlinux与iptables之前,配置服务器把这两项都关掉。

扩展资局渣料

SELinux 的作用及权限管理机制:

SELinux 主要作用就是更大限度地减小系统中服务进程可访问的资源(最小权限原则)。

SELinux 有三种工作模式,分别是:

1、enforcing:强制模式。违反 SELinux 规则的行为将被阻止并记录到日志中。

2、permissive:宽容模式。违反 SELinux 规则的行为只会记录到日志中。一般为调试用。

3、disabled:关闭 SELinux。

SELinux 工作模式可以在 /etc/selinux/config 中设定。

如果想从 disabled 切换到 enforcing 或者 permissive 的话,需要重启系统。反过来也一样。

enforcing 和 permissive 模式可以通过 setenforce 1|0 命令快速切换。

需要注意的是,如果系统已经在关闭 SELinux 的状态下运行了一段时间,在打开 SELinux 之后的之一次重启速度可能会比较慢。因为系统必须为磁盘中的文件指运创建安全上下文。

SELinux 日志的记录需要借助 auditd.service 这个服务,请不要禁用它。

一般安装linux课程时都亩兄把SELinux与iptables安排在后面,使初学者配置linux服务器时不成功,却没有头绪,那是因为在RedHat linux操作系统中默认开启了防火墙,SELinux也处于启动状态,一般状态为enforing。致使很多服务端口默认是关闭的。所以好多服务初学者明明配置文件正确,等验证时有时迅举袭连ping也ping不通。建议初学者在未学到SELlinux与iptables之前,配置服务器把这两项都关掉。那么怎么关呢?

1、关闭iptables

#service iptables stop

2、关闭SELinux

#vi /etc/selinux/config

将文件中的SELINUX=”” 为 disabled ,然后重启。

如果不想重启系统,使用命令setenforce 0注:setenforce 1 设置SELinux 成为enforcing模式

setenforce 0 设置SELinux 成为permissive模式

在lilo或者grub的启动参数中增加:selinux=0,也可以答缓关闭selinux

#

查看selinux状态:

/usr/bin/setstatus -v如下:SELinux status: enabled

SELinuxfs mount: /selinux

Current mode: permissive

Mode from config file: enforcing

Policy version: 21

getenforce/setenforce查看和设置SELinux的当前工作模式

原因有几个:

1. 麻闹祥烦确实是一个因素,SELinux策略是白名单原则,所以你需要非常清楚你的各项操作都需要哪些访问权限,这个好像数量有丛尘点多了。

2. 使用SELinux的真的非常少。因为关掉它造成的损失可以有解释说别人也是这么做的。

3. 不确定性,因为一直没有搞过,然渗弯禅后所有的软件的文档里也没有写SELinux相关的配置,遇到相关问题时不好解决。

因为有些规则会阻止某些访问。

你要是高手也可锋携以不关,

如果是想学习Linux,那就关了吧

开了以后由于新手不懂设置,

很悄或可能造成各种外部的连接不成功,比如ftp啊telnet啊,ssh啥的

深入启基伍学习Linux可查看Linux书籍《Linux就该这么学》。

运维的小伙伴应该知道的安全模块SELinux

Security-Enhanced Linux ,是美国国家安全局(NSA=The National Security Agency)和SCC(Secure Computing Corporation)开发的 Linux的一个强制访问控制的安全模块。2023年以GNU GPL发布,Linux内核 2.6版本后集成在内核中

DAC:Discretionary Access Control自由访问控拿桥制

MAC:Mandatory Access Control 强制访问控制

对象(object):所有可以读取的对象,包括文件、目录和进程,端口等

主体:进程称为主体(subject)

SELinux中对所有的文件都赋予一个type的文件类型标签,对于所有的进程也赋 予各自的一个domain的标签。domain标签能够执行的操作由安全策略里定义

当一个subject试图访问一个object,Kernel中的策略执行服务器将检查AVC (访 问矢量缓存Access Vector Cache), 在AVC中,subject和object的权限被缓存 (cached),查找“应用+文件”的安全环境。然后根据查询结果允许或拒绝访问

安全策略:定义主体读取对象的规则数消裂猛据库,规则中记录了哪个类型的主体使用 哪个方法读取哪一个对象是允许还是拒绝的,并且定义了哪种行为是充许或拒绝

SELinux有四种工作类型:

targeted为默认类型,minimum和mls稳定性不足,未加以应用,strict已不再 使用

传统Linux,一切皆文件,由用户,组,权限控制访问

在SELinux中,一切皆对象(object),由存放在inode的扩展属性域的安全元 素所控制其访问

所有文件和端口资源和进程都具备安全标签:安全上下文源镇(security context) 安全上下文有五个元素组成:

user:role:type:sensitivity:category

user_u:object_r:tmp_t:s0:c0

实际上下文:存放在文件系统中,ls –Z;ps –Z

期望(默认)上下文:存放在二进制的SELinux策略库(映射目录和期望安全上下 文)中

semanage fcontext –l

配置SELinux:

SELinux的状态:

相关命令:

getenforce: 获取selinux当前状态

sestatus :查看selinux状态

setenforce 0|1

配置文件:

/boot/grub/grub.conf 在kernel行使用selinux=0禁用SELinux

/boot/grub2/grub.cfg 在linux16行使用selinux=0禁用SELinux

/etc/selinux/config

/etc/sysconfig/selinux

给文件重新打安全标签:

chcon … FILE…

chcon … –reference=RFILE FILE…

恢复目录或文件默认的安全上下文:

restorecon /path/to/somewhere

semanage:来自policycoreutils-python包

查看默认的安全上下文

semanage fcontext –l

添加安全上下文

semanage fcontext -a –t httpd_sys_content_t ‘/testdir(/.*)?’ restorecon –Rv /testdir

删除安全上下文

semanage fcontext -d –t httpd_sys_content_t ‘/testdir(/.*)?’

查看端口标签

semanage port –l

添加端口

semanage port -a -t port_label -p tcp|udp

PORT

semanage port -a -t http_port_t -p tcp 9527

删除端口

semanage port -d -t port_label -p tcp|udp

PORT

semanage port -d -t http_port_t -p tcp 9527

修改现有端口为新标签

semanage port -m -t port_label -p tcp|udp

PORT

semanage port -m -t http_port_t -p tcp 9527

布尔型规则:

getsebool

setsebool

查看bool命令:

getsebool

semanage boolean –l

semanage boolean -l –C 查看修改过的布尔值

设置bool值命令:

setsebool boolean value(on,off)

setsebool Boolean=value(1,0)

yum install setroubleshoot(重启生效)

将错误的信息写入/var/log/message

grep setroubleshoot /var/log/messages

查看安全事件日志说明

sealert -l UUID

扫描并分析日志

如何解决SELinux问题

有碰到即使是permissive也不行的,最后是改成disabled后重启机器才可以。

  首先需要确认SELinux处于激活状态,可以使用getenforce命令:

 兄凳 shell> getenforce

Enforcing

  或者使用sestatus命令:

  shell> sestatus

SELinux status: enabled

SELinuxfs mount: /selinux

Current mode: enforcing

Mode from config file:enforcing

Policy version: 24

Policy from config file:targeted

  注:关于SELinux的基础知识介绍请参考鸟哥的Linux私房菜中相关的介绍。

  我们还需要确认系统已经安装并启动了Apache,没有的话就YUM装一个,这很简单,就不多说了,接着在root目录创建一个测试文件test.html,如下:

  shell> cat /root/test.html

hello, world.

  然后把这个测试文件拷贝到Apache的DocumentRoot目录,我的Apache是通过YUM安装的话,缺省是/var/www/html目录,如下:

  shell> cp /root/test.html /var/www/html

  接着浏览一下,如果没出什么幺蛾子,应该一切都在意料之中,如下:

  shell> curl

hello, world.

  看到这,你可能觉得我废话连篇,别着急,下面就是见证奇迹的时候了:

  同样还是那个测试文件test.html,不过这次不再是拷贝,而是移动,如下:

  shell> mv /root/test.html /var/www/html

  接着浏览一下,怎么样,结果很出人意料吧,竟然提示权限错误,如下:

  shell> curl

403 Forbidden

Forbidden

You don’t have permission to access /test.html

on this server.

  当然,我们现在知道这个问题是由于SELinux引起的,但还不知其所以然,实际上问题的原因此时已经被蚂郑audit进程记录到了相应的日志里,可以这羡物旅样查看:

  shell> audit2why yum install setroubleshoot

  它本身是一个GUI套件,不过其中包含的一个sealert命令对我们命令行用户很有用:

  shell> sealert -a /var/log/audit/audit.log

Summary:

SELinux is preventing /usr/in/httpd “getattr” access to

/var/www/html/test.html.

Detailed Description:

SELinux denied access requested by httpd. /var/www/html/test.html may be a

mislabeled. /var/www/html/test.html default SELinux type is httpd_sys_content_t,

but its current type is admin_home_t. Changing this file back to the default

type, may fix your problem.

File contexts can be assigned to a file in the following ways.

* Files created in a directory receive the file context of the parent

directory by default.

* The SELinux policy might override the default label inherited from the

parent directory by specifying a process running in context A which creates

a file in a directory labeled B will instead create the file with label C.

An example of this would be the dhcp client running with the dhclient_t type

and creating a file in the directory /etc. This file would normally receive

the etc_t type due to parental inheritance but instead the file is labeled

with the net_conf_t type because the SELinux policy specifies this.

* Users can change the file context on a file using tools such as chcon, or

restorecon.

This file could have been mislabeled either by user error, or if an normally

confined application was run under the wrong domain.

However, this might also indicate a bug in SELinux because the file should not

have been labeled with this type.

If you believe this is a bug, please file a bug report against this package.

Allowing Access:

You can restore the default system context to this file by executing the

restorecon command. restorecon ‘/var/www/html/test.html’, if this file is a

directory, you can recursively restore using restorecon -R

‘/var/www/html/test.html’.

Fix Command:

/in/restorecon ‘/var/www/html/test.html’

  这次应该看懂了吧!原因是说Apache下文件上下文类型应该是httpd_sys_content_t,但是现在是admin_home_t,所以权限错误,并且在结尾处给出了修复命令。

  可httpd_sys_content_t,admin_home_t都怎么看啊?很简单,借助ls命令的-Z参数即可:

  shell> ls -Z /path

  回到问题的开始,拷贝之所以没出现问题,是因为cp自动修改上下文属性,而移动之所以出现问题是因为mv保留原文件的上下文属性。

  注:关于SELinux和Apache的详细介绍,可以参考『man httpd_selinux』。

  知道了如何解决SELinux问题,以后如果遇到类似的情况不要急着武断的关闭SELinux。

上面有个小错,应该是echo “0” > /selinux/enforce (少了>)selinux如果有开很容易雀尘造成ifind问题,参看下面系统日志Aug 2 20:52:06 cc-lnx-imglib setroubleshoot: SELinux is preventing /opt/simpana/iDataAgent/ifind “execstack”梁岁枯 access to . For complete SELinux messages. run sealert -l 4a0b3993-b18f-4c72-9e1d-4f5d8175ad41Jul 31 00:30:09 cc-lnx-imglib setroubleshoot: SELinux is preventing /橡洞bin/hostname access to a leaked /tmp/.gxsetup/cvpkglo g.pipe.26262 file descriptor. For complete SELinux messages. run sealert -l e675a6a6-efc9-461f-bc0fd2d308

别一种查看SElinux方法:# sestatusSELinux status: disabled

selinux 内核禁用的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于selinux 内核禁用,如何禁用SELinux内核保护?,linux 为什么要关闭selinux,运维的小伙伴应该知道的安全模块SELinux,如何解决SELinux问题的信息别忘了在本站进行查找喔。


数据运维技术 » 如何禁用SELinux内核保护? (selinux 内核禁用)