selinux 综合大全

本文详细介绍了SELinux的相关命令,包括sestatus、selinuxenabled、setenforce、getsebool、restorecon、chcon等,以及如何通过setsebool配置SELinux布尔值。同时,提到了SELinux的配置文件、日志查看以及在ftp、httpd、named、nfs、samba、rsync、kerberos和nis等服务中的应用实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

selinux 相关命令

1.sestatus 查询系统的selinux目前的狀态
#sestatus -v


2. selinuxenabled 查询系统的selinux支 援是否有启用
3.setenforce 和 getenforce 设定/显示selinux运 作狀态


4.getsebool 列出所有selinux bool数值清单列表与内容
5.setsebool 设定selinux bool数值内容

6.restorecon 恢復档案目录的预设的security context (-R 可针对整过目录做递归修复)
7.chcon 变更档案目录security context
8.fixfiles 修正档案目录的预设的security context
9.secon 检视行程、档案等等项目的SELinux context

10.semanage SELiux policy管理程式
11.audit2why 检视SELinux audit讯息内容
12.sealert SELinux 讯息诊断用户端程式

selinux的配置目录和相关文件:/etc/selinux/config  其中重启后selinux不失效 改其中的permissive 为 enforce

selinux 日志的组件  setroubleshoot, setroubleshoot.plugins.noarch, setroubleshoot-server 包,可以用 yum list installed | grep setroubleshoot 查看一下。

平常工作中经常碰到selinux问题,首先查的就是日志。

selinux 日志:/var/log/audit.log 中带有 avc 关键字的日志,方便它们从其它信息中过滤出来。

#grep AVC audit.log | tail -l

也可用sealert -b 非图形界面显示
sealert -a /var/log/audit/audit.log > /tmp/sealert.txt  查看

********************************************************************************************************************

chcon 用法实例 (转)

chcon命令:修改对象(文件)的安全上下文。比如:用户:角色:类型:安全级别。
命令格式:

   Chcon [OPTIONS…] CONTEXT FILES…..

   Chcon [OPTIONS…] –reference=PEF_FILES FILES…

说明:

    CONTEXT 为要设置的安全上下文

    FILES 对象(文件)

    --reference 参照的对象

    PEF_FILES 参照文件上下文

    FILES      应用参照文件上下文为我的上下文。

    OPTIONS 如下:

        -f          强迫执行

        -R         递归地修改对象的安全上下文

        -r ROLE    修改安全上下文角色的配置

        -t TYPE    修改安全上下文类型的配置

        -u USER   修改安全上下文用户的配置

        -v         显示冗长的信息

         -l, --range=RANGE    修改安全上下文中的安全级别

范例:

1、ftp

//If you want to share files anonymously <如果你想把这个共享给匿名的话,需要开启以下>
chcon -R -t public_content_t /var/ftp

//If you want to setup a directory where you can upload files
<如果你想让你设置的FTP目录可以上传文件的话,SELINUX需要设置>
chcon -t public_content_rw_t /var/ftp/incoming

//You must also turn on the boolean allow_ftpd_anon_write <允许匿名用户写入权限>
setsebool -P allow_ftpd_anon_write=1

//If you are setting up this machine as a ftpd server and wish to allow users to access their home directorories<如果你希望你的FTP用户可以访问自己的家目录的话,需要开启>
setsebool -P ftp_home_dir 1

//If you want to run ftpd as a daemon<如果你希望将vsftpd以daemon的方式运行的话,需要开启>
setsebool -P ftpd_is_daemon 1

//You can disable SELinux protection for the ftpd daemon<你可以让SElinux停止保护vsftpd的daemon方式动行>
setsebool -P ftpd_disable_trans 1
 

 2、httpd
//If you want a particular domain to write to the public_content_rw_t domain
<如果希望具体个doman具有可写权限的话,需要设置>
setsebool -P allow_httpd_anon_write=1
or
setsebool -P allow_httpd_sys__anon_write=1

//httpd can be setup to allow cgi s to be executed <HTTP被设置允许cgi的设置>
setsebool -P httpd_enable_cgi 1

//If you want to allow access to users home directories<允许用户HHTP访问其家目录,该设定限仅于用户的家目录主页>
setsebool -P httpd_enable_homedirs 1
chcon -R -t httpd_sys_content_t ~user/public_html

//httpd is allowed access to the controling terminal<允许httpd访问终端>
setsebool -P httpd_tty_comm 1

//such that one httpd service can not interfere with another
setsebool -P httpd_unified 0

//loadable modules run under the same context as httpd
setsebool -P httpd_builtin_ing 0

//httpd s are allowed to connect out to the network
setsebool -P httpd_can_network_connect 1

// You can disable suexec transition
setsebool -P httpd_suexec_disable_trans 1

//You can disable SELinux protection for the httpd daemon by executing <关闭Selinux的关于httpd进程守护的保护>
setsebool -P httpd_disable_trans 1
service httpd restart

3、named

//If you want to have named update the master zone files <关于named,master更新selinux设定>
setsebool -P named_write_master_zones 1

//You can disable SELinux protection for the named daemon by executing
<关闭named的进程守护保护>
setsebool -P named_disable_trans 1
service named restart

4、nfs

//If you want to setup this machine to share nfs partitions read only
<Selinux将本机的NFS共享设置成只读>
setsebool -P nfs_export_all_ro 1

//If you want to share files read/write<Selinux将本机的NFS共享设置成可读可写>
setsebool -P nfs_export_all_rw 1

//If you want to use a remote NFS server for the home directories on this machine
<如果你想要将远程NFS的家目录共享到本机,需要开启>
setsebool -P use_nfs_home_dirs 1

5、samba

