如何开启或关闭SELinux

查看selinux

[root@mode ~]# getenforce
Disabled
[root@mode ~]# /usr/sbin/sestatus -v
SELinux status:                 disabled

临时关闭selinux
[root@mode ~]# setenforce 0
setenforce: SELinux is disabled

永久关闭selinux的两种方式

[root@mode ~]# vi /etc/sysconfig/selinux 



# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#       enforcing - SELinux security policy is enforced.
#       permissive - SELinux prints warnings instead of enforcing.
#       disabled - SELinux is fully disabled.
SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
#       targeted - Only targeted network daemons are protected.
#       strict - Full SELinux protection.
SELINUXTYPE=targeted
~
~

第二种方式:

~
[root@mode ~]# cat /boot/grub/menu.lst
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/sda3
#          initrd /initrd-version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
password --md5 $1$P6MX17N9$upRaE9GPh/J6Y2.weHY7B0
title CentOS (2.6.18-308.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-308.el5 ro root=LABEL=/ selinux=0
        initrd /initrd-2.6.18-308.el5.img

### 如何在 Linux 中临时永久关闭 SELinux #### 临时关闭 SELinux 可以通过执行命令来快速实现 SELinux 的临时关闭。此方法无需重启系统即可生效,适用于需要短时间内禁用 SELinux 场景。 运行以下命令可将 SELinux 设置为宽容模式(Permissive Mode),即不强制执行安全策略但仍记录违规行为: ```bash setenforce 0 ``` 若需重新启用 SELinux 并恢复到强制模式(Enforcing Mode),则可以运行: ```bash setenforce 1 ``` 以上操作仅影响当前会话,在系统重启后设置会被重置[^1]。 #### 永久关闭 SELinux 为了使更改持久化至每次启动时都保持关闭状态,则需要修改系统的配置文件 `/etc/selinux/config`: 打开该文件并找到 `SELINUX=enforcing` 这一行将其改为 `SELINUX=disabled` 者更改为其他所需的状态如 permissive。 ```bash vim /etc/selinux/config ``` 编辑完成后保存退出。随后需要重启计算机让改动生效。 ```bash reboot ``` 完成上述步骤之后,SELinux 将被彻底停用直到再次手动开启它为止[^4]。 另外需要注意的是,在某些 Android 设备上也可以利用 ADB 工具配合 root 权限来进行类似的 selinux 控制工作[^3];而在一般服务器环境中通常按照标准 linux 方法处理即可满足需求[^2]。 ```python # Python 示例代码展示如何通过脚本调用 shell 命令来控制 SELinux (仅供参考) import os def disable_selinux_temporarily(): result = os.system('setenforce 0') return 'Success' if not result else 'Failure' print(disable_selinux_temporarily()) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值