//If you want to share files other than home directorie
<如果你希望将目录共享给其他用户,你需要设置>
chcon -t samba_share_t /directory

//If you want to share files with multiple domains
如果samba服务器共享目录给多个域,则需要:
setsebool -P allow_smbd_anon_write=1

//If you are setting up this machine as a Samba server and wish to share the home directories
samba服务器要共享家目录时:
setsebool -P samba_enable_home_dirs 1

//If you want to use a remote Samba server for the home directories on this machine
如果你需在本机上使用远程samba服务器的家目录
setsebool -P use_samba_home_dirs 1

//You can disable SELinux protection for the samba daemon by executing
关闭selinux关于samba的进程守护的保护
setsebool -P smbd_disable_trans 1
service smb restart

6、rsync

//If you want to share files using the rsync daemon
共享rsync目录时:
chcon -t public_content_t /directories

//If you want to share files with multiple domains
允许其他用户写入时
setsebool -P allow_rsync_anon_write=1

//You can disable SELinux protection for the rsync daemon by executing
停止rsync的进程保护
setsebool -P rsync_disable_trans 1

7、kerberos

//allow your system to work properly in a Kerberos environment
允许系统使用kerberos
setsebool -P allow_kerberos 1

//If you are running Kerberos daemons kadmind or krb5kdc
setsebool -P krb5kdc_disable_trans 1
service krb5kdc restart
setsebool -P kadmind_disable_trans 1
service kadmind restart

8、nis

Allow your system to work properly in a NIS environment
系统工作在nis环境时
setsebool -P allow_ypbind 1

 

转载于:https://www.cnblogs.com/samhugh/p/4386536.html

<think>我们正在回答用户关于如何关闭Linux系统中SELinux的问题。根据引用内容,我们可以提供两种关闭方式:临时关闭和永久关闭。注意:引用中提供了两种方式,我们需要清晰地分步骤说明。步骤:1.首先,需要以root权限执行操作(可以使用sudo su-或直接使用root用户)。2.验证当前SELinux的状态(使用getenforce命令)。3.根据需求选择临时关闭或永久关闭。临时关闭:-执行命令:setenforce0-注意:临时关闭在系统重启后会失效。永久关闭:-编辑SELinux的配置文件:/etc/selinux/config-将SELINUX参数的值从enforcing改为disabled-保存文件并重启系统。注意:永久关闭需要重启系统才能生效。同时,我们还需要提醒用户:-关闭SELinux可能会降低系统的安全性,请确保在安全的环境下操作。-如果只是临时测试,建议使用临时关闭。根据引用内容,我们有两个引用源,但内容基本一致,我们可以综合给出步骤。回答结构:1.介绍关闭SELinux的两种方式。2.分步骤详细说明每种方式的操作。3.给出注意事项。同时,按照要求,我们需要生成相关问题。生成相关问题:1.关闭SELinux后如何重新启用?2.为什么需要关闭SELinux?3.如何检查SELinux的当前状态?4.关闭SELinux对系统安全有什么影响?注意:在回答中,我们引用了两处内容,在末尾需要添加引用标识[^1][^2]。但是,由于用户提供的两个引用内容相似,我们可以合并引用,或者分别引用。由于引用[1]和引用[2]都是关于关闭SELinux的方法,我们可以根据具体步骤引用对应的部分。但为了简洁,我们可以统一标注引用。然而,用户提供的引用内容中,引用[1]和引用[2]都包含了相似的操作步骤,我们可以综合引用。在回答中,我们可能会同时用到两个引用,因此可以在末尾添加[^1][^2]。但是,注意用户指令:引用的段落末尾自然地添加引用标识。因此,我们在提到具体步骤时,如果直接来自引用,则添加引用标识。由于整个步骤都来自引用,我们可以在最后统一添加引用标识。我们这样处理:在回答的最后,添加引用说明,例如:以上步骤参考了引用[^1]和引用[^2]。现在,我们开始组织回答。</think>### 如何关闭 Linux 系统中的 SELinux 以下是关闭 SELinux 的两种方法,请根据需求选择操作方式: #### 📌 一、临时关闭(重启后失效) 1. **获取 root 权限** ```bash sudo su - ``` 2. **验证 SELinux 状态** ```bash getenforce ``` - 若返回 `Enforcing` 表示已启用(需关闭)[^1][^2] 3. **临时关闭 SELinux** ```bash setenforce 0 ``` - 立即生效,无需重启 - 重启系统后会恢复原状态 #### 🔒 二、永久关闭(需重启生效) 1. **编辑配置文件** ```bash vim /etc/selinux/config ``` 2. **修改配置参数** 找到 `SELINUX=enforcing` 改为: ```bash SELINUX=disabled ``` ![配置修改示意图](https://example.com/selinux-config.png) (按 `i` 进入编辑模式,修改后按 `ESC` → 输入 `:wq` 保存) 3. **重启系统生效** ```bash reboot ``` 4. **验证关闭状态** ```bash sestatus ``` - 应显示:`SELinux status: disabled` > ⚠️ **注意事项** > - 永久关闭需重启生效 > - 关闭 SELinux 会降低系统安全性,仅建议测试环境使用 > - 企业生产环境推荐保持启用并配置安全策略[^1][^2] --- ### 📚 相关问题 1. 关闭 SELinux 后如何重新启用? 2. 如何在不重启的情况下临时禁用 SELinux? 3. SELinux 的宽容模式(permissive)有什么作用? 4. 为什么某些服务在 SELinux 开启时会出现权限错误? 5. 如何查看 SELinux 的审计日志以排查问题? [^1]: Linux关闭SELinux操作指南 [^2]: 最新Linux中开启或关闭SELinux
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值